forked from JohnSundell/ShellOut
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ShellOut.swift
142 lines (130 loc) · 5.19 KB
/
ShellOut.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* ShellOut
* Copyright (c) John Sundell 2017
* Licensed under the MIT license. See LICENSE file.
*/
import Foundation
import Dispatch
import Logging
import TSCBasic
import Algorithms
// MARK: - API
/**
* Run a shell command
*
* - parameter command: The command to run
* - parameter arguments: The arguments to pass to the command
* - parameter path: The path to execute the commands at (defaults to current folder)
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
* (at the moment this is only supported on macOS)
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
* (at the moment this is only supported on macOS)
* - parameter environment: The environment for the command.
*
* - returns: The output of running the command
* - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error
*
* Use this function to "shell out" in a Swift script or command line tool
* For example: `shellOut(to: "mkdir", arguments: ["NewFolder"], at: "~/CurrentFolder")`
*/
@discardableResult public func shellOut(
to command: String,
arguments: [String] = [],
at path: String = ".",
logger: Logger? = nil,
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil,
environment: [String : String]? = nil
) async throws -> (stdout: String, stderr: String) {
try await TSCBasic.Process.launch(
command: command,
arguments: arguments,
logger: logger,
outputHandle: outputHandle,
errorHandle: errorHandle,
environment: environment,
at: path == "." ? nil :
(path == "~" ? TSCBasic.localFileSystem.homeDirectory.pathString :
(path.starts(with: "~/") ? "\(TSCBasic.localFileSystem.homeDirectory.pathString)/\(path.dropFirst(2))" :
path))
)
}
/**
* Run a pre-defined shell command
*
* - parameter command: The command to run
* - parameter path: The path to execute the commands at (defaults to current folder)
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
* - parameter environment: The environment for the command.
*
* - returns: The output of running the command
* - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error
*
* Use this function to "shell out" in a Swift script or command line tool
* For example: `shellOut(to: .gitCommit(message: "Commit"), at: "~/CurrentFolder")`
*
* See `ShellOutCommand` for more info.
*/
@discardableResult public func shellOut(
to command: ShellOutCommand,
at path: String = ".",
logger: Logger? = nil,
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil,
environment: [String : String]? = nil
) async throws -> (stdout: String, stderr: String) {
try await shellOut(
to: command.command,
arguments: command.arguments,
at: path,
logger: logger,
outputHandle: outputHandle,
errorHandle: errorHandle,
environment: environment
)
}
// MARK: - Private
private extension TSCBasic.Process {
@discardableResult static func launch(
command: String,
arguments: [String],
logger: Logger? = nil,
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil,
environment: [String : String]? = nil,
at: String? = nil
) async throws -> (stdout: String, stderr: String) {
let actualCwd = try at.map { try .init(validating: $0) } ?? TSCBasic.localFileSystem.currentWorkingDirectory ?? .root
let process = Self.init(
arguments: [command] + arguments,
environment: environment ?? ProcessEnv.vars,
workingDirectory: actualCwd,
outputRedirection: .collect(redirectStderr: false),
startNewProcessGroup: false,
loggingHandler: logger.map { logger in { logger.debug("\($0)", metadata: ["cwd": .string(actualCwd.pathString)]) } }
)
try process.launch()
let result = try await process.waitUntilExit()
try outputHandle?.write(contentsOf: (try? result.output.get()) ?? [])
try outputHandle?.close()
try errorHandle?.write(contentsOf: (try? result.stderrOutput.get()) ?? [])
try errorHandle?.close()
guard case .terminated(code: let code) = result.exitStatus, code == 0 else {
let code: Int32
switch result.exitStatus {
case .terminated(code: let termCode): code = termCode
case .signalled(signal: let sigNo): code = -sigNo
}
throw ShellOutError(
terminationStatus: code,
errorData: Data((try? result.stderrOutput.get()) ?? []),
outputData: Data((try? result.output.get()) ?? [])
)
}
return try (
stdout: String(result.utf8Output().trimmingSuffix(while: \.isNewline)),
stderr: String(result.utf8stderrOutput().trimmingSuffix(while: \.isNewline))
)
}
}