{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.EraBased.Run.TextView
  ( runTextViewCmds
  , runTextViewInfoCmd
  )
where

import           Cardano.Api

import           Cardano.CLI.EraBased.Commands.TextView
import           Cardano.CLI.Helpers (pPrintCBOR)
import           Cardano.CLI.Types.Errors.TextViewFileError

import qualified Data.ByteString.Lazy.Char8 as LBS

runTextViewCmds :: TextViewCmds era -> ExceptT TextViewFileError IO ()
runTextViewCmds :: forall era. TextViewCmds era -> ExceptT TextViewFileError IO ()
runTextViewCmds = \case
  TextViewInfo FilePath
fpath Maybe (File () 'Out)
mOutfile -> FilePath -> Maybe (File () 'Out) -> ExceptT TextViewFileError IO ()
runTextViewInfoCmd FilePath
fpath Maybe (File () 'Out)
mOutfile

runTextViewInfoCmd
  :: ()
  => FilePath
  -> Maybe (File () Out)
  -> ExceptT TextViewFileError IO ()
runTextViewInfoCmd :: FilePath -> Maybe (File () 'Out) -> ExceptT TextViewFileError IO ()
runTextViewInfoCmd FilePath
fpath Maybe (File () 'Out)
mOutFile = do
  TextEnvelope
tv <- (FileError TextEnvelopeError -> TextViewFileError)
-> ExceptT (FileError TextEnvelopeError) IO TextEnvelope
-> ExceptT TextViewFileError IO TextEnvelope
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError TextEnvelopeError -> TextViewFileError
TextViewReadFileError (ExceptT (FileError TextEnvelopeError) IO TextEnvelope
 -> ExceptT TextViewFileError IO TextEnvelope)
-> ExceptT (FileError TextEnvelopeError) IO TextEnvelope
-> ExceptT TextViewFileError IO TextEnvelope
forall a b. (a -> b) -> a -> b
$ IO (Either (FileError TextEnvelopeError) TextEnvelope)
-> ExceptT (FileError TextEnvelopeError) IO TextEnvelope
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (FilePath -> IO (Either (FileError TextEnvelopeError) TextEnvelope)
readTextEnvelopeFromFile FilePath
fpath)
  let lbCBOR :: LazyByteString
lbCBOR = StrictByteString -> LazyByteString
LBS.fromStrict (TextEnvelope -> StrictByteString
textEnvelopeRawCBOR TextEnvelope
tv)
  case Maybe (File () 'Out)
mOutFile of
    Just (File FilePath
oFpath) -> IO () -> ExceptT TextViewFileError IO ()
forall a. IO a -> ExceptT TextViewFileError IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT TextViewFileError IO ())
-> IO () -> ExceptT TextViewFileError IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> LazyByteString -> IO ()
LBS.writeFile FilePath
oFpath LazyByteString
lbCBOR
    Maybe (File () 'Out)
Nothing -> (HelpersError -> TextViewFileError)
-> ExceptT HelpersError IO () -> ExceptT TextViewFileError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT HelpersError -> TextViewFileError
TextViewCBORPrettyPrintError (ExceptT HelpersError IO () -> ExceptT TextViewFileError IO ())
-> ExceptT HelpersError IO () -> ExceptT TextViewFileError IO ()
forall a b. (a -> b) -> a -> b
$ LazyByteString -> ExceptT HelpersError IO ()
pPrintCBOR LazyByteString
lbCBOR