Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to use pipes #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions Sources/ShellOut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import Dispatch
* - parameter command: The command to run
* - parameter arguments: The arguments to pass to the command
* - parameter path: The path to execute the commands at (defaults to current folder)
* - parameter inputHandle: Any `FileHandle` that any input (STDIN) should be redirected to
* (at the moment this is only supported on macOS)
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
* (at the moment this is only supported on macOS)
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
Expand All @@ -29,18 +31,21 @@ import Dispatch
@discardableResult public func shellOut(to command: String,
arguments: [String] = [],
at path: String = ".",
inputHandle: FileHandle? = nil,
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil) throws -> String {
let process = Process()
let command = "cd \(path.escapingSpaces) && \(command) \(arguments.joined(separator: " "))"
return try process.launchBash(with: command, outputHandle: outputHandle, errorHandle: errorHandle)
return try process.launchBash(with: command, inputHandle: inputHandle, outputHandle: outputHandle, errorHandle: errorHandle)
}

/**
* Run a series of shell commands using Bash
*
* - parameter commands: The commands to run
* - parameter path: The path to execute the commands at (defaults to current folder)
* - parameter inputHandle: Any `FileHandle` that any input (STDIN) should be redirected to
* (at the moment this is only supported on macOS)
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
* (at the moment this is only supported on macOS)
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
Expand All @@ -54,17 +59,19 @@ import Dispatch
*/
@discardableResult public func shellOut(to commands: [String],
at path: String = ".",
inputHandle: FileHandle? = nil,
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil) throws -> String {
let command = commands.joined(separator: " && ")
return try shellOut(to: command, at: path, outputHandle: outputHandle, errorHandle: errorHandle)
return try shellOut(to: command, at: path, inputHandle: inputHandle, outputHandle: outputHandle, errorHandle: errorHandle)
}

/**
* Run a pre-defined shell command using Bash
*
* - parameter command: The command to run
* - parameter path: The path to execute the commands at (defaults to current folder)
* - parameter inputHandle: Any `FileHandle` that any input (STDIN) should be redirected to
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
*
Expand All @@ -78,9 +85,10 @@ import Dispatch
*/
@discardableResult public func shellOut(to command: ShellOutCommand,
at path: String = ".",
inputHandle: FileHandle? = nil,
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil) throws -> String {
return try shellOut(to: command.string, at: path, outputHandle: outputHandle, errorHandle: errorHandle)
return try shellOut(to: command.string, at: path, inputHandle: inputHandle, outputHandle: outputHandle, errorHandle: errorHandle)
}

/// Structure used to pre-define commands for use with ShellOut
Expand Down Expand Up @@ -346,7 +354,7 @@ extension ShellOutError: LocalizedError {
// MARK: - Private

private extension Process {
@discardableResult func launchBash(with command: String, outputHandle: FileHandle? = nil, errorHandle: FileHandle? = nil) throws -> String {
@discardableResult func launchBash(with command: String, inputHandle: FileHandle? = nil, outputHandle: FileHandle? = nil, errorHandle: FileHandle? = nil) throws -> String {
launchPath = "/bin/bash"
arguments = ["-c", command]

Expand All @@ -359,12 +367,15 @@ private extension Process {
var outputData = Data()
var errorData = Data()

standardInput = inputHandle

let outputPipe = Pipe()
standardOutput = outputPipe

let errorPipe = Pipe()
standardError = errorPipe


#if !os(Linux)
outputPipe.fileHandleForReading.readabilityHandler = { handler in
outputQueue.async {
Expand Down
11 changes: 11 additions & 0 deletions Tests/ShellOutTests/ShellOutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ class ShellOutTests: XCTestCase {
XCTAssertTrue(uptime.contains("load average"))
}

func testWithInputData() throws {
let pipe = Pipe()

// echo "Hello world"
let _ = try shellOut(to: "echo", arguments: ["Hello world"], outputHandle: pipe.fileHandleForWriting)

// simulate `echo "Hello world" | cat`
let cat = try shellOut(to: "cat", inputHandle: pipe.fileHandleForReading)
XCTAssertEqual(cat, "Hello world")
}

func testWithArguments() throws {
let echo = try shellOut(to: "echo", arguments: ["Hello world"])
XCTAssertEqual(echo, "Hello world")
Expand Down