Skip to content

Commit

Permalink
Merge pull request #17 from NathanFlurry/master
Browse files Browse the repository at this point in the history
Improved `backgroundExecute` and `foregroundExecute`
  • Loading branch information
tanner0101 authored Sep 8, 2016
2 parents 8282b83 + 5b6b711 commit 234904f
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions Sources/Console/Console/Console.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import libc
import Foundation
import Core

/**
Protocol for powering styled Console I/O.
Expand Down Expand Up @@ -72,7 +73,6 @@ public protocol ConsoleProtocol {
}

extension ConsoleProtocol {

public func foregroundExecute(program: String, arguments: [String]) throws {
#if os(Linux)
let stdin = FileHandle.standardInput()
Expand All @@ -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 backgroundExecute(program: String, arguments: [String]) throws -> String {
public func backgroundExecute(program: String, arguments: [String]) throws -> Bytes {
let input = Pipe()
let output = Pipe()
let error = Pipe()
Expand All @@ -113,8 +121,20 @@ extension ConsoleProtocol {
}

close(output.fileHandleForWriting.fileDescriptor)
let data = output.fileHandleForReading.readDataToEndOfFile()
return String(data: data, encoding: .utf8) ?? ""
return try output.fileHandleForReading.readDataToEndOfFile().makeBytes()
}

public func backgroundExecute<Type: BytesInitializable>(program: String, arguments: [String]) throws -> Type {
let bytes = try backgroundExecute(program: program, arguments: arguments) as Bytes
return try Type(bytes: bytes)
}

public func backgroundExecute<Type: BytesInitializable>(commands: [String]) throws -> Type {
return try backgroundExecute(program: commands[0], arguments: commands.dropFirst(1).array)
}

public func backgroundExecute<Type: BytesInitializable>(commands: String...) throws -> Type {
return try backgroundExecute(commands: commands)
}
}

Expand Down

0 comments on commit 234904f

Please sign in to comment.