目录
目录README.md

swift-shell-client

CI

A package that allows you to run shell scripts from your swift code.

Github Repository Documentation

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.

邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号