aboutsummaryrefslogtreecommitdiffstats
path: root/app/Process.hs
blob: ecc9cd3d0b2b32bfbfb8b1e15ba3368a3992d77d (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
26
27
28
29
30
31
32
module Process (sh, sh_, quote) where

import Control.Exception (Exception, throwIO)
import Data.ByteString.Lazy (ByteString)
import System.Exit (ExitCode (ExitSuccess))
import System.Process.Typed (ProcessConfig, readProcess, readProcessStderr)

data ProcessException = ProcessException String ExitCode ByteString
  deriving (Show)

instance Exception ProcessException

sh :: ProcessConfig stdin stdoutIgnored stderr -> IO ByteString
sh proc = do
  (exitCode, out, err) <- readProcess proc
  if exitCode == ExitSuccess
    then pure out
    else throwIO $ ProcessException (show proc) exitCode err

sh_ :: ProcessConfig stdin stdoutIgnored stderr -> IO ()
sh_ proc = do
  (exitCode, err) <- readProcessStderr proc
  if exitCode == ExitSuccess
    then pure ()
    else throwIO $ ProcessException (show proc) exitCode err

quote :: String -> String
quote s = "'" ++ escape s ++ "'"
  where
    escape [] = []
    escape ('\'' : cs) = '\'' : '\\' : '\'' : '\'' : escape cs
    escape (c : cs) = c : escape cs