@@ -112,9 +112,9 @@ extension SSHClient {
112
112
/// - maxResponseSize: The maximum size of the response. If the response is larger, the command will fail.
113
113
/// - mergeStreams: If the answer should also include stderr.
114
114
/// - inShell: Whether to request the remote server to start a shell before executing the command.
115
- public func executeCommand( _ command: String , maxResponseSize: Int = . max, mergeStreams: Bool = false , inShell: Bool = false ) async throws -> ByteBuffer {
115
+ public func executeCommand( _ command: String , maxResponseSize: Int = . max, mergeStreams: Bool = false , inShell: Bool = false , environment : [ String : String ] = [ : ] ) async throws -> ByteBuffer {
116
116
var result = ByteBuffer ( )
117
- let stream = try await executeCommandStream ( command, inShell: inShell)
117
+ let stream = try await executeCommandStream ( command, inShell: inShell, environment : environment )
118
118
119
119
for try await chunk in stream {
120
120
switch chunk {
@@ -142,7 +142,7 @@ extension SSHClient {
142
142
/// - Parameters:
143
143
/// - command: The command to execute.
144
144
/// - inShell: Whether to request the remote server to start a shell before executing the command.
145
- public func executeCommandStream( _ command: String , inShell: Bool = false ) async throws -> AsyncThrowingStream < ExecCommandOutput , Error > {
145
+ public func executeCommandStream( _ command: String , inShell: Bool = false , environment : [ String : String ] = [ : ] ) async throws -> AsyncThrowingStream < ExecCommandOutput , Error > {
146
146
var streamContinuation : AsyncThrowingStream < ExecCommandOutput , Error > . Continuation !
147
147
let stream = AsyncThrowingStream < ExecCommandOutput , Error > { continuation in
148
148
streamContinuation = continuation
@@ -181,6 +181,12 @@ extension SSHClient {
181
181
return createChannel. futureResult
182
182
} . get ( )
183
183
184
+ for (key, value) in environment {
185
+ try await channel. triggerUserOutboundEvent ( SSHChannelRequestEvent . EnvironmentRequest (
186
+ wantReply: true , name: key, value: value)
187
+ )
188
+ }
189
+
184
190
if inShell {
185
191
try await channel. triggerUserOutboundEvent ( SSHChannelRequestEvent . ShellRequest (
186
192
wantReply: true
@@ -198,7 +204,7 @@ extension SSHClient {
198
204
/// Executes a command on the remote server. This will return the pair of streams stdout and stderr of the command. If the command fails, the error will be thrown.
199
205
/// - Parameters:
200
206
/// - command: The command to execute.
201
- public func executeCommandPair( _ command: String , inShell: Bool = false ) async throws -> ExecCommandStream {
207
+ public func executeCommandPair( _ command: String , inShell: Bool = false , environment : [ String : String ] = [ : ] ) async throws -> ExecCommandStream {
202
208
var stdoutContinuation : AsyncThrowingStream < ByteBuffer , Error > . Continuation !
203
209
var stderrContinuation : AsyncThrowingStream < ByteBuffer , Error > . Continuation !
204
210
let stdout = AsyncThrowingStream < ByteBuffer , Error > { continuation in
@@ -216,7 +222,7 @@ extension SSHClient {
216
222
217
223
Task {
218
224
do {
219
- let stream = try await executeCommandStream ( command, inShell: inShell)
225
+ let stream = try await executeCommandStream ( command, inShell: inShell, environment : environment )
220
226
for try await chunk in stream {
221
227
switch chunk {
222
228
case . stdout( let buffer) :
0 commit comments