Skip to content

Utility classes to help with common system/shell actions in Swift

License

Notifications You must be signed in to change notification settings

Kitura/ShellToolKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

20be64f · Jun 2, 2023

History

78 Commits
Oct 4, 2021
Jun 2, 2023
Sep 30, 2022
Oct 3, 2021
Oct 4, 2021
Sep 20, 2022
Sep 20, 2022
Aug 30, 2022

Repository files navigation

ShellToolKit

build status macOS Linux Apache 2

`ShellToolKit` is a set of classes that are typically useful when using Swift as a command-line tool.

Installation

import PackageDescription

let package = Package(
    name: "YourAwesomeSoftware",
    dependencies: [
        .package(url: "https://github.com/Kitura/ShellToolKit.git", from: "0.1.0")
    ],
    targets: [
        .target(
            name: "MyApp",
            dependencies: ["ShellToolKit"]
        )
    ]
)
#!/usr/bin/swift sh

import ShellToolKit          // @Kitura


System Action

Currently the only class supported is SystemAction which allows command-line tools to more easily support variations of verbose/dry-run. It uses the Rainbow library for colorized output and SwiftShell for executing commands.

A typical swift-argument-parser based program may look something like this:

import ArgumentParser   // https://github.com/apple/swift-argument-parser.git

struct BuildCommand: ParsableCommand {
   @Flag(name: .shortAndLong, help: "Enable verbose mode")
   var verbose: Bool = false

   @Flag(name: [.customLong("dry-run"), .customShort("n")], help: "Dry-run (print but do not execute commands)")
   var enableDryRun: Bool = false


   mutating func run() throws {
       let actions: SystemAction
       
       if enableDryRun {
           actions = CompositeAction([SystemActionPrint()])
       } else if verbose {
           actions = CompositeAction([SystemActionPrint(), SystemActionReal()])
       } else {
           actions = CompositeAction([SystemActionReal()])
       }
    
       try actions.runAndPrint(command: "echo", "Hello", "World!")
   }

}
References

Capturing stdin/stdout

Related/interesting projects

About

Utility classes to help with common system/shell actions in Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages