Skip to content

Commit cc756dc

Browse files
authored
Merge pull request #53 from finestructure/add-support-for-environment-variables
Add support for environment variables
2 parents 806cff7 + 7e52379 commit cc756dc

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Sources/Citadel/TTY/Client/TTY.swift

+11-5
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ extension SSHClient {
112112
/// - maxResponseSize: The maximum size of the response. If the response is larger, the command will fail.
113113
/// - mergeStreams: If the answer should also include stderr.
114114
/// - 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 {
116116
var result = ByteBuffer()
117-
let stream = try await executeCommandStream(command, inShell: inShell)
117+
let stream = try await executeCommandStream(command, inShell: inShell, environment: environment)
118118

119119
for try await chunk in stream {
120120
switch chunk {
@@ -142,7 +142,7 @@ extension SSHClient {
142142
/// - Parameters:
143143
/// - command: The command to execute.
144144
/// - 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> {
146146
var streamContinuation: AsyncThrowingStream<ExecCommandOutput, Error>.Continuation!
147147
let stream = AsyncThrowingStream<ExecCommandOutput, Error> { continuation in
148148
streamContinuation = continuation
@@ -181,6 +181,12 @@ extension SSHClient {
181181
return createChannel.futureResult
182182
}.get()
183183

184+
for (key, value) in environment {
185+
try await channel.triggerUserOutboundEvent(SSHChannelRequestEvent.EnvironmentRequest(
186+
wantReply: true, name: key, value: value)
187+
)
188+
}
189+
184190
if inShell {
185191
try await channel.triggerUserOutboundEvent(SSHChannelRequestEvent.ShellRequest(
186192
wantReply: true
@@ -198,7 +204,7 @@ extension SSHClient {
198204
/// 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.
199205
/// - Parameters:
200206
/// - 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 {
202208
var stdoutContinuation: AsyncThrowingStream<ByteBuffer, Error>.Continuation!
203209
var stderrContinuation: AsyncThrowingStream<ByteBuffer, Error>.Continuation!
204210
let stdout = AsyncThrowingStream<ByteBuffer, Error> { continuation in
@@ -216,7 +222,7 @@ extension SSHClient {
216222

217223
Task {
218224
do {
219-
let stream = try await executeCommandStream(command, inShell: inShell)
225+
let stream = try await executeCommandStream(command, inShell: inShell, environment: environment)
220226
for try await chunk in stream {
221227
switch chunk {
222228
case .stdout(let buffer):

0 commit comments

Comments
 (0)