summaryrefslogtreecommitdiff
path: root/Coolbal/Process.hs
blob: 8b4576cb0ea3a7498d59efe7dc31df0be1666860 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module Coolbal.Process where

import System.Exit (ExitCode(..))
import System.Process
import System.IO (hGetContents)

import Coolbal.Log
import Coolbal.Options


runCommand :: Flags -> String -> [String] -> IO ExitCode
runCommand flags cmd args = do
    logVerbose flags "cmd" $ "Running command: " ++ showCommandForUser cmd args
    (_, _, _, ph) <- createProcess (proc cmd args)
    waitForProcess ph

readCommand :: Flags -> String -> [String] -> IO (Either ExitCode String)
readCommand flags cmd args = do
    logVerbose flags "cmd" $ "Running command: " ++ showCommandForUser cmd args
    (_, Just handle, _, ph) <- createProcess (proc cmd args) { std_out = CreatePipe }
    output <- hGetContents handle
    code <- waitForProcess ph
    case code of
      ExitSuccess -> return (Right output)
      ExitFailure _ -> return (Left code)