@@ -20,7 +20,6 @@ module Obelisk.CliApp.Process
2020 ) where
2121
2222import Control.Monad ((<=<), join, void)
23−import Control.Monad.Catch (Exception(..), MonadMask, throwM)
2423import Control.Monad.Except (throwError)
2524import Control.Monad.IO.Class (MonadIO, liftIO)
2625import qualified Data.ByteString as BS
@@ -42,17 +41,10 @@ import qualified System.Process as Process
4241
4342import Control.Monad.Log (Severity (..))
4443import Obelisk.CliApp.Logging (putLog, putLogRaw)
45−import Obelisk.CliApp.Types (Cli)
46−
47−data ProcessFailed = ProcessFailed Process.CmdSpec Int -- exit code
48− deriving Show
49−
50−instance Exception ProcessFailed where
51− displayException (ProcessFailed spec exitCode) =
52− unwords ["ProcessFailed", reconstructCommand spec, show exitCode]
44+import Obelisk.CliApp.Types (Cli, ProcessFailed (..), reconstructCommand)
5345
5446readProcessAndLogStderr
55− :: (MonadIO m, MonadMask m, Cli m)
47+ :: (MonadIO m, Cli m)
5648 => Severity -> CreateProcess -> m Text
5749readProcessAndLogStderr sev process = do
5850 (out, _err) <- withProcess process $ \_out err -> do
@@ -68,7 +60,7 @@ readProcessAndLogStderr sev process = do
6860-- `callProcessAndLogOutput (Debug, Debug)`.
6961
7062readProcessAndLogOutput
71− :: (MonadIO m, MonadMask m, Cli m)
63+ :: (MonadIO m, Cli m)
7264 => (Severity, Severity) -> CreateProcess -> m Text
7365readProcessAndLogOutput (sev_out, sev_err) process = do
7466 (_, Just out, Just err, p) <- createProcess $ process
@@ -81,7 +73,7 @@ readProcessAndLogOutput (sev_out, sev_err) process = do
8173
8274 liftIO (waitForProcess p) >>= \case
8375 ExitSuccess -> pure outText
84− ExitFailure code -> throwError $ T.pack $ show $ ProcessFailed (cmdspec process) code
76+ ExitFailure code -> throwError $ Right $ ProcessFailed (cmdspec process) code
8577
8678-- | Like `System.Process.callProcess` but logs the combined output (stdout and stderr)
8779-- with the corresponding severity.
@@ -92,7 +84,7 @@ readProcessAndLogOutput (sev_out, sev_err) process = do
9284-- `callProcessAndLogOutput (Debug, Debug)`.
9385callProcessAndLogOutput
9486
95− :: (MonadIO m, MonadMask m, Cli m)
87+ :: (MonadIO m, Cli m)
9688 => (Severity, Severity) -> CreateProcess -> m ()
9789callProcessAndLogOutput (sev_out, sev_err) process =
9890 void $ withProcess process $ \out err -> do
@@ -108,7 +100,7 @@ createProcess
108100 :: (MonadIO m, Cli m)
109101 => CreateProcess -> m (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
110102createProcess p = do
111− putLog Debug $ "Creating process: " <> T.pack (reconstructCommand (cmdspec p))
103+ putLog Debug $ "Creating process: " <> reconstructCommand (cmdspec p)
112104 liftIO $ Process.createProcess p
113105
114106-- | Like `System.Process.createProcess_` but also logs (debug) the process being run
@@ -116,7 +108,7 @@ createProcess_
116108 :: (MonadIO m, Cli m)
117109 => String -> CreateProcess -> m (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
118110createProcess_ name p = do
119− putLog Debug $ "Creating process " <> T.pack name <> ": " <> T.pack (reconstructCommand (cmdspec p))
111+ putLog Debug $ "Creating process " <> T.pack name <> ": " <> reconstructCommand (cmdspec p)
120112 liftIO $ Process.createProcess p
121113
122114-- | Like `System.Process.callProcess` but also logs (debug) the process being run
@@ -136,7 +128,7 @@ callCommand cmd = do
136128 liftIO $ Process.callCommand cmd
137129
138130withProcess
139− :: (MonadIO m, MonadMask m, Cli m)
131+ :: (MonadIO m, Cli m)
140132 => CreateProcess -> (Handle -> Handle -> m ()) -> m (Handle, Handle)
141133withProcess process f = do -- TODO: Use bracket.
142134 -- FIXME: Using `withCreateProcess` here leads to something operating illegally on closed handles.
@@ -147,7 +139,7 @@ withProcess process f = do -- TODO: Use bracket.
147139
148140 liftIO (waitForProcess p) >>= \case
149141 ExitSuccess -> return (out, err)
150− ExitFailure code -> throwM $ ProcessFailed (cmdspec process) code
142+ ExitFailure code -> throwError $ Right $ ProcessFailed (cmdspec process) code
151143
152144-- Create an input stream from the file handle, associating each item with the given severity.
153145streamHandle :: Severity -> Handle -> IO (InputStream (Severity, BSC.ByteString))
@@ -155,16 +147,9 @@ streamHandle sev = Streams.map (sev,) <=< handleToInputStream
155147
156148-- | Read from an input stream and log its contents
157149streamToLog
158− :: (MonadIO m, MonadMask m, Cli m)
150+ :: (MonadIO m, Cli m)
159151 => InputStream (Severity, BSC.ByteString) -> m ()
160152streamToLog stream = fix $ \loop -> do
161153 liftIO (Streams.read stream) >>= \case
162154 Nothing -> return ()
163155 Just (sev, line) -> putLogRaw sev (T.decodeUtf8 line) >> loop
164−
165−reconstructCommand :: Process.CmdSpec -> String
166−reconstructCommand (Process.ShellCommand str) = str
167−reconstructCommand (Process.RawCommand c as) = processToShellString c as
168− where
169− processToShellString cmd args = unwords $ map quoteAndEscape (cmd : args)
170− quoteAndEscape x = T.unpack $ "'" <> T.replace "'" "'\''" (T.pack x) <> "'"