-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSystem.swift
More file actions
43 lines (30 loc) · 1.2 KB
/
System.swift
File metadata and controls
43 lines (30 loc) · 1.2 KB
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
import Foundation
private extension Pipe {
func stringsToEndOfFile(encoding: String.Encoding = .ascii) -> [String] {
let data = fileHandleForReading.readDataToEndOfFile()
let contents = String(data: data, encoding: encoding) ?? ""
let clearContents = contents.trimmingCharacters(in: .newlines)
return clearContents.components(separatedBy: "\n")
}
}
struct System {
typealias CommandReturn = (output: [String], error: [String], exitCode: Int32)
static func runOnBash(command: String) -> CommandReturn {
return run(command: "/bin/bash", args: "-l", "-c", command)
}
static func run(command: String, args: String...) -> CommandReturn {
let process = Process()
process.launchPath = command
process.arguments = args
let outputPipe = Pipe()
process.standardOutput = outputPipe
let errorPipe = Pipe()
process.standardError = errorPipe
process.launch()
let output = outputPipe.stringsToEndOfFile()
let error = errorPipe.stringsToEndOfFile()
process.waitUntilExit()
let status = process.terminationStatus
return (output, error, status)
}
}