Skip to content

Instantly share code, notes, and snippets.

@honood
Forked from andreacipriani/Bash.swift
Created August 19, 2021 13:24
Show Gist options
  • Select an option

  • Save honood/bb14bba0884799a1a8af87d9faa5da06 to your computer and use it in GitHub Desktop.

Select an option

Save honood/bb14bba0884799a1a8af87d9faa5da06 to your computer and use it in GitHub Desktop.

Revisions

  1. @andreacipriani andreacipriani revised this gist Oct 9, 2020. 1 changed file with 20 additions and 13 deletions.
    33 changes: 20 additions & 13 deletions Bash.swift
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,39 @@
    import UIKit

    protocol CommandExecuting {
    func run(commandName: String, arguments: [String]) -> String?
    func run(commandName: String, arguments: [String]) throws -> String
    }

    struct Bash: CommandExecuting {
    enum BashError: Error {
    case commandNotFound(name: String)
    }

    // MARK: - CommandExecuting
    struct Bash: CommandExecuting {
    func run(commandName: String, arguments: [String] = []) throws -> String {
    return try run(resolve(commandName), with: arguments)
    }

    func run(commandName: String, arguments: [String] = []) -> String? {
    guard var bashCommand = run(command: "/bin/bash" , arguments: ["-l", "-c", "which \(commandName)"]) else { return "\(commandName) not found" }
    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 run(command: bashCommand, arguments: arguments)
    return bashCommand
    }

    // MARK: Private

    private func run(command: String, arguments: [String] = []) -> String? {
    private func run(_ command: String, with arguments: [String] = []) throws -> String {
    let process = Process()
    process.executableURL = URL(fileURLWithPath: command)
    process.launchPath = command
    process.arguments = arguments
    let outputPipe = Pipe()
    process.standardOutput = outputPipe
    try process.run()
    process.launch()
    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", arguments: []) { print(lsOutput) }
    if let lsWithArgumentsOutput = bash.run(commandName: "ls", arguments: ["-la"]) { print(lsWithArgumentsOutput) }
    if let lsOutput = try? bash.run(commandName: "ls", arguments: []) { print(lsOutput) }
    if let lsWithArgumentsOutput = try? bash.run(commandName: "ls", arguments: ["-la"]) { print(lsWithArgumentsOutput) }
  2. @andreacipriani andreacipriani revised this gist May 4, 2020. 1 changed file with 6 additions and 9 deletions.
    15 changes: 6 additions & 9 deletions Bash.swift
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,19 @@
    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)
    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") { print(lsOutput) }
    if let lsOutput = bash.run(commandName: "ls", arguments: []) { print(lsOutput) }
    if let lsWithArgumentsOutput = bash.run(commandName: "ls", arguments: ["-la"]) { print(lsWithArgumentsOutput) }

  3. @andreacipriani andreacipriani revised this gist May 4, 2020. 1 changed file with 13 additions and 20 deletions.
    33 changes: 13 additions & 20 deletions Bash.swift
    Original file line number Diff line number Diff line change
    @@ -1,42 +1,35 @@
    import Foundation

    protocol CommandExecuting {
    func execute(commandName: String) -> String?
    func execute(commandName: String, arguments: [String]) -> String?
    func run(commandName: String, arguments: [String]) -> String?
    }

    final class Bash: CommandExecuting {
    struct Bash: CommandExecuting {

    // MARK: - CommandExecuting

    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" }
    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 execute(command: String, arguments: [String] = []) -> String? {
    private func run(command: String, arguments: [String] = []) -> String? {
    let process = Process()
    process.launchPath = command
    process.executableURL = URL(fileURLWithPath: 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)
    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.execute(commandName: "ls") { print(lsOutput) }
    if let lsWithArgumentsOutput = bash.execute(commandName: "ls", arguments: ["-la"]) { print(lsWithArgumentsOutput) }
    if let lsOutput = bash.run(commandName: "ls") { print(lsOutput) }
    if let lsWithArgumentsOutput = bash.run(commandName: "ls", arguments: ["-la"]) { print(lsWithArgumentsOutput) }

  4. Andrea Cipriani renamed this gist Dec 11, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. Andrea Cipriani revised this gist Dec 11, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions Shell.swift
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    import Foundation

    protocol CommandsExecuting {
    protocol CommandExecuting {
    func execute(commandName: String) -> String?
    func execute(commandName: String, arguments: [String]) -> String?
    }

    final class Bash: CommandsExecuting {
    final class Bash: CommandExecuting {

    // MARK: - ShellCommandsExecuting
    // MARK: - CommandExecuting

    func execute(commandName: String) -> String? {
    return execute(commandName: commandName, arguments: [])
    @@ -36,7 +36,7 @@ final class Bash: CommandsExecuting {
    }
    }

    let bash: CommandsExecuting = Bash()
    let bash: CommandExecuting = Bash()
    if let lsOutput = bash.execute(commandName: "ls") { print(lsOutput) }
    if let lsWithArgumentsOutput = bash.execute(commandName: "ls", arguments: ["-la"]) { print(lsWithArgumentsOutput) }

  6. Andrea Cipriani revised this gist Dec 11, 2018. 1 changed file with 29 additions and 21 deletions.
    50 changes: 29 additions & 21 deletions Shell.swift
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,42 @@
    import Foundation

    final class Shell
    {
    func outputOf(commandName: String, arguments: [String] = []) -> String? {
    return bash(commandName: commandName, arguments:arguments)
    }
    protocol CommandsExecuting {
    func execute(commandName: String) -> String?
    func execute(commandName: String, arguments: [String]) -> String?
    }

    final class Bash: CommandsExecuting {

    // MARK: private
    // MARK: - ShellCommandsExecuting

    func execute(commandName: String) -> String? {
    return execute(commandName: commandName, arguments: [])
    }

    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)
    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)
    }

    private func executeShell(command: String, arguments: [String] = []) -> String? {
    let task = Task()
    task.launchPath = command
    task.arguments = arguments
    // MARK: Private

    private func execute(command: String, arguments: [String] = []) -> String? {
    let process = Process()
    process.launchPath = command
    process.arguments = arguments

    let pipe = Pipe()
    task.standardOutput = pipe
    task.launch()
    process.standardOutput = pipe
    process.launch()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output: String? = String(data: data, encoding: String.Encoding.utf8)

    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) }

  7. Andrea Cipriani created this gist Aug 5, 2016.
    34 changes: 34 additions & 0 deletions Shell.swift
    Original 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
    }

    }