mas/MasKit/ExternalCommands/ExternalCommand.swift

74 lines
1.8 KiB
Swift
Raw Normal View History

2019-01-03 06:53:21 +00:00
//
// ExternalCommand.swift
// MasKit
//
// Created by Ben Chatelain on 1/1/19.
// Copyright © 2019 mas-cli. All rights reserved.
//
/// CLI command
public protocol ExternalCommand {
var binaryPath: String { get set }
var process: Process { get }
var stdout: String { get }
var stderr: String { get }
2019-01-03 06:53:21 +00:00
var stdoutPipe: Pipe { get }
var stderrPipe: Pipe { get }
var exitCode: Int? { get }
var succeeded: Bool { get }
var failed: Bool { get }
/// Runs the command.
func run(arguments: String...) throws
2019-01-03 06:53:21 +00:00
}
/// Common implementation
extension ExternalCommand {
public var stdout: String { get {
2019-01-03 06:53:21 +00:00
let data = stdoutPipe.fileHandleForReading.readDataToEndOfFile()
return String(data: data, encoding: .utf8) ?? ""
2019-01-03 06:53:21 +00:00
}}
public var stderr: String { get {
2019-01-03 06:53:21 +00:00
let data = stderrPipe.fileHandleForReading.readDataToEndOfFile()
return String(data: data, encoding: .utf8) ?? ""
2019-01-03 06:53:21 +00:00
}}
public var exitCode: Int? { get {
return Int(process.terminationStatus)
}}
public var succeeded: Bool { get {
return exitCode == 0
}}
public var failed: Bool { get {
return !succeeded
}}
/// Runs the command.
public func run(arguments: String...) throws {
2019-01-03 06:53:21 +00:00
process.standardOutput = stdoutPipe
process.standardError = stderrPipe
process.arguments = arguments
if #available(OSX 10.13, *) {
process.executableURL = URL(fileURLWithPath: binaryPath)
do {
try process.run()
} catch {
printError("Unable to launch command")
//return throw Error()
}
} else {
process.launchPath = binaryPath
process.launch()
}
process.waitUntilExit()
}
}