{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.EraBased.Run
  ( runAnyEraCommand
  , runCmds
  , runGovernanceCmds
  )
where

import           Cardano.Api

import           Cardano.CLI.EraBased.Commands.TopLevelCommands
import           Cardano.CLI.EraBased.Run.Genesis
import           Cardano.CLI.EraBased.Run.Governance
import           Cardano.CLI.EraBased.Run.Query
import           Cardano.CLI.EraBased.Run.StakeAddress
import           Cardano.CLI.EraBased.Run.StakePool
import           Cardano.CLI.EraBased.Run.TextView
import           Cardano.CLI.EraBased.Run.Transaction
import           Cardano.CLI.Helpers (printEraDeprecationWarning)
import           Cardano.CLI.Run.Address
import           Cardano.CLI.Run.Key
import           Cardano.CLI.Run.Node
import           Cardano.CLI.Types.Errors.CmdError

import           Data.Function ((&))

runAnyEraCommand
  :: ()
  => AnyEraCommand
  -> ExceptT CmdError IO ()
runAnyEraCommand :: AnyEraCommand -> ExceptT CmdError IO ()
runAnyEraCommand = \case
  AnyEraCommandOf ShelleyBasedEra era
sbe Cmds era
cmd -> do
    ShelleyBasedEra era -> ExceptT CmdError IO ()
forall era (m :: * -> *) (eon :: * -> *).
(Typeable era, MonadIO m, ToCardanoEra eon) =>
eon era -> m ()
printEraDeprecationWarning ShelleyBasedEra era
sbe
    ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era => ExceptT CmdError IO ())
-> ExceptT CmdError IO ()
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints ShelleyBasedEra era
sbe ((ShelleyBasedEraConstraints era => ExceptT CmdError IO ())
 -> ExceptT CmdError IO ())
-> (ShelleyBasedEraConstraints era => ExceptT CmdError IO ())
-> ExceptT CmdError IO ()
forall a b. (a -> b) -> a -> b
$ Cmds era -> ExceptT CmdError IO ()
forall era. Cmds era -> ExceptT CmdError IO ()
runCmds Cmds era
cmd

runCmds
  :: ()
  => Cmds era
  -> ExceptT CmdError IO ()
runCmds :: forall era. Cmds era -> ExceptT CmdError IO ()
runCmds = \case
  AddressCmds AddressCmds
cmd ->
    AddressCmds -> ExceptT AddressCmdError IO ()
runAddressCmds AddressCmds
cmd ExceptT AddressCmdError IO ()
-> (ExceptT AddressCmdError IO () -> ExceptT CmdError IO ())
-> ExceptT CmdError IO ()
forall a b. a -> (a -> b) -> b
& (AddressCmdError -> CmdError)
-> ExceptT AddressCmdError IO () -> ExceptT CmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT AddressCmdError -> CmdError
CmdAddressError
  KeyCmds KeyCmds
cmd ->
    KeyCmds -> ExceptT KeyCmdError IO ()
runKeyCmds KeyCmds
cmd
      ExceptT KeyCmdError IO ()
-> (ExceptT KeyCmdError IO () -> ExceptT CmdError IO ())
-> ExceptT CmdError IO ()
forall a b. a -> (a -> b) -> b
& (KeyCmdError -> CmdError)
-> ExceptT KeyCmdError IO () -> ExceptT CmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT KeyCmdError -> CmdError
CmdKeyError
  GovernanceCmds GovernanceCmds era
cmd ->
    GovernanceCmds era -> ExceptT CmdError IO ()
forall era. GovernanceCmds era -> ExceptT CmdError IO ()
runGovernanceCmds GovernanceCmds era
cmd
  GenesisCmds GenesisCmds era
cmd ->
    GenesisCmds era -> ExceptT GenesisCmdError IO ()
forall era. GenesisCmds era -> ExceptT GenesisCmdError IO ()
runGenesisCmds GenesisCmds era
cmd
      ExceptT GenesisCmdError IO ()
-> (ExceptT GenesisCmdError IO () -> ExceptT CmdError IO ())
-> ExceptT CmdError IO ()
forall a b. a -> (a -> b) -> b
& (GenesisCmdError -> CmdError)
-> ExceptT GenesisCmdError IO () -> ExceptT CmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT GenesisCmdError -> CmdError
CmdGenesisError
  NodeCmds NodeCmds
cmd ->
    NodeCmds -> ExceptT NodeCmdError IO ()
runNodeCmds NodeCmds
cmd
      ExceptT NodeCmdError IO ()
-> (ExceptT NodeCmdError IO () -> ExceptT CmdError IO ())
-> ExceptT CmdError IO ()
forall a b. a -> (a -> b) -> b
& (NodeCmdError -> CmdError)
-> ExceptT NodeCmdError IO () -> ExceptT CmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT NodeCmdError -> CmdError
CmdNodeError
  QueryCmds QueryCmds era
cmd ->
    QueryCmds era -> ExceptT QueryCmdError IO ()
forall era. QueryCmds era -> ExceptT QueryCmdError IO ()
runQueryCmds QueryCmds era
cmd
      ExceptT QueryCmdError IO ()
-> (ExceptT QueryCmdError IO () -> ExceptT CmdError IO ())
-> ExceptT CmdError IO ()
forall a b. a -> (a -> b) -> b
& (QueryCmdError -> CmdError)
-> ExceptT QueryCmdError IO () -> ExceptT CmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT QueryCmdError -> CmdError
CmdQueryError
  StakeAddressCmds StakeAddressCmds era
cmd ->
    StakeAddressCmds era -> ExceptT StakeAddressCmdError IO ()
forall era.
StakeAddressCmds era -> ExceptT StakeAddressCmdError IO ()
runStakeAddressCmds StakeAddressCmds era
cmd
      ExceptT StakeAddressCmdError IO ()
-> (ExceptT StakeAddressCmdError IO () -> ExceptT CmdError IO ())
-> ExceptT CmdError IO ()
forall a b. a -> (a -> b) -> b
& (StakeAddressCmdError -> CmdError)
-> ExceptT StakeAddressCmdError IO () -> ExceptT CmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT StakeAddressCmdError -> CmdError
CmdStakeAddressError
  StakePoolCmds StakePoolCmds era
cmd ->
    StakePoolCmds era -> ExceptT StakePoolCmdError IO ()
forall era. StakePoolCmds era -> ExceptT StakePoolCmdError IO ()
runStakePoolCmds StakePoolCmds era
cmd
      ExceptT StakePoolCmdError IO ()
-> (ExceptT StakePoolCmdError IO () -> ExceptT CmdError IO ())
-> ExceptT CmdError IO ()
forall a b. a -> (a -> b) -> b
& (StakePoolCmdError -> CmdError)
-> ExceptT StakePoolCmdError IO () -> ExceptT CmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT StakePoolCmdError -> CmdError
CmdStakePoolError
  TextViewCmds TextViewCmds era
cmd ->
    TextViewCmds era -> ExceptT TextViewFileError IO ()
forall era. TextViewCmds era -> ExceptT TextViewFileError IO ()
runTextViewCmds TextViewCmds era
cmd
      ExceptT TextViewFileError IO ()
-> (ExceptT TextViewFileError IO () -> ExceptT CmdError IO ())
-> ExceptT CmdError IO ()
forall a b. a -> (a -> b) -> b
& (TextViewFileError -> CmdError)
-> ExceptT TextViewFileError IO () -> ExceptT CmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT TextViewFileError -> CmdError
CmdTextViewError
  TransactionCmds TransactionCmds era
cmd ->
    TransactionCmds era -> ExceptT TxCmdError IO ()
forall era. TransactionCmds era -> ExceptT TxCmdError IO ()
runTransactionCmds TransactionCmds era
cmd
      ExceptT TxCmdError IO ()
-> (ExceptT TxCmdError IO () -> ExceptT CmdError IO ())
-> ExceptT CmdError IO ()
forall a b. a -> (a -> b) -> b
& (TxCmdError -> CmdError)
-> ExceptT TxCmdError IO () -> ExceptT CmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT TxCmdError -> CmdError
CmdTransactionError