From 280eddf54149d0053c1528863e0cfced286cde71 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Wed, 7 Sep 2016 20:04:52 -0700 Subject: [PATCH] - Removed `backgroundExecuteData` - Added generic `backgroundExecute` - Added overloads for `backgroundExecute` and `foregroundExecute` for executing with an array of commands or variable arguments of commands --- Sources/Console/Console/Console.swift | 28 +++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/Sources/Console/Console/Console.swift b/Sources/Console/Console/Console.swift index 46131883..a901eee8 100644 --- a/Sources/Console/Console/Console.swift +++ b/Sources/Console/Console/Console.swift @@ -1,5 +1,6 @@ import libc import Foundation +import Core /** Protocol for powering styled Console I/O. @@ -72,7 +73,6 @@ public protocol ConsoleProtocol { } extension ConsoleProtocol { - public func foregroundExecute(program: String, arguments: [String]) throws { #if os(Linux) let stdin = FileHandle.standardInput() @@ -92,8 +92,16 @@ extension ConsoleProtocol { error: stderr.fileDescriptor ) } + + public func foregroundExecute(commands: [String]) throws { + try foregroundExecute(program: commands[0], arguments: commands.dropFirst(1).array) + } + + public func foregroundExecute(commands: String...) throws { + try foregroundExecute(commands: commands) + } - public func backgroundExecuteData(program: String, arguments: [String]) throws -> Data { + public func backgroundExecute(program: String, arguments: [String]) throws -> Bytes { let input = Pipe() let output = Pipe() let error = Pipe() @@ -113,12 +121,20 @@ extension ConsoleProtocol { } close(output.fileHandleForWriting.fileDescriptor) - return output.fileHandleForReading.readDataToEndOfFile() + return try output.fileHandleForReading.readDataToEndOfFile().makeBytes() + } + + public func backgroundExecute(program: String, arguments: [String]) throws -> Type { + let bytes = try backgroundExecute(program: program, arguments: arguments) as Bytes + return try Type(bytes: bytes) + } + + public func backgroundExecute(commands: [String]) throws -> Type { + return try backgroundExecute(program: commands[0], arguments: commands.dropFirst(1).array) } - public func backgroundExecute(program: String, arguments: [String]) throws -> String { - let data = try backgroundExecuteData(program: program, arguments: arguments) - return String(data: data, encoding: .utf8) ?? "" + public func backgroundExecute(commands: String...) throws -> Type { + return try backgroundExecute(commands: commands) } }