Skip to content

Commit c1f9c85

Browse files
committed
Add typespecs for Connection and Channel
1 parent 412970c commit c1f9c85

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

lib/sshkit.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ defmodule SSHKit do
129129

130130
output = Enum.reverse(output)
131131

132-
# TODO: Proper file struct?
132+
# TODO: Proper error struct?
133133
if status != 0, do: raise("Non-zero exit code: #{status}")
134134

135135
output

lib/sshkit/channel.ex

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ defmodule SSHKit.Channel do
99
* `id` - the unique channel id
1010
"""
1111

12+
alias SSHKit.Connection
13+
1214
defstruct [:connection, :type, :id]
1315

1416
@type t() :: %__MODULE__{}
@@ -30,6 +32,7 @@ defmodule SSHKit.Channel do
3032
* `:initial_window_size` - defaults to 128 KiB
3133
* `:max_packet_size` - defaults to 32 KiB
3234
"""
35+
@spec open(Connection.t(), keyword()) :: {:ok, t()} | {:error, term()}
3336
def open(conn, options \\ []) do
3437
timeout = Keyword.get(options, :timeout, :infinity)
3538
ini_window_size = Keyword.get(options, :initial_window_size, 128 * 1024)
@@ -51,8 +54,7 @@ defmodule SSHKit.Channel do
5154
5255
For more details, see [`:ssh_connection.subsystem/4`](http://erlang.org/doc/man/ssh_connection.html#subsystem-4).
5356
"""
54-
@spec subsystem(channel :: struct(), subsystem :: String.t(), options :: list()) ::
55-
:success | :failure | {:error, reason :: String.t()}
57+
@spec subsystem(t(), binary(), keyword()) :: :success | :failure | {:error, term()}
5658
def subsystem(channel, subsystem, options \\ []) do
5759
timeout = Keyword.get(options, :timeout, :infinity)
5860
@core.subsystem(channel.connection.ref, channel.id, to_charlist(subsystem), timeout)
@@ -65,6 +67,7 @@ defmodule SSHKit.Channel do
6567
6668
For more details, see [`:ssh_connection.close/2`](http://erlang.org/doc/man/ssh_connection.html#close-2).
6769
"""
70+
@spec close(t()) :: :ok
6871
def close(channel) do
6972
@core.close(channel.connection.ref, channel.id)
7073
end
@@ -81,6 +84,7 @@ defmodule SSHKit.Channel do
8184
`loop/4` may be used to process any channel messages received as a result of
8285
executing `command` on the remote.
8386
"""
87+
@spec exec(t(), binary() | charlist(), timeout()) :: :success | :failure | {:error, term()}
8488
def exec(channel, command, timeout \\ :infinity)
8589

8690
def exec(channel, command, timeout) when is_binary(command) do
@@ -94,10 +98,11 @@ defmodule SSHKit.Channel do
9498
@doc """
9599
Allocates PTTY.
96100
97-
Returns `:success`.
101+
Returns `:success`, `:failure` or `{:error, reason}`.
98102
99103
For more details, see [`:ssh_connection.ptty_alloc/4`](http://erlang.org/doc/man/ssh_connection.html#ptty_alloc-4).
100104
"""
105+
@spec ptty(t(), keyword(), timeout()) :: :success | :failure | {:error, term()}
101106
def ptty(channel, options \\ [], timeout \\ :infinity) do
102107
@core.ptty_alloc(channel.connection.ref, channel.id, options, timeout)
103108
end
@@ -111,6 +116,8 @@ defmodule SSHKit.Channel do
111116
112117
For more details, see [`:ssh_connection.send/5`](http://erlang.org/doc/man/ssh_connection.html#send-5).
113118
"""
119+
@spec send(t(), non_neg_integer(), term(), timeout()) ::
120+
:ok | {:error, :timeout} | {:error, :closed}
114121
def send(channel, type \\ 0, data, timeout \\ :infinity)
115122

116123
def send(channel, type, data, timeout) when is_binary(data) or is_list(data) do
@@ -131,6 +138,7 @@ defmodule SSHKit.Channel do
131138
132139
For more details, see [`:ssh_connection.send_eof/2`](http://erlang.org/doc/man/ssh_connection.html#send_eof-2).
133140
"""
141+
@spec eof(t()) :: :ok | {:error, term()}
134142
def eof(channel) do
135143
@core.send_eof(channel.connection.ref, channel.id)
136144
end
@@ -155,6 +163,7 @@ defmodule SSHKit.Channel do
155163
156164
For more details, see [`:ssh_connection`](http://erlang.org/doc/man/ssh_connection.html).
157165
"""
166+
@spec recv(t(), timeout()) :: {:ok, tuple()} | {:error, term()}
158167
def recv(channel, timeout \\ :infinity) do
159168
ref = channel.connection.ref
160169
id = channel.id
@@ -173,6 +182,7 @@ defmodule SSHKit.Channel do
173182
174183
Returns `:ok`.
175184
"""
185+
@spec flush(t(), timeout()) :: :ok
176186
def flush(channel, timeout \\ 0) do
177187
ref = channel.connection.ref
178188
id = channel.id
@@ -191,6 +201,7 @@ defmodule SSHKit.Channel do
191201
192202
For more details, see [`:ssh_connection.adjust_window/3`](http://erlang.org/doc/man/ssh_connection.html#adjust_window-3).
193203
"""
204+
@spec adjust(t(), non_neg_integer()) :: :ok
194205
def adjust(channel, size) when is_integer(size) do
195206
@core.adjust_window(channel.connection.ref, channel.id, size)
196207
end

lib/sshkit/connection.ex

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,14 @@ defmodule SSHKit.Connection do
4040
4141
Returns `{:ok, conn}` on success, `{:error, reason}` otherwise.
4242
"""
43+
@spec open(binary() | charlist(), keyword()) :: {:ok, t()} | {:error, term()}
4344
def open(host, options \\ [])
4445

45-
def open(nil, _) do
46-
{:error, "No host given."}
47-
end
48-
4946
def open(host, options) when is_binary(host) do
5047
open(to_charlist(host), options)
5148
end
5249

53-
def open(host, options) do
50+
def open(host, options) when is_list(host) do
5451
{details, opts} = extract(options)
5552

5653
port = details[:port]
@@ -89,6 +86,7 @@ defmodule SSHKit.Connection do
8986
9087
For details, see [`:ssh.close/1`](http://erlang.org/doc/man/ssh.html#close-1).
9188
"""
89+
@spec close(t()) :: :ok
9290
def close(conn) do
9391
@core.close(conn.ref)
9492
end
@@ -103,6 +101,7 @@ defmodule SSHKit.Connection do
103101
104102
Returns `{:ok, conn}` or `{:error, reason}`.
105103
"""
104+
@spec reopen(term(), keyword()) :: {:ok, t()} | {:error, term()}
106105
def reopen(conn, options \\ []) do
107106
options =
108107
conn.options

test/sshkit/connection_test.exs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ defmodule SSHKit.ConnectionTest do
119119

120120
assert open("test.io") == {:error, :failed}
121121
end
122-
123-
test "returns an error if no host is given" do
124-
assert open(nil) == {:error, "No host given."}
125-
end
126122
end
127123

128124
describe "close/1" do

0 commit comments

Comments
 (0)