From 3c6080debaa19dbd08e924d65e7e769076c40aac Mon Sep 17 00:00:00 2001 From: James Crayne Date: Tue, 26 Apr 2016 17:07:58 -0400 Subject: readPipe util --- lib/ProcessUtils.hs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/ProcessUtils.hs b/lib/ProcessUtils.hs index 4e3ac38..49483b5 100644 --- a/lib/ProcessUtils.hs +++ b/lib/ProcessUtils.hs @@ -42,4 +42,41 @@ systemEnv vars cmd = do _ <- installHandler sigQUIT old_quit Nothing return r - +-- | readPipe +-- +-- This is like System.Process.readProcess but instead of a single +-- command with arguments, you pass it a list which it then executes +-- piping the the output from the prior command into the input for +-- the next command. Like readProcess the next parameter is a string +-- which is sent as input into the initial standard in. +-- +-- > readPipe [("grep",["HostConfig"]),("wc",["-l"]) =<< readFile "input" +-- +-- is equivalent to shell code: +-- +-- > grep HostConfig input | wc -l +-- +readPipe :: [(FilePath,[String])] -> String -> IO String +readPipe [(cmd,args)] input = readProcess cmd args input +readPipe ((cmd,args):xs) stdin0 = do + let p = (shell cmd) { std_in = CreatePipe + , std_out = CreatePipe + , std_err = CreatePipe + , cmdspec = RawCommand cmd args + } + (Just sinh,Just sout,Just serr, ph) <- createProcess p + hPutStr sinh stdin0 + hClose sinh + readPipe0 xs sout + where + readPipe0 :: [(FilePath,[String])] -> Handle -> IO String + readPipe0 ((cmd,args):xs) stdin0 = do + let p = (shell cmd) + { std_in = UseHandle stdin0 + , std_out = CreatePipe + , std_err = CreatePipe + , cmdspec = RawCommand cmd args + } + (Nothing,Just sout,Just serr, ph) <- createProcess p + readPipe0 xs sout + readPipe0 [] h = hGetContents h -- cgit v1.2.3 From e93cec660f146819689dd1ce3370ea37c9282c19 Mon Sep 17 00:00:00 2001 From: James Crayne Date: Tue, 26 Apr 2016 17:10:11 -0400 Subject: fix previous commit: import System.IO --- lib/ProcessUtils.hs | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/ProcessUtils.hs b/lib/ProcessUtils.hs index 49483b5..d4f0f98 100644 --- a/lib/ProcessUtils.hs +++ b/lib/ProcessUtils.hs @@ -11,6 +11,7 @@ import System.Environment import Data.Maybe ( isNothing ) import System.IO.Error ( mkIOError, ioeSetErrorString ) import System.Exit ( ExitCode(..) ) +import System.IO -- | systemEnv -- cgit v1.2.3