module Cardano.CLI.Shelley.Run
  ( ShelleyClientCmdError
  , renderShelleyClientCmdError
  , runShelleyClientCommand
  ) where

import           Control.Monad.Trans.Except (ExceptT)
import           Data.Text (Text)

import           Cardano.Api

import           Control.Monad.Trans.Except.Extra (firstExceptT)
import qualified Data.Text as Text

import           Cardano.CLI.Shelley.Parsers

import           Cardano.CLI.Shelley.Run.Address
import           Cardano.CLI.Shelley.Run.Governance
import           Cardano.CLI.Shelley.Run.Key
import           Cardano.CLI.Shelley.Run.Node
import           Cardano.CLI.Shelley.Run.Pool
import           Cardano.CLI.Shelley.Run.Query
import           Cardano.CLI.Shelley.Run.StakeAddress
import           Cardano.CLI.Shelley.Run.Transaction
                                         -- Block, System, DevOps
import           Cardano.CLI.Shelley.Run.Genesis
import           Cardano.CLI.Shelley.Run.TextView

data ShelleyClientCmdError
  = ShelleyCmdAddressError !ShelleyAddressCmdError
  | ShelleyCmdGenesisError !ShelleyGenesisCmdError
  | ShelleyCmdGovernanceError !ShelleyGovernanceCmdError
  | ShelleyCmdNodeError !ShelleyNodeCmdError
  | ShelleyCmdPoolError !ShelleyPoolCmdError
  | ShelleyCmdStakeAddressError !ShelleyStakeAddressCmdError
  | ShelleyCmdTextViewError !ShelleyTextViewFileError
  | ShelleyCmdTransactionError !ShelleyTxCmdError
  | ShelleyCmdQueryError !ShelleyQueryCmdError
  | ShelleyCmdKeyError !ShelleyKeyCmdError

renderShelleyClientCmdError :: ShelleyCommand -> ShelleyClientCmdError -> Text
renderShelleyClientCmdError :: ShelleyCommand -> ShelleyClientCmdError -> Text
renderShelleyClientCmdError ShelleyCommand
cmd ShelleyClientCmdError
err =
  case ShelleyClientCmdError
err of
    ShelleyCmdAddressError ShelleyAddressCmdError
addrCmdErr ->
       forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyAddressCmdError -> Text
renderShelleyAddressCmdError ShelleyAddressCmdError
addrCmdErr
    ShelleyCmdGenesisError ShelleyGenesisCmdError
genesisCmdErr ->
       forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd (String -> Text
Text.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. Error e => e -> String
displayError) ShelleyGenesisCmdError
genesisCmdErr
    ShelleyCmdGovernanceError ShelleyGovernanceCmdError
govCmdErr ->
       forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyGovernanceCmdError -> Text
renderShelleyGovernanceError ShelleyGovernanceCmdError
govCmdErr
    ShelleyCmdNodeError ShelleyNodeCmdError
nodeCmdErr ->
       forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyNodeCmdError -> Text
renderShelleyNodeCmdError ShelleyNodeCmdError
nodeCmdErr
    ShelleyCmdPoolError ShelleyPoolCmdError
poolCmdErr ->
       forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyPoolCmdError -> Text
renderShelleyPoolCmdError ShelleyPoolCmdError
poolCmdErr
    ShelleyCmdStakeAddressError ShelleyStakeAddressCmdError
stakeAddrCmdErr ->
       forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyStakeAddressCmdError -> Text
renderShelleyStakeAddressCmdError ShelleyStakeAddressCmdError
stakeAddrCmdErr
    ShelleyCmdTextViewError ShelleyTextViewFileError
txtViewErr ->
       forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyTextViewFileError -> Text
renderShelleyTextViewFileError ShelleyTextViewFileError
txtViewErr
    ShelleyCmdTransactionError ShelleyTxCmdError
txErr ->
       forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyTxCmdError -> Text
renderShelleyTxCmdError ShelleyTxCmdError
txErr
    ShelleyCmdQueryError ShelleyQueryCmdError
queryErr ->
       forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyQueryCmdError -> Text
renderShelleyQueryCmdError ShelleyQueryCmdError
queryErr
    ShelleyCmdKeyError ShelleyKeyCmdError
keyErr ->
       forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyKeyCmdError -> Text
renderShelleyKeyCmdError ShelleyKeyCmdError
keyErr
 where
   renderError :: ShelleyCommand -> (a -> Text) -> a -> Text
   renderError :: forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
shelleyCmd a -> Text
renderer a
shelCliCmdErr =
      forall a. Monoid a => [a] -> a
mconcat [ Text
"Command failed: "
              , ShelleyCommand -> Text
renderShelleyCommand ShelleyCommand
shelleyCmd
              , Text
"  Error: "
              , a -> Text
renderer a
shelCliCmdErr
              ]


--
-- CLI shelley command dispatch
--

runShelleyClientCommand :: ShelleyCommand -> ExceptT ShelleyClientCmdError IO ()
runShelleyClientCommand :: ShelleyCommand -> ExceptT ShelleyClientCmdError IO ()
runShelleyClientCommand (AddressCmd      AddressCmd
cmd) = forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyAddressCmdError -> ShelleyClientCmdError
ShelleyCmdAddressError forall a b. (a -> b) -> a -> b
$ AddressCmd -> ExceptT ShelleyAddressCmdError IO ()
runAddressCmd AddressCmd
cmd
runShelleyClientCommand (StakeAddressCmd StakeAddressCmd
cmd) = forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyStakeAddressCmdError -> ShelleyClientCmdError
ShelleyCmdStakeAddressError forall a b. (a -> b) -> a -> b
$ StakeAddressCmd -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressCmd StakeAddressCmd
cmd
runShelleyClientCommand (KeyCmd          KeyCmd
cmd) = forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyKeyCmdError -> ShelleyClientCmdError
ShelleyCmdKeyError forall a b. (a -> b) -> a -> b
$ KeyCmd -> ExceptT ShelleyKeyCmdError IO ()
runKeyCmd KeyCmd
cmd
runShelleyClientCommand (TransactionCmd  TransactionCmd
cmd) = forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyTxCmdError -> ShelleyClientCmdError
ShelleyCmdTransactionError forall a b. (a -> b) -> a -> b
$ TransactionCmd -> ExceptT ShelleyTxCmdError IO ()
runTransactionCmd  TransactionCmd
cmd
runShelleyClientCommand (NodeCmd         NodeCmd
cmd) = forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyNodeCmdError -> ShelleyClientCmdError
ShelleyCmdNodeError forall a b. (a -> b) -> a -> b
$ NodeCmd -> ExceptT ShelleyNodeCmdError IO ()
runNodeCmd NodeCmd
cmd
runShelleyClientCommand (PoolCmd         PoolCmd
cmd) = forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyPoolCmdError -> ShelleyClientCmdError
ShelleyCmdPoolError forall a b. (a -> b) -> a -> b
$ PoolCmd -> ExceptT ShelleyPoolCmdError IO ()
runPoolCmd PoolCmd
cmd
runShelleyClientCommand (QueryCmd        QueryCmd
cmd) = forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyQueryCmdError -> ShelleyClientCmdError
ShelleyCmdQueryError forall a b. (a -> b) -> a -> b
$ QueryCmd -> ExceptT ShelleyQueryCmdError IO ()
runQueryCmd QueryCmd
cmd
runShelleyClientCommand (GovernanceCmd   GovernanceCmd
cmd) = forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyGovernanceCmdError -> ShelleyClientCmdError
ShelleyCmdGovernanceError forall a b. (a -> b) -> a -> b
$ GovernanceCmd -> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceCmd GovernanceCmd
cmd
runShelleyClientCommand (GenesisCmd      GenesisCmd
cmd) = forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyGenesisCmdError -> ShelleyClientCmdError
ShelleyCmdGenesisError forall a b. (a -> b) -> a -> b
$ GenesisCmd -> ExceptT ShelleyGenesisCmdError IO ()
runGenesisCmd GenesisCmd
cmd
runShelleyClientCommand (TextViewCmd     TextViewCmd
cmd) = forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyTextViewFileError -> ShelleyClientCmdError
ShelleyCmdTextViewError forall a b. (a -> b) -> a -> b
$ TextViewCmd -> ExceptT ShelleyTextViewFileError IO ()
runTextViewCmd TextViewCmd
cmd