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

import           Cardano.Prelude

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

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
  deriving Int -> ShelleyClientCmdError -> ShowS
[ShelleyClientCmdError] -> ShowS
ShelleyClientCmdError -> String
(Int -> ShelleyClientCmdError -> ShowS)
-> (ShelleyClientCmdError -> String)
-> ([ShelleyClientCmdError] -> ShowS)
-> Show ShelleyClientCmdError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ShelleyClientCmdError] -> ShowS
$cshowList :: [ShelleyClientCmdError] -> ShowS
show :: ShelleyClientCmdError -> String
$cshow :: ShelleyClientCmdError -> String
showsPrec :: Int -> ShelleyClientCmdError -> ShowS
$cshowsPrec :: Int -> ShelleyClientCmdError -> ShowS
Show

renderShelleyClientCmdError :: ShelleyCommand -> ShelleyClientCmdError -> Text
renderShelleyClientCmdError :: ShelleyCommand -> ShelleyClientCmdError -> Text
renderShelleyClientCmdError ShelleyCommand
cmd ShelleyClientCmdError
err =
  case ShelleyClientCmdError
err of
    ShelleyCmdAddressError ShelleyAddressCmdError
addrCmdErr ->
       ShelleyCommand
-> (ShelleyAddressCmdError -> Text)
-> ShelleyAddressCmdError
-> Text
forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyAddressCmdError -> Text
renderShelleyAddressCmdError ShelleyAddressCmdError
addrCmdErr
    ShelleyCmdGenesisError ShelleyGenesisCmdError
genesisCmdErr ->
       ShelleyCommand
-> (ShelleyGenesisCmdError -> Text)
-> ShelleyGenesisCmdError
-> Text
forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyGenesisCmdError -> Text
renderShelleyGenesisCmdError ShelleyGenesisCmdError
genesisCmdErr
    ShelleyCmdGovernanceError ShelleyGovernanceCmdError
govCmdErr ->
       ShelleyCommand
-> (ShelleyGovernanceCmdError -> Text)
-> ShelleyGovernanceCmdError
-> Text
forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyGovernanceCmdError -> Text
renderShelleyGovernanceError ShelleyGovernanceCmdError
govCmdErr
    ShelleyCmdNodeError ShelleyNodeCmdError
nodeCmdErr ->
       ShelleyCommand
-> (ShelleyNodeCmdError -> Text) -> ShelleyNodeCmdError -> Text
forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyNodeCmdError -> Text
renderShelleyNodeCmdError ShelleyNodeCmdError
nodeCmdErr
    ShelleyCmdPoolError ShelleyPoolCmdError
poolCmdErr ->
       ShelleyCommand
-> (ShelleyPoolCmdError -> Text) -> ShelleyPoolCmdError -> Text
forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyPoolCmdError -> Text
renderShelleyPoolCmdError ShelleyPoolCmdError
poolCmdErr
    ShelleyCmdStakeAddressError ShelleyStakeAddressCmdError
stakeAddrCmdErr ->
       ShelleyCommand
-> (ShelleyStakeAddressCmdError -> Text)
-> ShelleyStakeAddressCmdError
-> Text
forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyStakeAddressCmdError -> Text
renderShelleyStakeAddressCmdError ShelleyStakeAddressCmdError
stakeAddrCmdErr
    ShelleyCmdTextViewError ShelleyTextViewFileError
txtViewErr ->
       ShelleyCommand
-> (ShelleyTextViewFileError -> Text)
-> ShelleyTextViewFileError
-> Text
forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyTextViewFileError -> Text
renderShelleyTextViewFileError ShelleyTextViewFileError
txtViewErr
    ShelleyCmdTransactionError ShelleyTxCmdError
txErr ->
       ShelleyCommand
-> (ShelleyTxCmdError -> Text) -> ShelleyTxCmdError -> Text
forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyTxCmdError -> Text
renderShelleyTxCmdError ShelleyTxCmdError
txErr
    ShelleyCmdQueryError ShelleyQueryCmdError
queryErr ->
       ShelleyCommand
-> (ShelleyQueryCmdError -> Text) -> ShelleyQueryCmdError -> Text
forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyQueryCmdError -> Text
renderShelleyQueryCmdError ShelleyQueryCmdError
queryErr
    ShelleyCmdKeyError ShelleyKeyCmdError
keyErr ->
       ShelleyCommand
-> (ShelleyKeyCmdError -> Text) -> ShelleyKeyCmdError -> Text
forall a. ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
cmd ShelleyKeyCmdError -> Text
renderShelleyKeyCmdError ShelleyKeyCmdError
keyErr
 where
   renderError :: ShelleyCommand -> (a -> Text) -> a -> Text
   renderError :: ShelleyCommand -> (a -> Text) -> a -> Text
renderError ShelleyCommand
shelleyCmd a -> Text
renderer a
shelCliCmdErr =
      [Text] -> Text
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) = (ShelleyAddressCmdError -> ShelleyClientCmdError)
-> ExceptT ShelleyAddressCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyAddressCmdError -> ShelleyClientCmdError
ShelleyCmdAddressError (ExceptT ShelleyAddressCmdError IO ()
 -> ExceptT ShelleyClientCmdError IO ())
-> ExceptT ShelleyAddressCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ AddressCmd -> ExceptT ShelleyAddressCmdError IO ()
runAddressCmd AddressCmd
cmd
runShelleyClientCommand (StakeAddressCmd StakeAddressCmd
cmd) = (ShelleyStakeAddressCmdError -> ShelleyClientCmdError)
-> ExceptT ShelleyStakeAddressCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyStakeAddressCmdError -> ShelleyClientCmdError
ShelleyCmdStakeAddressError (ExceptT ShelleyStakeAddressCmdError IO ()
 -> ExceptT ShelleyClientCmdError IO ())
-> ExceptT ShelleyStakeAddressCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ StakeAddressCmd -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressCmd StakeAddressCmd
cmd
runShelleyClientCommand (KeyCmd          KeyCmd
cmd) = (ShelleyKeyCmdError -> ShelleyClientCmdError)
-> ExceptT ShelleyKeyCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyKeyCmdError -> ShelleyClientCmdError
ShelleyCmdKeyError (ExceptT ShelleyKeyCmdError IO ()
 -> ExceptT ShelleyClientCmdError IO ())
-> ExceptT ShelleyKeyCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ KeyCmd -> ExceptT ShelleyKeyCmdError IO ()
runKeyCmd KeyCmd
cmd
runShelleyClientCommand (TransactionCmd  TransactionCmd
cmd) = (ShelleyTxCmdError -> ShelleyClientCmdError)
-> ExceptT ShelleyTxCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyTxCmdError -> ShelleyClientCmdError
ShelleyCmdTransactionError (ExceptT ShelleyTxCmdError IO ()
 -> ExceptT ShelleyClientCmdError IO ())
-> ExceptT ShelleyTxCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ TransactionCmd -> ExceptT ShelleyTxCmdError IO ()
runTransactionCmd  TransactionCmd
cmd
runShelleyClientCommand (NodeCmd         NodeCmd
cmd) = (ShelleyNodeCmdError -> ShelleyClientCmdError)
-> ExceptT ShelleyNodeCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyNodeCmdError -> ShelleyClientCmdError
ShelleyCmdNodeError (ExceptT ShelleyNodeCmdError IO ()
 -> ExceptT ShelleyClientCmdError IO ())
-> ExceptT ShelleyNodeCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ NodeCmd -> ExceptT ShelleyNodeCmdError IO ()
runNodeCmd NodeCmd
cmd
runShelleyClientCommand (PoolCmd         PoolCmd
cmd) = (ShelleyPoolCmdError -> ShelleyClientCmdError)
-> ExceptT ShelleyPoolCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyPoolCmdError -> ShelleyClientCmdError
ShelleyCmdPoolError (ExceptT ShelleyPoolCmdError IO ()
 -> ExceptT ShelleyClientCmdError IO ())
-> ExceptT ShelleyPoolCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ PoolCmd -> ExceptT ShelleyPoolCmdError IO ()
runPoolCmd PoolCmd
cmd
runShelleyClientCommand (QueryCmd        QueryCmd
cmd) = (ShelleyQueryCmdError -> ShelleyClientCmdError)
-> ExceptT ShelleyQueryCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyQueryCmdError -> ShelleyClientCmdError
ShelleyCmdQueryError (ExceptT ShelleyQueryCmdError IO ()
 -> ExceptT ShelleyClientCmdError IO ())
-> ExceptT ShelleyQueryCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ QueryCmd -> ExceptT ShelleyQueryCmdError IO ()
runQueryCmd QueryCmd
cmd
runShelleyClientCommand (GovernanceCmd   GovernanceCmd
cmd) = (ShelleyGovernanceCmdError -> ShelleyClientCmdError)
-> ExceptT ShelleyGovernanceCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyGovernanceCmdError -> ShelleyClientCmdError
ShelleyCmdGovernanceError (ExceptT ShelleyGovernanceCmdError IO ()
 -> ExceptT ShelleyClientCmdError IO ())
-> ExceptT ShelleyGovernanceCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ GovernanceCmd -> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceCmd GovernanceCmd
cmd
runShelleyClientCommand (GenesisCmd      GenesisCmd
cmd) = (ShelleyGenesisCmdError -> ShelleyClientCmdError)
-> ExceptT ShelleyGenesisCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyGenesisCmdError -> ShelleyClientCmdError
ShelleyCmdGenesisError (ExceptT ShelleyGenesisCmdError IO ()
 -> ExceptT ShelleyClientCmdError IO ())
-> ExceptT ShelleyGenesisCmdError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ GenesisCmd -> ExceptT ShelleyGenesisCmdError IO ()
runGenesisCmd GenesisCmd
cmd
runShelleyClientCommand (TextViewCmd     TextViewCmd
cmd) = (ShelleyTextViewFileError -> ShelleyClientCmdError)
-> ExceptT ShelleyTextViewFileError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyTextViewFileError -> ShelleyClientCmdError
ShelleyCmdTextViewError (ExceptT ShelleyTextViewFileError IO ()
 -> ExceptT ShelleyClientCmdError IO ())
-> ExceptT ShelleyTextViewFileError IO ()
-> ExceptT ShelleyClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ TextViewCmd -> ExceptT ShelleyTextViewFileError IO ()
runTextViewCmd TextViewCmd
cmd