Skip to content

Commit b277fe2

Browse files
committed
Add typespecs for Connection and Channel
1 parent 25c2fd4 commit b277fe2

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
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: 13 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,7 @@ 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()) :: :ok | {:error, :timeout} | {:error, :closed}
114120
def send(channel, type \\ 0, data, timeout \\ :infinity)
115121

116122
def send(channel, type, data, timeout) when is_binary(data) or is_list(data) do
@@ -131,6 +137,7 @@ defmodule SSHKit.Channel do
131137
132138
For more details, see [`:ssh_connection.send_eof/2`](http://erlang.org/doc/man/ssh_connection.html#send_eof-2).
133139
"""
140+
@spec eof(t()) :: :ok | {:error, term()}
134141
def eof(channel) do
135142
@core.send_eof(channel.connection.ref, channel.id)
136143
end
@@ -155,6 +162,7 @@ defmodule SSHKit.Channel do
155162
156163
For more details, see [`:ssh_connection`](http://erlang.org/doc/man/ssh_connection.html).
157164
"""
165+
@spec recv(t(), timeout()) :: {:ok, tuple()} | {:error, term()}
158166
def recv(channel, timeout \\ :infinity) do
159167
ref = channel.connection.ref
160168
id = channel.id
@@ -173,6 +181,7 @@ defmodule SSHKit.Channel do
173181
174182
Returns `:ok`.
175183
"""
184+
@spec flush(t(), timeout()) :: :ok
176185
def flush(channel, timeout \\ 0) do
177186
ref = channel.connection.ref
178187
id = channel.id
@@ -191,6 +200,7 @@ defmodule SSHKit.Channel do
191200
192201
For more details, see [`:ssh_connection.adjust_window/3`](http://erlang.org/doc/man/ssh_connection.html#adjust_window-3).
193202
"""
203+
@spec adjust(t(), non_neg_integer()) :: :ok
194204
def adjust(channel, size) when is_integer(size) do
195205
@core.adjust_window(channel.connection.ref, channel.id, size)
196206
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

0 commit comments

Comments
 (0)