Skip to content

Commit 6154f77

Browse files
committed
Add :context option to exec!/3 and run!/3
1 parent c4ce38d commit 6154f77

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

examples/context.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{:ok, conn} = SSHKit.connect("127.0.0.1", port: 2222, user: "deploy", password: "deploy", silently_accept_hosts: true)
2+
3+
context =
4+
SSHKit.Context.new()
5+
|> SSHKit.Context.path("/tmp")
6+
|> SSHKit.Context.umask("007")
7+
|> SSHKit.Context.env(%{"X" => "Y"})
8+
9+
conn
10+
|> SSHKit.run!(~S(echo $X && pwd && umask && id -un && id -gn), context: context)
11+
|> Enum.each(fn {type, data} -> IO.write("#{type}: #{data}") end)
12+
13+
:ok = SSHKit.close(conn)

lib/sshkit.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ defmodule SSHKit do
2020

2121
alias SSHKit.Channel
2222
alias SSHKit.Connection
23+
alias SSHKit.Context
2324
alias SSHKit.Download
2425
alias SSHKit.Transfer
2526
alias SSHKit.Upload
@@ -43,6 +44,10 @@ defmodule SSHKit do
4344

4445
@spec exec!(Connection.t(), binary(), keyword()) :: Enumerable.t()
4546
def exec!(conn, command, options \\ []) do
47+
{context, options} = Keyword.pop(options, :context, Context.new())
48+
49+
command = Context.build(context, command)
50+
4651
# TODO: Separate options for open/exec/recv
4752
Stream.resource(
4853
fn ->
@@ -115,6 +120,11 @@ defmodule SSHKit do
115120
def send(chan, :stdout, data, timeout), do: Channel.send(chan, 0, data, timeout)
116121
def send(chan, :stderr, data, timeout), do: Channel.send(chan, 1, data, timeout)
117122

123+
@doc """
124+
TODO
125+
126+
Accepts the same options as `exec!/3`.
127+
"""
118128
@spec run!(Connection.t(), binary(), keyword()) :: [{:stdout | :stderr, binary()}]
119129
def run!(conn, command, options \\ []) do
120130
stream = exec!(conn, command, options)

0 commit comments

Comments
 (0)