@ -95,6 +95,7 @@ extension Tailmac {
extension Tailmac {
struct Run : ParsableCommand {
@ Option ( help : " The vm identifier " ) var id : String
@ Option ( help : " Optional share directory " ) var share : String ?
@ Flag ( help : " Tail the TailMac log output instead of returning immediatly " ) var tail
mutating func run ( ) {
@ -115,7 +116,12 @@ extension Tailmac {
fatalError ( " Could not find Host.app at \( appPath ) . This must be co-located with the tailmac utility " )
}
process . arguments = [ " run " , " --id " , id ]
var args = [ " run " , " --id " , id ]
if let share {
args . append ( " --share " )
args . append ( share )
}
process . arguments = args
do {
process . standardOutput = stdOutPipe
@ -124,26 +130,18 @@ extension Tailmac {
fatalError ( " Unable to launch the vm process " )
}
// T h i s d o e s n ' t p r i n t u n t i l w e e x i t w h i c h i s n o t i d e a l , b u t a t l e a s t w e
// g e t t h e o u t p u t
if tail != 0 {
// ( j o n a t h a n ) TODO: H o w d o w e g e t t h e p r o c e s s o u t p u t i n r e a l t i m e ?
// T h e c h i l d p r o c e s s o n l y s e e m s t o f l u s h t o s t d o u t o n c o m p l e t i o n
let outHandle = stdOutPipe . fileHandleForReading
let queue = OperationQueue ( )
NotificationCenter . default . addObserver (
forName : NSNotification . Name . NSFileHandleDataAvailable ,
object : outHandle , queue : queue )
{
notification -> Void in
let data = outHandle . availableData
outHandle . readabilityHandler = { handle in
let data = handle . availableData
if data . count > 0 {
if let str = String ( data : data , encoding : String . Encoding . utf8 ) {
print ( str )
}
}
outHandle . waitForDataInBackgroundAndNotify ( )
}
outHandle . waitForDataInBackgroundAndNotify ( )
process . waitUntilExit ( )
}
}