Giter Site home page Giter Site logo

shellprocess's Introduction

ShellProcess

A lightweight wrapper around Foundation's Process.

Install

Add the following dependency as a package in your Package.swift file or Xcode project:

[email protected]:rlziii/ShellProcess.git

Usage

The library is made of of three simple parts:

  • Action: An action to be fed into a ShellProcess to run a command. The command and arguments are part of this struct.
  • Result: The result of running a ShellProcess command that represents the standard output and standard error Strings, if any exists.
  • ShellProcess: A struct that wraps a single Process to be executed. After being initialized, use the run(_:withDirectoryURL:) or run(withInput:) methods to execute the command.

Action

Actions can be created by either making new extensions and providing an executable path (to a Unix command) along with optional arguments or by using the convenience raw(_:) Action that accepts a String to be used as a raw Shell script. For example, this is an example (provided in the project) to wrap the who command:

public static func who(currentTerminalOnly: Bool) -> ShellProcess.Action {
  .init(executablePath: "/usr/bin/who", arguments: currentTerminalOnly ? ["-m"] : [])
}

And can be used like so:

ShellAction(.who(currentTerminalOnly: true)).run()

If a command doesn't need to accept arguments then it can be written as a static property instead of a static method:

public static let whoAmI = ShellProcess.Action(executablePath: "/usr/bin/who", arguments: ["-m"])

And used as such:

ShellAction(.whoAmI).run()

Alternatively, this could have been written using raw:

ShellAction(.raw("who -m")).run()

When using raw(_:), you craft commands as though you are typing in a Terminal window.

Result

The Result type (not to be confused with Swift's built-in Result) is a simple wrapper struct around two Optional Strings: standardOutput and standardError. After a ShellAction has finished executing (via run(...)), a Result is returned with these values potentially available, representing the stream of text that was written to the Shell's standard output and error pipes. These return values are marked as @discardableResult so they can be conveniently ignored if they are not needed.

ShellProcess

A ShellProcess is initialized with an Action and optionally a directoryURL (i.e. the directory from which to execute within). There are two run(...) methods:

public func run(withInput input: String) -> Result
public func run(withInput input: Data? = nil) -> Result

The former is just a convenience method for the latter when a String is the desired input (which is true for most cases). There are also async versions of both methods.

If the result of one ShellProcess is required for a subsequent command, they can be chained together in a simple way (especially using Swift's async/await). For example:

func someFunction() async {
  let result1 = await ShellProcess(.someAction1).run()
  let result2 = await ShellProcess(.someAction2(with: result1.standardOutput)).run()
  let result3 = await ShellProcess(.someAction3(with: result2.standardOutput)).run()
  print(result3.standardOutput)
}

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.