Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatible command group #917

Merged
merged 14 commits into from
Oct 22, 2024
4 changes: 4 additions & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ library
Cardano.CLI.Commands.Key
Cardano.CLI.Commands.Node
Cardano.CLI.Commands.Ping
Cardano.CLI.Compatible.Commands
Cardano.CLI.Compatible.Governance
Cardano.CLI.Compatible.Run
Cardano.CLI.Compatible.Transaction
Cardano.CLI.Environment
Cardano.CLI.EraBased.Commands
Cardano.CLI.EraBased.Commands.Genesis
Expand Down
5 changes: 4 additions & 1 deletion cardano-cli/src/Cardano/CLI/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Cardano.CLI.Commands.Hash (HashCmds)
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.Legacy.Commands

Expand All @@ -23,7 +24,9 @@ data ClientCommand
| AddressCommand AddressCmds
| -- | Byron Related Commands
ByronCommand ByronCommand
| -- | Era agnostic hashing commands
| -- | Backward compatible commands for testing only
CompatibleCommands AnyCompatibleCommand
| -- | Era-agnostic hashing commands
HashCmds HashCmds
| -- | Era agnostic key commands
KeyCommands KeyCmds
Expand Down
75 changes: 75 additions & 0 deletions cardano-cli/src/Cardano/CLI/Compatible/Commands.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}

{-
This module is concerned with providing backwards compatible cli commands for our internal
testing needs. The intention is to restrict as much as possible which functionality we maintain backwards
compatibility for.
-}

module Cardano.CLI.Compatible.Commands
( AnyCompatibleCommand (..)
, CompatibleCommand (..)
, pAnyCompatibleCommand
, renderAnyCompatibleCommand
)
where

import Cardano.Api

import Cardano.CLI.Compatible.Governance
import Cardano.CLI.Compatible.Transaction
import Cardano.CLI.Environment
import Cardano.CLI.Parser

import Data.Foldable
import Data.Text
import Options.Applicative
import qualified Options.Applicative as Opt

data AnyCompatibleCommand where
AnyCompatibleCommand :: CompatibleCommand era -> AnyCompatibleCommand

renderAnyCompatibleCommand :: AnyCompatibleCommand -> Text
renderAnyCompatibleCommand = \case
AnyCompatibleCommand cmd -> renderCompatibleCommand cmd

data CompatibleCommand era
= CompatibleTransactionCmd (CompatibleTransactionCmds era)
| CompatibleGovernanceCmds (CompatibleGovernanceCmds era)

renderCompatibleCommand :: CompatibleCommand era -> Text
renderCompatibleCommand = \case
CompatibleTransactionCmd cmd -> renderCompatibleTransactionCmd cmd
CompatibleGovernanceCmds cmd -> renderCompatibleGovernanceCmds cmd

pAnyCompatibleCommand :: EnvCli -> Parser AnyCompatibleCommand
Copy link
Contributor

@carbolymer carbolymer Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parsers should go to cardano-cli/src/Cardano/CLI/Compatible/Options/Common.hs or something similar

Copy link
Contributor Author

@Jimbo4350 Jimbo4350 Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to mirror the previous EraBased directory structure. A Common module is fine but this parser is not common i.e it's called in one place.

pAnyCompatibleCommand envCli =
asum
[ -- Note, byron is ommitted because there is already a legacy command group for it.
subParser "shelley" $
Opt.info (AnyCompatibleCommand <$> pCompatibleCommand ShelleyBasedEraShelley envCli) $
Opt.progDesc "Shelley era commands"
, subParser "allegra" $
Opt.info (AnyCompatibleCommand <$> pCompatibleCommand ShelleyBasedEraAllegra envCli) $
Opt.progDesc "Allegra era commands"
, subParser "mary" $
Opt.info (AnyCompatibleCommand <$> pCompatibleCommand ShelleyBasedEraMary envCli) $
Opt.progDesc "Mary era commands"
, subParser "alonzo" $
Opt.info (AnyCompatibleCommand <$> pCompatibleCommand ShelleyBasedEraAlonzo envCli) $
Opt.progDesc "Alonzo era commands"
, subParser "babbage" $
Opt.info (AnyCompatibleCommand <$> pCompatibleCommand ShelleyBasedEraBabbage envCli) $
Opt.progDesc "Babbage era commands"
, subParser "conway" $
Opt.info (AnyCompatibleCommand <$> pCompatibleCommand ShelleyBasedEraConway envCli) $
Opt.progDesc "Conway era commands"
]

pCompatibleCommand :: ShelleyBasedEra era -> EnvCli -> Parser (CompatibleCommand era)
pCompatibleCommand era env =
asum
[ CompatibleTransactionCmd <$> pAllCompatibleTransactionCommands env era
, CompatibleGovernanceCmds <$> pCompatibleGovernanceCmds era
]
39 changes: 39 additions & 0 deletions cardano-cli/src/Cardano/CLI/Compatible/Governance.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Compatible.Governance
( CompatibleGovernanceCmds (..)
, pCompatibleGovernanceCmds
, renderCompatibleGovernanceCmds
, runCompatibleGovernanceCmds
)
where

import Cardano.Api

import Cardano.CLI.EraBased.Options.Governance
import Cardano.CLI.EraBased.Run.Governance
import Cardano.CLI.Types.Errors.CmdError

import Data.Foldable
import Data.Maybe
import Data.Text
import Options.Applicative

pCompatibleGovernanceCmds :: ShelleyBasedEra era -> Parser (CompatibleGovernanceCmds era)
pCompatibleGovernanceCmds sbe =
asum $ catMaybes [fmap CreateCompatibleProtocolUpdateCmd <$> pGovernanceCmds sbe]

-- TODO: After QA confirmms that the new compatibility commands meet their needs
-- we can remove all remaining legacy commands. We can also remove/move the exising
-- byron era commands under the new compatiblilty commands.
newtype CompatibleGovernanceCmds era
= CreateCompatibleProtocolUpdateCmd (GovernanceCmds era)

runCompatibleGovernanceCmds :: CompatibleGovernanceCmds era -> ExceptT CmdError IO ()
runCompatibleGovernanceCmds = \case
CreateCompatibleProtocolUpdateCmd cmd -> runGovernanceCmds cmd

renderCompatibleGovernanceCmds :: CompatibleGovernanceCmds era -> Text
renderCompatibleGovernanceCmds = \case
CreateCompatibleProtocolUpdateCmd cmd -> renderGovernanceCmds cmd
37 changes: 37 additions & 0 deletions cardano-cli/src/Cardano/CLI/Compatible/Run.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Compatible.Run
( CompatibleCmdError
, renderCompatibleCmdError
, runAnyCompatibleCommand
, runCompatibleCommand
)
where

import Cardano.Api

import Cardano.CLI.Compatible.Commands
import Cardano.CLI.Compatible.Governance
import Cardano.CLI.Compatible.Transaction
import Cardano.CLI.Render
import Cardano.CLI.Types.Errors.CmdError

import Data.Text (Text)

data CompatibleCmdError
= CompatibleTransactionError CompatibleTransactionError
| CompatibleGovernanceError CmdError

renderCompatibleCmdError :: Text -> CompatibleCmdError -> Doc ann
renderCompatibleCmdError cmdText = \case
CompatibleTransactionError e -> renderAnyCmdError cmdText prettyError e
CompatibleGovernanceError e -> renderCmdError cmdText e

runAnyCompatibleCommand :: AnyCompatibleCommand -> ExceptT CompatibleCmdError IO ()
runAnyCompatibleCommand (AnyCompatibleCommand cmd) = runCompatibleCommand cmd

runCompatibleCommand :: CompatibleCommand era -> ExceptT CompatibleCmdError IO ()
runCompatibleCommand (CompatibleTransactionCmd txCmd) =
firstExceptT CompatibleTransactionError $ runCompatibleTransactionCmd txCmd
runCompatibleCommand (CompatibleGovernanceCmds govCmd) =
firstExceptT CompatibleGovernanceError $ runCompatibleGovernanceCmds govCmd
Loading
Loading