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