module Control.Monad.Trans.Either.Exit (
orDie
, orDieWithCode
) where
import Control.Applicative (pure)
import Control.Monad ((>>=), (>>))
import Data.Either (either)
import Data.Function ((.))
import Data.Int (Int)
import Data.Text (Text)
import qualified Data.Text as T
import System.Exit (ExitCode(..), exitWith)
import System.IO (IO, stderr, hPutStrLn)
import Control.Monad.Trans.Either
orDie :: (e -> Text) -> EitherT e IO a -> IO a
orDie = orDieWithCode 1
orDieWithCode :: Int -> (e -> Text) -> EitherT e IO a -> IO a
orDieWithCode code render e =
runEitherT e >>=
either (\err -> (hPutStrLn stderr . T.unpack . render) err >> exitWith (ExitFailure code)) pure