Contents

m-housh/swift-shell-client

A package that allows you to run shell scripts from your

Usage

You can include in your project, by using swift package manager.

import PackageDescription

let package = Package(
  ...
  dependencies: [
    .package(url: "https://github.com/m-housh/swift-shell-client.git", from: "0.1.0"),
    ...
  ],
  targets: [
    .target(
      name: "MyTarget",
      dependencies: [
        .product(name: "ShellClient", package: "swift-shell-client"),
      ]
    ),
    ...
  ]
)

Basic Usage

You access a shell client through the swift-dependencies system.

import ShellClient

func echo() throws {
  @Dependency(\.logger) var logger
  @Dependency(\.shellClient) var shell

  try shell.foreground(["echo", "Foo"])

  // Or run in a background process, and capture the output.

  let output = try shell.background(
    ["echo", "world"]
    trimmingCharactersIn: .whitespacesAndNewlines
  )

  logger.info("Hello \(output)!")

}

func echoAsync() async throws {
  @Dependency(\.logger) var logger
  @Dependency(\.asyncShellClient) var shell

  try await shell.foreground(["echo", "Foo"])

  // Or run in a background process, and capture the output.

  let output = try await shell.background(
    ["echo", "world"],
    trimmingCharactersIn: .whitespacesAndNewlines
  )

  logger.info("Hello \(output)!")

}

try echo()
try await echoAsync()

Logging

We use swift-log along with swift-log-format-and-pipe to create a basic logger that you have access to. You can also use Rainbow for color text output to the terminal.

The built-in logger will just log messages without any label when built in release and will log with the label shell-client when in debug or testing context.

You can create a basic logger with a label by using the following method provided by the library.

import Rainbow

let logger = basicLogger(.showing(label: "log".red))

logger.info("blob")
// log ▸ blob

Documentation

You can read the full documentation here.

Package Metadata

Repository: m-housh/swift-shell-client

Default branch: main

README: README.md