Skip to content

Commit 25c2fd4

Browse files
committed
Add typespecs for Upload
1 parent ef90bcb commit 25c2fd4

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

examples/upload.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
:ok =
44
conn
5-
|> SSHKit.upload!("test/fixtures", "/tmp/fixtures", recursive: true)
5+
|> SSHKit.upload!("test/fixtures", "/tmp/fixtures")
66
|> Stream.run()
77

88
:ok = SSHKit.close(conn)

lib/sshkit/transfer.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule SSHKit.Transfer do
1616
if module.done?(transfer) do
1717
{:halt, transfer}
1818
else
19-
{:ok, transfer} = module.continue(transfer)
19+
{:ok, transfer} = module.step(transfer)
2020
{[transfer], transfer}
2121
end
2222
end,

lib/sshkit/upload.ex

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,41 @@ defmodule SSHKit.Upload do
33
TODO
44
"""
55

6+
alias SSHKit.Connection
67
alias SSHKit.SFTP.Channel
78

89
defstruct [:source, :target, :options, :cwd, :stack, :channel]
910

1011
@type t() :: %__MODULE__{}
1112

13+
@spec init(binary(), binary(), keyword()) :: t()
1214
def init(source, target, options \\ []) do
1315
%__MODULE__{source: Path.expand(source), target: target, options: options}
1416
end
1517

18+
@spec start(t(), Connection.t()) :: {:ok, t()} | {:error, term()}
1619
def start(%__MODULE__{options: options} = upload, conn) do
1720
# accepts options like timeout… http://erlang.org/doc/man/ssh_sftp.html#start_channel-1
1821
start_options =
1922
options
2023
|> Keyword.get(:start, [])
2124
|> Keyword.put_new(:timeout, Keyword.get(options, :timeout, :infinity))
2225

23-
with {:ok, upload} <- prepare(upload),
26+
with {:ok, upload} <- preflight(upload),
2427
{:ok, chan} <- Channel.start(conn, start_options) do
2528
{:ok, %{upload | channel: chan}}
2629
end
2730
end
2831

29-
defp prepare(%__MODULE__{source: source, options: options} = upload) do
30-
if !Keyword.get(options, :recursive, false) && File.dir?(source) do
31-
# TODO: Better error
32-
{:error, "Option :recursive not specified, but local file is a directory (#{source})"}
33-
else
32+
defp preflight(%__MODULE__{source: source, options: options} = upload) do
33+
if File.exists?(source) do
3434
{:ok, %{upload | cwd: Path.dirname(source), stack: [[Path.basename(source)]]}}
35+
else
36+
{:error, :enoent}
3537
end
3638
end
3739

40+
@spec stop(t()) :: {:ok, t()} | {:error, term()}
3841
def stop(%__MODULE__{channel: nil} = upload), do: {:ok, upload}
3942

4043
def stop(%__MODULE__{channel: chan} = upload) do
@@ -45,15 +48,16 @@ defmodule SSHKit.Upload do
4548

4649
# TODO: Handle unstarted uploads w/o channel, cwd, stack… and provide helpful error?
4750

48-
def continue(%__MODULE__{stack: []} = upload) do
51+
@spec step(t()) :: {:ok, t()} | {:error, term()}
52+
def step(%__MODULE__{stack: []} = upload) do
4953
{:ok, upload}
5054
end
5155

52-
def continue(%__MODULE__{stack: [[] | paths]} = upload) do
56+
def step(%__MODULE__{stack: [[] | paths]} = upload) do
5357
{:ok, %{upload | cwd: Path.dirname(upload.cwd), stack: paths}}
5458
end
5559

56-
def continue(%__MODULE__{stack: [[name | rest] | paths]} = upload) do
60+
def step(%__MODULE__{stack: [[name | rest] | paths]} = upload) do
5761
path = Path.join(upload.cwd, name)
5862
relpath = Path.relative_to(path, upload.source)
5963
relpath = if relpath == path, do: ".", else: relpath
@@ -101,6 +105,7 @@ defmodule SSHKit.Upload do
101105
|> Enum.find(:ok, &(&1 != :ok))
102106
end
103107

104-
def done?(%{stack: []}), do: true
105-
def done?(%{}), do: false
108+
@spec done?(t()) :: boolean()
109+
def done?(%__MODULE__{stack: []}), do: true
110+
def done?(%__MODULE__{}), do: false
106111
end

0 commit comments

Comments
 (0)