diff options
author | Alexander Foremny <aforemny@posteo.de> | 2023-12-20 02:35:38 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-03-25 04:57:19 +0100 |
commit | 812fcbadae72960d200286355c9aaecfbe350bf2 (patch) | |
tree | f5b872861daa9e55162431f8ea9ccb27b11e1acd /app/Process.hs | |
parent | 38e98644192ed38de8b0a789cebfb0a6149eadbd (diff) |
chore: parameterize `sh`'s output
Diffstat (limited to 'app/Process.hs')
-rw-r--r-- | app/Process.hs | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/app/Process.hs b/app/Process.hs index 2b3eaf6..8df56c5 100644 --- a/app/Process.hs +++ b/app/Process.hs @@ -12,21 +12,23 @@ module Process where import Control.Exception (throwIO) -import Data.ByteString.Lazy.Char8 qualified as LB +import Data.ByteString qualified as B +import Data.ByteString.Lazy qualified as LB import Data.List (intercalate) import Data.String (fromString) import Data.Text qualified as T +import Data.Text.Encoding qualified as T import Data.Text.Lazy qualified as LT import Data.Text.Lazy.Encoding qualified as LT import Exception qualified as E import System.Exit (ExitCode (ExitSuccess)) import System.Process.Typed (ProcessConfig, StreamSpec, StreamType (STInput), byteStringInput, readProcess, readProcessStderr) -sh :: ProcessConfig stdin stdoutIgnored stderr -> IO LB.ByteString +sh :: Output a => ProcessConfig stdin stdoutIgnored stderr -> IO a sh processConfig = do (exitCode, out, err) <- readProcess processConfig if exitCode == ExitSuccess - then pure out + then pure (fromLB out) else throwIO $ E.ProcessException (show processConfig) exitCode err sh_ :: ProcessConfig stdin stdoutIgnored stderr -> IO () @@ -36,6 +38,21 @@ sh_ processConfig = do then pure () else throwIO $ E.ProcessException (show processConfig) exitCode err +class Output a where + fromLB :: LB.ByteString -> a + +instance Output LB.ByteString where + fromLB = id + +instance Output B.ByteString where + fromLB = LB.toStrict + +instance Output LT.Text where + fromLB = LT.decodeUtf8 + +instance Output T.Text where + fromLB = T.decodeUtf8 . LB.toStrict + class Quotable a where quote :: a -> String |