-
-
Save honood/bb14bba0884799a1a8af87d9faa5da06 to your computer and use it in GitHub Desktop.
Revisions
-
andreacipriani revised this gist
Oct 9, 2020 . 1 changed file with 20 additions and 13 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,32 +1,39 @@ import UIKit protocol CommandExecuting { func run(commandName: String, arguments: [String]) throws -> String } enum BashError: Error { case commandNotFound(name: String) } struct Bash: CommandExecuting { func run(commandName: String, arguments: [String] = []) throws -> String { return try run(resolve(commandName), with: arguments) } private func resolve(_ command: String) throws -> String { guard var bashCommand = try? run("/bin/bash" , with: ["-l", "-c", "which \(command)"]) else { throw BashError.commandNotFound(name: command) } bashCommand = bashCommand.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) return bashCommand } private func run(_ command: String, with arguments: [String] = []) throws -> String { let process = Process() process.launchPath = command process.arguments = arguments let outputPipe = Pipe() process.standardOutput = outputPipe process.launch() let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile() let output = String(decoding: outputData, as: UTF8.self) return output } } let bash: CommandExecuting = Bash() if let lsOutput = try? bash.run(commandName: "ls", arguments: []) { print(lsOutput) } if let lsWithArgumentsOutput = try? bash.run(commandName: "ls", arguments: ["-la"]) { print(lsWithArgumentsOutput) } -
andreacipriani revised this gist
May 4, 2020 . 1 changed file with 6 additions and 9 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,21 +1,19 @@ protocol CommandExecuting { func run(commandName: String, arguments: [String]) -> String? } struct Bash: CommandExecuting { // MARK: - CommandExecuting func run(commandName: String, arguments: [String] = []) -> String? { guard var bashCommand = run(command: "/bin/bash" , arguments: ["-l", "-c", "which \(commandName)"]) else { return "\(commandName) not found" } bashCommand = bashCommand.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) return run(command: bashCommand, arguments: arguments) } // MARK: Private private func run(command: String, arguments: [String] = []) -> String? { let process = Process() process.executableURL = URL(fileURLWithPath: command) @@ -30,6 +28,5 @@ struct Bash: CommandExecuting { } let bash: CommandExecuting = Bash() if let lsOutput = bash.run(commandName: "ls", arguments: []) { print(lsOutput) } if let lsWithArgumentsOutput = bash.run(commandName: "ls", arguments: ["-la"]) { print(lsWithArgumentsOutput) } -
andreacipriani revised this gist
May 4, 2020 . 1 changed file with 13 additions and 20 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,42 +1,35 @@ import Foundation protocol CommandExecuting { func run(commandName: String, arguments: [String]) -> String? } struct Bash: CommandExecuting { // MARK: - CommandExecuting func run(commandName: String, arguments: [String] = []) -> String? { guard var bashCommand = run(command: "/bin/bash" , arguments: ["-l", "-c", "which \(commandName)"]) else { return "\(commandName) not found" } bashCommand = bashCommand.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) return execute(command: bashCommand, arguments: arguments) } // MARK: Private private func run(command: String, arguments: [String] = []) -> String? { let process = Process() process.executableURL = URL(fileURLWithPath: command) process.arguments = arguments let outputPipe = Pipe() process.standardOutput = outputPipe try process.run() let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile() let output = String(decoding: outputData, as: UTF8.self) return output } } let bash: CommandExecuting = Bash() if let lsOutput = bash.run(commandName: "ls") { print(lsOutput) } if let lsWithArgumentsOutput = bash.run(commandName: "ls", arguments: ["-la"]) { print(lsWithArgumentsOutput) } -
Andrea Cipriani renamed this gist
Dec 11, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Andrea Cipriani revised this gist
Dec 11, 2018 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,13 @@ import Foundation protocol CommandExecuting { func execute(commandName: String) -> String? func execute(commandName: String, arguments: [String]) -> String? } final class Bash: CommandExecuting { // MARK: - CommandExecuting func execute(commandName: String) -> String? { return execute(commandName: commandName, arguments: []) @@ -36,7 +36,7 @@ final class Bash: CommandsExecuting { } } let bash: CommandExecuting = Bash() if let lsOutput = bash.execute(commandName: "ls") { print(lsOutput) } if let lsWithArgumentsOutput = bash.execute(commandName: "ls", arguments: ["-la"]) { print(lsWithArgumentsOutput) } -
Andrea Cipriani revised this gist
Dec 11, 2018 . 1 changed file with 29 additions and 21 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,34 +1,42 @@ import Foundation protocol CommandsExecuting { func execute(commandName: String) -> String? func execute(commandName: String, arguments: [String]) -> String? } final class Bash: CommandsExecuting { // MARK: - ShellCommandsExecuting func execute(commandName: String) -> String? { return execute(commandName: commandName, arguments: []) } func execute(commandName: String, arguments: [String]) -> String? { guard var bashCommand = execute(command: "/bin/bash" , arguments: ["-l", "-c", "which \(commandName)"]) else { return "\(commandName) not found" } bashCommand = bashCommand.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) return execute(command: bashCommand, arguments: arguments) } // MARK: Private private func execute(command: String, arguments: [String] = []) -> String? { let process = Process() process.launchPath = command process.arguments = arguments let pipe = Pipe() process.standardOutput = pipe process.launch() let data = pipe.fileHandleForReading.readDataToEndOfFile() let output = String(data: data, encoding: String.Encoding.utf8) return output } } let bash: CommandsExecuting = Bash() if let lsOutput = bash.execute(commandName: "ls") { print(lsOutput) } if let lsWithArgumentsOutput = bash.execute(commandName: "ls", arguments: ["-la"]) { print(lsWithArgumentsOutput) } -
Andrea Cipriani created this gist
Aug 5, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ import Foundation final class Shell { func outputOf(commandName: String, arguments: [String] = []) -> String? { return bash(commandName: commandName, arguments:arguments) } // MARK: private private func bash(commandName: String, arguments: [String]) -> String? { guard var whichPathForCommand = executeShell(command: "/bin/bash" , arguments:[ "-l", "-c", "which \(commandName)" ]) else { return "\(commandName) not found" } whichPathForCommand = whichPathForCommand.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) return executeShell(command: whichPathForCommand, arguments: arguments) } private func executeShell(command: String, arguments: [String] = []) -> String? { let task = Task() task.launchPath = command task.arguments = arguments let pipe = Pipe() task.standardOutput = pipe task.launch() let data = pipe.fileHandleForReading.readDataToEndOfFile() let output: String? = String(data: data, encoding: String.Encoding.utf8) return output } }