module Cardano.CLI.Shelley.Run.TextView
  ( ShelleyTextViewFileError(..)
  , renderShelleyTextViewFileError
  , runTextViewCmd
  ) where

import           Cardano.Prelude

import qualified Data.Text as Text

import           Cardano.CLI.Helpers (HelpersError, pPrintCBOR, renderHelpersError)
import           Cardano.CLI.Shelley.Parsers

import           Cardano.Api

import           Control.Monad.Trans.Except.Extra (firstExceptT, newExceptT)

import qualified Data.ByteString.Lazy.Char8 as LBS

data ShelleyTextViewFileError
  = TextViewReadFileError (FileError TextEnvelopeError)
  | TextViewCBORPrettyPrintError !HelpersError
  deriving Int -> ShelleyTextViewFileError -> ShowS
[ShelleyTextViewFileError] -> ShowS
ShelleyTextViewFileError -> String
(Int -> ShelleyTextViewFileError -> ShowS)
-> (ShelleyTextViewFileError -> String)
-> ([ShelleyTextViewFileError] -> ShowS)
-> Show ShelleyTextViewFileError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ShelleyTextViewFileError] -> ShowS
$cshowList :: [ShelleyTextViewFileError] -> ShowS
show :: ShelleyTextViewFileError -> String
$cshow :: ShelleyTextViewFileError -> String
showsPrec :: Int -> ShelleyTextViewFileError -> ShowS
$cshowsPrec :: Int -> ShelleyTextViewFileError -> ShowS
Show

renderShelleyTextViewFileError :: ShelleyTextViewFileError -> Text
renderShelleyTextViewFileError :: ShelleyTextViewFileError -> Text
renderShelleyTextViewFileError ShelleyTextViewFileError
err =
  case ShelleyTextViewFileError
err of
    TextViewReadFileError FileError TextEnvelopeError
fileErr -> String -> Text
Text.pack (FileError TextEnvelopeError -> String
forall e. Error e => e -> String
displayError FileError TextEnvelopeError
fileErr)
    TextViewCBORPrettyPrintError HelpersError
hlprsErr ->
      Text
"Error pretty printing CBOR: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> HelpersError -> Text
renderHelpersError HelpersError
hlprsErr


runTextViewCmd :: TextViewCmd -> ExceptT ShelleyTextViewFileError IO ()
runTextViewCmd :: TextViewCmd -> ExceptT ShelleyTextViewFileError IO ()
runTextViewCmd TextViewCmd
cmd =
  case TextViewCmd
cmd of
    TextViewInfo String
fpath Maybe OutputFile
mOutfile -> String
-> Maybe OutputFile -> ExceptT ShelleyTextViewFileError IO ()
runTextViewInfo String
fpath Maybe OutputFile
mOutfile

runTextViewInfo :: FilePath -> Maybe OutputFile -> ExceptT ShelleyTextViewFileError IO ()
runTextViewInfo :: String
-> Maybe OutputFile -> ExceptT ShelleyTextViewFileError IO ()
runTextViewInfo String
fpath Maybe OutputFile
mOutFile = do
  TextEnvelope
tv <- (FileError TextEnvelopeError -> ShelleyTextViewFileError)
-> ExceptT (FileError TextEnvelopeError) IO TextEnvelope
-> ExceptT ShelleyTextViewFileError IO TextEnvelope
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError TextEnvelopeError -> ShelleyTextViewFileError
TextViewReadFileError (ExceptT (FileError TextEnvelopeError) IO TextEnvelope
 -> ExceptT ShelleyTextViewFileError IO TextEnvelope)
-> ExceptT (FileError TextEnvelopeError) IO TextEnvelope
-> ExceptT ShelleyTextViewFileError 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 (String -> IO (Either (FileError TextEnvelopeError) TextEnvelope)
readTextEnvelopeFromFile String
fpath)
  let lbCBOR :: ByteString
lbCBOR = ByteString -> ByteString
LBS.fromStrict (TextEnvelope -> ByteString
textEnvelopeRawCBOR TextEnvelope
tv)
  case Maybe OutputFile
mOutFile of
    Just (OutputFile String
oFpath) -> IO () -> ExceptT ShelleyTextViewFileError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ShelleyTextViewFileError IO ())
-> IO () -> ExceptT ShelleyTextViewFileError IO ()
forall a b. (a -> b) -> a -> b
$ String -> ByteString -> IO ()
LBS.writeFile String
oFpath ByteString
lbCBOR
    Maybe OutputFile
Nothing -> (HelpersError -> ShelleyTextViewFileError)
-> ExceptT HelpersError IO ()
-> ExceptT ShelleyTextViewFileError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT HelpersError -> ShelleyTextViewFileError
TextViewCBORPrettyPrintError (ExceptT HelpersError IO ()
 -> ExceptT ShelleyTextViewFileError IO ())
-> ExceptT HelpersError IO ()
-> ExceptT ShelleyTextViewFileError IO ()
forall a b. (a -> b) -> a -> b
$ ByteString -> ExceptT HelpersError IO ()
pPrintCBOR ByteString
lbCBOR