Git Product home page Git Product logo

shellexecutor's Introduction

ShellExecutor

Util for executing shell commands, and getting the results easily(data, string, and any decodable).

Requirements

  • Xcode 14.0+
  • Swift 5.7+
  • macOS 10.15+

Installation

You can install ShellExecutor via SPM(Swift Package Manager), adding ShellExecutor through Xcode, or as a dependency is as easy as adding it to the dependencies value of your Package.swift:

...
dependencies: [
    .package(url: "https://github.com/azone/ShellExecutor.git", from: "0.2.0")
]
...

Usage

ShellExecutor provides many ways to execute shell commands.

Using array as the command and arguments:

let command: GeneralCommand = ["ipconfig", "getifaddr", "en0"]
do {
    let ip: String = try ShellExecutor.execute(command: command)
    print(ip) // it will be printed like 192.168.1.174
} catch {
    print(error)
}

Execute multiple commands like pipeline

let command1: GeneralCommand = ["echo", "Hello"]
let command2: GeneralCommand = ["cat"]
do {
    let result: String = try ShellExecutor.execute(commands: [command1, command2])
    print(result) // will be print Hello
} catch {
    print(error)
}

Execute command with environment variables

let command = GeneralCommand(["/bin/bash", "-c", "echo $NAME"], environment: ["NAME": "Logan"])
do {
    let result: String = try ShellExecutor.execute(command: command)
    print(result) // will be print Logan
} catch {
    print(error)
}

Execute shell command directly

do {
    let ip: String = ShellExecutor.execute(shell: "ipconfig getifaddr en0") // and you can also specify which shell you want to use
    print(ip) // it will be printed like 192.168.1.174
} catch {
    print(error)
}

Other convenient ways to execute the commands

// decode struct from the command directly

struct Person: Decodable, Equatable {
    let name: String
    let age: Int
}

do {
    let jsonString = """
{ "name": "Logan", "age": 36 }
"""
    let command: GeneralCommand = ["echo", jsonString]
    let decoder = JSONDecoder()
    let person: Person = try ShellExecutor.execute(command: command, decoder: decoder)
    print(person) // Person(name: "Logan", age: 36)
} catch {
    print(error)
}

// execute shell command(s) using @resultBuilder
do {
    let ip: String = try ShellExecutor.execute {
        "ipconfig"
        "getifaddr"
        "en0"
    }
    print(ip) // 192.168.x.x
    
    let hello: String = try ShellExecutor.execute {
        ["echo", "Hello"]
        ["cat"]
    }
    print(hello) // Hello
} catch {
    print(error)
}

For more information & examples please see the tests.

LICENSE


ShellExecutor is released under the MIT license. See LICENSE for details.

shellexecutor's People

Contributors

azone avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.