Skip to content

Commit b62dc4d

Browse files
Merge pull request #1 from daveverwer/support-env-modification
Add environment parameter
2 parents 4488089 + bae7e21 commit b62dc4d

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

Sources/ShellOut.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Dispatch
2020
* (at the moment this is only supported on macOS)
2121
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
2222
* (at the moment this is only supported on macOS)
23+
* - parameter environment: The environment for the command.
2324
*
2425
* - returns: The output of running the command
2526
* - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error
@@ -33,14 +34,16 @@ import Dispatch
3334
at path: String = ".",
3435
process: Process = .init(),
3536
outputHandle: FileHandle? = nil,
36-
errorHandle: FileHandle? = nil
37+
errorHandle: FileHandle? = nil,
38+
environment: [String : String]? = nil
3739
) throws -> String {
3840
let command = "cd \(path.escapingSpaces) && \(command) \(arguments.joined(separator: " "))"
3941

4042
return try process.launchBash(
4143
with: command,
4244
outputHandle: outputHandle,
43-
errorHandle: errorHandle
45+
errorHandle: errorHandle,
46+
environment: environment
4447
)
4548
}
4649

@@ -54,6 +57,7 @@ import Dispatch
5457
* (at the moment this is only supported on macOS)
5558
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
5659
* (at the moment this is only supported on macOS)
60+
* - parameter environment: The environment for the command.
5761
*
5862
* - returns: The output of running the command
5963
* - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error
@@ -66,7 +70,8 @@ import Dispatch
6670
at path: String = ".",
6771
process: Process = .init(),
6872
outputHandle: FileHandle? = nil,
69-
errorHandle: FileHandle? = nil
73+
errorHandle: FileHandle? = nil,
74+
environment: [String : String]? = nil
7075
) throws -> String {
7176
let command = commands.joined(separator: " && ")
7277

@@ -75,7 +80,8 @@ import Dispatch
7580
at: path,
7681
process: process,
7782
outputHandle: outputHandle,
78-
errorHandle: errorHandle
83+
errorHandle: errorHandle,
84+
environment: environment
7985
)
8086
}
8187

@@ -87,6 +93,7 @@ import Dispatch
8793
* - parameter process: Which process to use to perform the command (default: A new one)
8894
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
8995
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
96+
* - parameter environment: The environment for the command.
9097
*
9198
* - returns: The output of running the command
9299
* - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error
@@ -101,14 +108,16 @@ import Dispatch
101108
at path: String = ".",
102109
process: Process = .init(),
103110
outputHandle: FileHandle? = nil,
104-
errorHandle: FileHandle? = nil
111+
errorHandle: FileHandle? = nil,
112+
environment: [String : String]? = nil
105113
) throws -> String {
106114
return try shellOut(
107115
to: command.string,
108116
at: path,
109117
process: process,
110118
outputHandle: outputHandle,
111-
errorHandle: errorHandle
119+
errorHandle: errorHandle,
120+
environment: environment
112121
)
113122
}
114123

@@ -397,10 +406,14 @@ extension ShellOutError: LocalizedError {
397406
// MARK: - Private
398407

399408
private extension Process {
400-
@discardableResult func launchBash(with command: String, outputHandle: FileHandle? = nil, errorHandle: FileHandle? = nil) throws -> String {
409+
@discardableResult func launchBash(with command: String, outputHandle: FileHandle? = nil, errorHandle: FileHandle? = nil, environment: [String : String]? = nil) throws -> String {
401410
launchPath = "/bin/bash"
402411
arguments = ["-c", command]
403412

413+
if let environment = environment {
414+
self.environment = environment
415+
}
416+
404417
// Because FileHandle's readabilityHandler might be called from a
405418
// different queue from the calling queue, avoid a data race by
406419
// protecting reads and writes to outputData and errorData on

0 commit comments

Comments
 (0)