https://github.com/pauljeannot/swiftybash
Bash scripting & piping in Swift made easy !
https://github.com/pauljeannot/swiftybash
bash bash-scripting bashscript easy pipe pipeline pretty swift swift-package-manager swift4
Last synced: 2 months ago
JSON representation
Bash scripting & piping in Swift made easy !
- Host: GitHub
- URL: https://github.com/pauljeannot/swiftybash
- Owner: pauljeannot
- License: mit
- Created: 2017-11-12T19:47:48.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-31T17:34:13.000Z (about 7 years ago)
- Last Synced: 2025-03-19T18:50:33.595Z (3 months ago)
- Topics: bash, bash-scripting, bashscript, easy, pipe, pipeline, pretty, swift, swift-package-manager, swift4
- Language: Swift
- Homepage:
- Size: 42 KB
- Stars: 20
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README


[](https://travis-ci.org/pauljeannot/SwiftyBash)
SwiftyBash is a Swift Bash scripting & piping library, written in Swift.
## 🤖 Usage
### 🚀 Simple bash command
Describe your command through a `BashCmd` object, and run it! It is that simple :
```swift
// Run `ls` in the executable directory
let ls = BashCmd("ls")
let result = try! ls.run()// Run `git status` from ~/MyProject directory
let status = BashCmd("git", args:"status", from:"~/MyProject")
let result = try! status.run()
```### 🤝 Pipe
You can pipe output and input stream easily, as many times as you want !
```swift
let ls = BashCmd("ls", args:"-l", from:"~/")
let grep = BashCmd("grep", args:"root")
let wc = BashCmd("wc", args:"-l")// Use pipe() function
let result = try! ls.pipe(grep).pipe(wc).run()
```#### Custom operator
You can use the `|` to pipe from a command to another 😻
⚠️ You have to declare the operator somewhere in your project to be able to use it !
```swift
// Operator declaration somewhere in your project
infix operator |// Have fun ! How beautiful is it ?
let result = try! (ls | grep | wc).run()
```### 🔥 Error handling
SwiftyBash uses Swift exception to handle error, by throwing a BashException.
```swift
let grep = BashCmd("grep", args:"hosts", "/private/etc/*")
do {
try grep.run()
}
catch {
if let error = error as? BashException {
print(error.stderr) // Prints STDERR
print(error.stdout) // Prints STDOUT
}
}```
### ✏️ Output type
You can chose the output type between a simple `String` or to write the output into a file.
Default is `.string(.raw)`.#### Write into a file
```swift
let cat = BashCmd("cat", args:"file.json")
let grep = BashCmd("grep", args:"'secrets'")// Write the result into `secrets.json`
try! (cat | grep).run(outputType:.file("secrets.json"))
```#### Whitespaces trimming
```swift
let ls = BashCmd("ls", args:"-l")
let wc = BashCmd("wc", args:"-l")let result = try! (ls | wc).run(outputType:.string(.raw)) // result is ` 6`
let result = try! (ls | wc).run(outputType:.string(.whiteSpacesTrimmed)) // result is `6`
```#### Example of output
When taking the output as a string or to a file, new lines (`\n`) are kept. So, here is an example of output :
```swift
let ls = BashCmd("ls", args:"-l")
let result = try! ls.run()
print(result!)
```
*... shows ...*
```bash
total 24
-rw-r--r--+ 1 me staff 331 12 nov 23:33 Package.resolved
-rw-r--r--+ 1 me staff 743 12 nov 23:19 Package.swift
-rw-r--r--+ 1 me staff 50 12 nov 23:02 README.md
drwxr-xr-x+ 3 me staff 102 12 nov 23:02 Sources
drwxr-xr-x+ 2 me staff 68 12 nov 23:02 Tests
```## 🚧 Installation
SwiftyBash is currently only available for **Swift 4**. If you need Swift 3 compatibility, feel free to open an issue or to create a Pull Request 😉
### 📦 Swift Package Manager
Update your Package.swift file by adding this line to your dependencies :
```swift
dependencies: [
[...]
.package(url: "https://github.com/pauljeannot/SwiftyBash.git", from: "1.0.0"),
]
```Do not forget to also add it to your target dependencies :
```swift
targets: [
.target(
name: "YourProject",
dependencies: ["...", "SwiftyBash"]),
]
```## 👤 Contact
If you have any question, new idea or if you find a bug (❌), feel free to open an issue or to contact me by [email]([email protected]).