Created
July 23, 2017 18:08
-
-
Save MoralAlberto/f6c97075ca76082a11373980f8332c94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * SwiftShell | |
| * Copyright (c) 2017 Alberto Moral | |
| * Licensed under the MIT license, see LICENSE file | |
| */ | |
| import Foundation | |
| extension Process { | |
| public func shell(command: String) -> String { | |
| launchPath = "/bin/bash" | |
| arguments = ["-c", command] | |
| let outputPipe = Pipe() | |
| standardOutput = outputPipe | |
| launch() | |
| let data = outputPipe.fileHandleForReading.readDataToEndOfFile() | |
| guard let outputData = String(data: data, encoding: String.Encoding.utf8) else { return "" } | |
| return outputData.characters.reduce("") { (result, value) in | |
| return result + String(value) | |
| } | |
| } | |
| } | |
| public func launch(command: String, arguments: [String]) -> String { | |
| let process = Process() | |
| let command = "\(command) \(arguments.joined(separator: " "))" | |
| return process.shell(command: command) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment