aboutsummaryrefslogtreecommitdiffstats
path: root/app/Process.hs
diff options
context:
space:
mode:
Diffstat (limited to 'app/Process.hs')
-rw-r--r--app/Process.hs23
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