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

import           Control.Monad.Trans.Except (ExceptT)
import qualified Data.ByteString.Lazy.Char8 as LBS
import           Data.Text (Text)
import qualified Data.Text as Text

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

import           Cardano.Api

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

data ShelleyTextViewFileError
  = TextViewReadFileError (FileError TextEnvelopeError)
  | TextViewCBORPrettyPrintError !HelpersError
  deriving Int -> ShelleyTextViewFileError -> ShowS
[ShelleyTextViewFileError] -> ShowS
ShelleyTextViewFileError -> String
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 (forall e. Error e => e -> String
displayError FileError TextEnvelopeError
fileErr)
    TextViewCBORPrettyPrintError HelpersError
hlprsErr ->
      Text
"Error pretty printing CBOR: " 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 <- forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError TextEnvelopeError -> ShelleyTextViewFileError
TextViewReadFileError forall a b. (a -> b) -> a -> b
$ 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) -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ String -> ByteString -> IO ()
LBS.writeFile String
oFpath ByteString
lbCBOR
    Maybe OutputFile
Nothing -> forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT HelpersError -> ShelleyTextViewFileError
TextViewCBORPrettyPrintError forall a b. (a -> b) -> a -> b
$ ByteString -> ExceptT HelpersError IO ()
pPrintCBOR ByteString
lbCBOR