Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Oct 22, 2024
1 parent 9197c62 commit 4f0fca7
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 137 deletions.
4 changes: 3 additions & 1 deletion cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ library
Cardano.CLI.Compatible.Run
Cardano.CLI.Compatible.Transaction
Cardano.CLI.Environment
Cardano.CLI.EraBased.Commands
Cardano.CLI.EraBased.Commands.Genesis
Cardano.CLI.EraBased.Commands.Governance
Cardano.CLI.EraBased.Commands.Governance.Actions
Expand All @@ -83,8 +82,10 @@ library
Cardano.CLI.EraBased.Commands.StakeAddress
Cardano.CLI.EraBased.Commands.StakePool
Cardano.CLI.EraBased.Commands.TextView
Cardano.CLI.EraBased.Commands.TopLevelCommands
Cardano.CLI.EraBased.Commands.Transaction
Cardano.CLI.EraBased.Options.Common
Cardano.CLI.EraBased.Options.Era
Cardano.CLI.EraBased.Options.Genesis
Cardano.CLI.EraBased.Options.Governance
Cardano.CLI.EraBased.Options.Governance.Actions
Expand All @@ -96,6 +97,7 @@ library
Cardano.CLI.EraBased.Options.StakeAddress
Cardano.CLI.EraBased.Options.StakePool
Cardano.CLI.EraBased.Options.TextView
Cardano.CLI.EraBased.Options.TopLevelCommands
Cardano.CLI.EraBased.Options.Transaction
Cardano.CLI.EraBased.Run
Cardano.CLI.EraBased.Run.Genesis
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Cardano.CLI.Commands.Key
import Cardano.CLI.Commands.Node
import Cardano.CLI.Commands.Ping (PingCmd (..))
import Cardano.CLI.Compatible.Commands
import Cardano.CLI.EraBased.Commands
import Cardano.CLI.EraBased.Commands.TopLevelCommands
import Cardano.CLI.Legacy.Commands

import Options.Applicative.Types (ParserInfo (..), ParserPrefs (..))
Expand Down
132 changes: 0 additions & 132 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands.hs

This file was deleted.

73 changes: 73 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/TopLevelCommands.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.EraBased.Commands.TopLevelCommands
( AnyEraCommand (..)
, Cmds (..)
, renderAnyEraCommand
, renderCmds
)
where

import Cardano.Api (ShelleyBasedEra (..))

import Cardano.CLI.Commands.Address
import Cardano.CLI.Commands.Key
import Cardano.CLI.Commands.Node
import Cardano.CLI.EraBased.Commands.Genesis
import Cardano.CLI.EraBased.Commands.Query
import Cardano.CLI.EraBased.Commands.StakeAddress
import Cardano.CLI.EraBased.Commands.StakePool
import Cardano.CLI.EraBased.Commands.TextView
import Cardano.CLI.EraBased.Commands.Transaction

import Cardano.CLI.EraBased.Options.Governance (GovernanceCmds,
renderGovernanceCmds)

import Data.Text (Text)
import Data.Typeable (Typeable)


-- TODO: ShelleyBasedEra era is not needed in AnyEraCommandOf
data AnyEraCommand where
AnyEraCommandOf :: Typeable era => ShelleyBasedEra era -> Cmds era -> AnyEraCommand

renderAnyEraCommand :: AnyEraCommand -> Text
renderAnyEraCommand = \case
AnyEraCommandOf _ cmd -> renderCmds cmd

data Cmds era
= AddressCmds AddressCmds
| KeyCmds KeyCmds
| GenesisCmds (GenesisCmds era)
| GovernanceCmds (GovernanceCmds era)
| NodeCmds NodeCmds
| QueryCmds (QueryCmds era)
| StakeAddressCmds (StakeAddressCmds era)
| StakePoolCmds (StakePoolCmds era)
| TextViewCmds (TextViewCmds era)
| TransactionCmds (TransactionCmds era)

renderCmds :: Cmds era -> Text
renderCmds = \case
AddressCmds cmd ->
renderAddressCmds cmd
KeyCmds cmd ->
renderKeyCmds cmd
GenesisCmds cmd ->
renderGenesisCmds cmd
GovernanceCmds cmd ->
renderGovernanceCmds cmd
NodeCmds cmd ->
renderNodeCmds cmd
QueryCmds cmd ->
renderQueryCmds cmd
StakeAddressCmds cmd ->
renderStakeAddressCmds cmd
StakePoolCmds cmd ->
renderStakePoolCmds cmd
TextViewCmds cmd ->
renderTextViewCmds cmd
TransactionCmds cmd ->
renderTransactionCmds cmd

47 changes: 47 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Era.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Cardano.CLI.EraBased.Options.Era
( pAnyEraCommand
)
where

import Cardano.Api (ShelleyBasedEra (..))


import Cardano.CLI.Environment
import Cardano.CLI.EraBased.Options.Common
import Cardano.CLI.EraBased.Options.TopLevelCommands
import Cardano.CLI.EraBased.Commands.TopLevelCommands

import Cardano.CLI.Parser

import Data.Foldable

import Options.Applicative (Parser)
import qualified Options.Applicative as Opt

pAnyEraCommand :: EnvCli -> Parser AnyEraCommand
pAnyEraCommand envCli =
asum
[ -- Note, byron is ommitted because there is already a legacy command group for it.

subParser "shelley" $
Opt.info (AnyEraCommandOf ShelleyBasedEraShelley <$> pCmds ShelleyBasedEraShelley envCli) $
Opt.progDesc ("Shelley era commands" <> deprecationText)
, subParser "allegra" $
Opt.info (AnyEraCommandOf ShelleyBasedEraAllegra <$> pCmds ShelleyBasedEraAllegra envCli) $
Opt.progDesc ("Allegra era commands" <> deprecationText)
, subParser "mary" $
Opt.info (AnyEraCommandOf ShelleyBasedEraMary <$> pCmds ShelleyBasedEraMary envCli) $
Opt.progDesc ("Mary era commands" <> deprecationText)
, subParser "alonzo" $
Opt.info (AnyEraCommandOf ShelleyBasedEraAlonzo <$> pCmds ShelleyBasedEraAlonzo envCli) $
Opt.progDesc ("Alonzo era commands" <> deprecationText)
, subParser "babbage" $
Opt.info (AnyEraCommandOf ShelleyBasedEraBabbage <$> pCmds ShelleyBasedEraBabbage envCli) $
Opt.progDesc ("Babbage era commands" <> deprecationText)
, subParser "conway" $
Opt.info (AnyEraCommandOf ShelleyBasedEraConway <$> pCmds ShelleyBasedEraConway envCli) $
Opt.progDesc "Conway era commands"
, subParser "latest" $
Opt.info (AnyEraCommandOf ShelleyBasedEraConway <$> pCmds ShelleyBasedEraConway envCli) $
Opt.progDesc "Latest era commands (Conway)"
]
41 changes: 41 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/TopLevelCommands.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Cardano.CLI.EraBased.Options.TopLevelCommands
( pCmds
)
where

import Cardano.Api (ShelleyBasedEra (..))

import Cardano.CLI.Environment
import Cardano.CLI.EraBased.Options.Genesis
import Cardano.CLI.EraBased.Options.Governance ( pGovernanceCmds
)
import Cardano.CLI.EraBased.Options.Query
import Cardano.CLI.EraBased.Commands.TopLevelCommands
import Cardano.CLI.EraBased.Options.StakeAddress
import Cardano.CLI.EraBased.Options.StakePool
import Cardano.CLI.EraBased.Options.TextView
import Cardano.CLI.EraBased.Options.Transaction
import Cardano.CLI.Options.Address
import Cardano.CLI.Options.Key
import Cardano.CLI.Options.Node

import Data.Foldable
import Data.Maybe

import Options.Applicative (Parser)

pCmds :: ShelleyBasedEra era -> EnvCli -> Parser (Cmds era)
pCmds era envCli = do
asum $
catMaybes
[ Just (AddressCmds <$> pAddressCmds envCli)
, Just (KeyCmds <$> pKeyCmds)
, fmap GenesisCmds <$> pGenesisCmds era envCli
, fmap GovernanceCmds <$> pGovernanceCmds era
, Just (NodeCmds <$> pNodeCmds)
, fmap QueryCmds <$> pQueryCmds era envCli
, fmap StakeAddressCmds <$> pStakeAddressCmds era envCli
, fmap StakePoolCmds <$> pStakePoolCmds era envCli
, fmap TextViewCmds <$> pTextViewCmds
, fmap TransactionCmds <$> pTransactionCmds era envCli
]
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ where

import Cardano.Api

import Cardano.CLI.EraBased.Commands
import Cardano.CLI.EraBased.Commands.TopLevelCommands
import Cardano.CLI.EraBased.Run.Genesis
import Cardano.CLI.EraBased.Run.Governance
import Cardano.CLI.EraBased.Run.Query
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ where
import Cardano.CLI.Byron.Parsers (backwardsCompatibilityCommands, parseByronCommands)
import Cardano.CLI.Compatible.Commands
import Cardano.CLI.Environment (EnvCli)
import Cardano.CLI.EraBased.Commands
import Cardano.CLI.EraBased.Options.Common
import Cardano.CLI.EraBased.Options.Era
import Cardano.CLI.Legacy.Options (parseLegacyCmds)
import Cardano.CLI.Options.Address
import Cardano.CLI.Options.Debug
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Cardano.CLI.Byron.Run (ByronClientCmdError, renderByronClientCm
import Cardano.CLI.Commands
import Cardano.CLI.Compatible.Commands
import Cardano.CLI.Compatible.Run
import Cardano.CLI.EraBased.Commands
import Cardano.CLI.EraBased.Commands.TopLevelCommands
import Cardano.CLI.EraBased.Run
import Cardano.CLI.Legacy.Commands
import Cardano.CLI.Legacy.Run (runLegacyCmds)
Expand Down

0 comments on commit 4f0fca7

Please sign in to comment.