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

Last update: Jul 30, 2022

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.1.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.

GitHub

https://github.com/azone/ShellExecutor
You might also like...

iOS command-line tool that allows searching and downloading ipa files from the iOS App Store

ipatool for iOS This is a port of Majd Alfhaily's ipatool adapted to run on iOS Build / Installation To build this, make sure you have AppSync install

Jun 16, 2022

The best command-line tool to install and switch between multiple versions of Xcode.

The best command-line tool to install and switch between multiple versions of Xcode.

The best command-line tool to install and switch between multiple versions of Xcode.

Aug 6, 2022

A way to build TUI apps with a layout system and API that's similar to SwiftUI.

terminal-ui A way to build TUI apps with a layout system and API that's similar to SwiftUI. We reimplemented parts of the SwiftUI layout system in the

Jul 12, 2022

A powerful command line tool for performing stoichiometry calculations on checmicals and chemical equations.

Stoichiometry Stoichiometry is a powerful command line tool for preforming stoichiometry chemicals and chemical equations. Its subcommands are listed

Jul 15, 2022

An extremely simple CLI tool that was created to diagnose and further understand an issue in DriverKit causing kIOHIDOptionsTypeSeizeDevice to behave incorrectly when used in DriverKit system extensions.

IOKitHIDKeyboardTester This tool is NOT useful to, or intended for general users. IOKitHIDKeyboardTester is an extremely simple (one-file!) CLI tool t

Jul 22, 2022

A library and CLI Utility to manage NVRAM Stuff, written in Swift.

NVRAMKit A Library and CLI Utility to manage NVRAM Stuff, written in Swift. Library Adding Library to Project Simply add this line to the dependencies

Jun 5, 2022

A command line tool to parse pricing from a pdf and generate an updated csv file for House Call Pro

A command line tool to parse pricing from a pdf and generate an updated csv file for House Call Pro

Feb 17, 2022

A command line profiling tool with stopwatch, cpu and memory usage

A command line profiling tool with stopwatch, cpu and memory usage

timeui A command line profiling tool with stopwatch, cpu and memory usage. Usage ./timeui path/to/app-to-profile runs the stopwatch and signpost regio

Jun 25, 2022

📋A hand-curated collection of useful and informative Swift Scripting materials.

📋A hand-curated collection of useful and informative Swift Scripting materials.

Articles • Videos Articles Creating iOS Application Icons with SwiftUI by Eneko Alonso An interesting way to use SwiftUI with CLI for icon generation.

Jul 19, 2022
Shell scripting in Swift

Shwift Shell-scripting in Swift DISCLAIMER: Shwift depends on Swift's incoming concurrency features. As such, it requires a recent Swift toolchain, an

Jun 20, 2022
This app is a sample app that recognizes specific voice commands such as "make it red", "make it blue", "make it green", and "make it black" and change the background color of the view in the frame.
This app is a sample app that recognizes specific voice commands such as

VoiceOperationSample This app is a sample app that recognizes specific voice commands such as "make it red", "make it blue", "make it green", and "mak

Dec 3, 2021
Swift utilities for running commands.

Swift Commands Swift utilities for running commands. The Commands module allows you to take a system command as a string and return the standard outpu

Jun 3, 2022
Command-line commands for Chaqmoq applications

Chaqmoq CLI Installation Swift Download and install Swift Swift Package mkdir MyApp cd MyApp swift package init --type executable // Creates an execut

Dec 25, 2021
Terminal string styling for Swift.
Terminal string styling for Swift.

Terminal string styling for Swift. Integration Swift Package Manager (SPM) You can use The Swift Package Manager to install ColorizeSwift by adding it

Jul 23, 2022
✏️Expressive styling on terminal string. (chalk for swift)
✏️Expressive styling on terminal string. (chalk for swift)

Chalk Expressive styling on terminal string. Highlights Expressive API 256/TrueColor support Nest styles Auto downgrading to terminal supported color

Jun 10, 2022
Overlook - A commandline app that will watch your folder and monitor any changes
Overlook - A commandline app that will watch your folder and monitor any changes

A commandline app that will watch your folder and monitor any changes. When a change occurs, Overlook will execute (or restart) a command you specify. Overlook is platform independent and will work with anything from writing a README file, to developing a service.

Dec 6, 2021
A command line tool to easily install and browse Xcode templates

?? XTrail A command line tool to easily install and browse Xcode templates. Usage The general invocation syntax for xtrail is as follows: xtrail <subc

Dec 16, 2021
Guaka - Smart and beautiful POSIX compliant CLI framework for Swift.
Guaka - Smart and beautiful POSIX compliant CLI framework for Swift.

Guaka - Smart and beautiful POSIX compliant CLI framework for Swift. It helps you create modern and familiar CLI apps in the vein of widely used proje

Jul 29, 2022
ipatool is a command line tool that allows you to search for iOS apps on the App Store and download a copy of the app package, known as an ipa file.
ipatool is a command line tool that allows you to search for iOS apps on the App Store and download a copy of the app package, known as an ipa file.

ipatool is a command line tool that allows you to search for iOS apps on the App Store and download a copy of the app package, known as an ipa file.

Aug 6, 2022