module Run (runCommand) where import System.Process (runInteractiveCommand,waitForProcess) import Control.Concurrent (forkIO) import Control.Concurrent.MVar (newEmptyMVar,takeMVar,putMVar) import Control.Exception (finally) import System.IO (stderr,hGetContents,hPutStr,hPutStrLn) import System.Exit (ExitCode(..)) runCommand :: String -> String -> Bool -> IO (String,Bool) runCommand cmd input silent = do (inp,out,err,pid) <- runInteractiveCommand cmd let get h = do mvar <- newEmptyMVar let put xs = seq (length xs) (putMVar mvar xs) forkIO $ finally (hGetContents h >>= put) (put []) takeMVar mvar if null input then return () else hPutStr inp input output <- get out errmsg <- get err exit <- waitForProcess pid case exit of ExitSuccess -> return (output,True) ExitFailure _ -> do if silent then return () else hPutStrLn stderr errmsg return (errmsg,False)