aboutsummaryrefslogtreecommitdiffstats
path: root/app/Process.hs
diff options
context:
space:
mode:
authorLibravatar Fabian Kirchner <kirchner@posteo.de>2023-10-13 21:24:04 +0200
committerLibravatar Fabian Kirchner <kirchner@posteo.de>2023-10-13 22:19:24 +0200
commita0c72a80fbfed8e0963edff17ab5d52d2e3abd5b (patch)
treead33fffa970f94d8931e70644e44946cc3dfe524 /app/Process.hs
parent09d5a5941a1feb23aece4d5311092c1a23d4ae11 (diff)
refactor: extract sh and sh_ into Process
Diffstat (limited to 'app/Process.hs')
-rw-r--r--app/Process.hs25
1 files changed, 25 insertions, 0 deletions
diff --git a/app/Process.hs b/app/Process.hs
new file mode 100644
index 0000000..39132d4
--- /dev/null
+++ b/app/Process.hs
@@ -0,0 +1,25 @@
+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