packages feed

seihou-core-0.6.0.0: src/Seihou/Effect/ProcessInterp.hs

module Seihou.Effect.ProcessInterp
  ( runProcessIO,
  )
where

import Data.Text qualified as T
import Seihou.Effect.Process (Process (..))
import Seihou.Prelude
import System.Exit (ExitCode)
import System.Process (CreateProcess (..), proc, readCreateProcessWithExitCode)

runProcessIO :: (IOE :> es) => Eff (Process : es) a -> Eff es a
runProcessIO = interpret $ \_ -> \case
  RunProcess cmd args workDir -> liftIO $ do
    -- CreateProcess is a third-party type with no Generic instance, so it has
    -- no #cwd label to set. Record update syntax is the only option here.
    let cp =
          (proc (T.unpack cmd) (map T.unpack args))
            { cwd = workDir
            }
    (exitCode, stdout, stderr) <- readCreateProcessWithExitCode cp ""
    pure (exitCode, T.pack stdout, T.pack stderr)