Skip to content

Commit

Permalink
Merge pull request #521 from input-output-hk/smelc/fix-vote-view-outp…
Browse files Browse the repository at this point in the history
…ut-format

governance vote view: use `--output-format`, like other commands, instead of `--yaml`
  • Loading branch information
smelc committed Dec 14, 2023
2 parents c7f1e95 + 23d330e commit e20d931
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ data GovernanceActionViewCmdArgs era
= GovernanceActionViewCmdArgs
{ eon :: !(ConwayEraOnwards era)
, actionFile :: !(ProposalFile In)
, outFormat :: !GovernanceActionViewOutputFormat
, outFormat :: !ViewOutputFormat
, mOutFile :: !(Maybe (File () Out))
} deriving Show

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ data GovernanceVoteCreateCmdArgs era
data GovernanceVoteViewCmdArgs era
= GovernanceVoteViewCmdArgs
{ eon :: ConwayEraOnwards era
, yamlOutput :: Bool
, outFormat :: !ViewOutputFormat
, voteFile :: VoteFile In
, mOutFile :: Maybe (File () Out)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ newtype TransactionTxIdCmdArgs = TransactionTxIdCmdArgs
} deriving Show

data TransactionViewCmdArgs = TransactionViewCmdArgs
{ outputFormat :: !TxViewOutputFormat
{ outputFormat :: !ViewOutputFormat
, mOutFile :: !(Maybe (File () Out))
, inputTxBodyOrTxFile :: !InputTxBodyOrTxFile
} deriving Show
Expand Down
31 changes: 15 additions & 16 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,28 +1566,27 @@ pPoolIdOutputFormat =
, Opt.value IdOutputFormatBech32
]

pTxViewOutputFormat :: Parser TxViewOutputFormat
pTxViewOutputFormat =
Opt.option readTxViewOutputFormat $ mconcat
[ Opt.long "output-format"
, Opt.metavar "STRING"
, Opt.help $ mconcat
[ "Optional transaction view output format. Accepted output formats are \"json\" "
, "and \"yaml\" (default is \"json\")."
]
, Opt.value TxViewOutputFormatJson
]
pTxViewOutputFormat :: Parser ViewOutputFormat
pTxViewOutputFormat = pViewOutputFormat "transaction"

pGovernanceActionViewOutputFormat :: Parser ViewOutputFormat
pGovernanceActionViewOutputFormat = pViewOutputFormat "governance action"

pGovernanceVoteViewOutputFormat :: Parser ViewOutputFormat
pGovernanceVoteViewOutputFormat = pViewOutputFormat "governance vote"

pGovernanceActionViewOutputFormat :: Parser GovernanceActionViewOutputFormat
pGovernanceActionViewOutputFormat =
Opt.option readGovernanceActionViewOutputFormat $ mconcat
-- | @pViewOutputFormat kind@ is a parser to specify in which format
-- to view some data (json or yaml). @what@ is the kind of data considered.
pViewOutputFormat :: String -> Parser ViewOutputFormat
pViewOutputFormat kind =
Opt.option (readViewOutputFormat kind) $ mconcat
[ Opt.long "output-format"
, Opt.metavar "STRING"
, Opt.help $ mconcat
[ "Optional governance action view output format. Accepted output formats are \"json\" "
[ "Optional ", kind ," view output format. Accepted output formats are \"json\" "
, "and \"yaml\" (default is \"json\")."
]
, Opt.value GovernanceActionViewOutputFormatJson
, Opt.value ViewOutputFormatJson
]

pMaybeOutputFile :: Parser (Maybe (File content Out))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ pGovernanceVoteViewCmd era = do
pGovernanceVoteViewCmdArgs :: ConwayEraOnwards era -> Parser (GovernanceVoteViewCmdArgs era)
pGovernanceVoteViewCmdArgs cOnwards =
GovernanceVoteViewCmdArgs cOnwards
<$> pYamlOutput
<$> pGovernanceVoteViewOutputFormat
<*> pFileInDirection "vote-file" "Input filepath of the vote."
<*> pMaybeOutputFile
where
pYamlOutput :: Parser Bool
pYamlOutput =
Opt.switch
( Opt.long "yaml"
<> Opt.help "Output vote in YAML format (and not JSON)."
)
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ runGovernanceActionViewCmd
firstExceptT GovernanceActionsCmdWriteFileError . newExceptT $
friendlyProposal
(case outFormat of
GovernanceActionViewOutputFormatJson -> FriendlyJson
GovernanceActionViewOutputFormatYaml -> FriendlyYaml)
ViewOutputFormatJson -> FriendlyJson
ViewOutputFormatYaml -> FriendlyYaml)
mOutFile
eon
proposal
Expand Down
14 changes: 9 additions & 5 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Vote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ runGovernanceVoteViewCmd :: ()
runGovernanceVoteViewCmd
Cmd.GovernanceVoteViewCmdArgs
{ eon
, yamlOutput
, outFormat
, voteFile
, mOutFile
} = do
Expand All @@ -96,8 +96,12 @@ runGovernanceVoteViewCmd
readVotingProceduresFile eon voteFile
firstExceptT GovernanceVoteCmdWriteError .
newExceptT .
(if yamlOutput
then writeByteStringOutput mOutFile . Yaml.encodePretty (Yaml.setConfCompare compare Yaml.defConfig)
else writeLazyByteStringOutput mOutFile . encodePretty' (defConfig {confCompare = compare})) .
(case outFormat of
ViewOutputFormatYaml ->
writeByteStringOutput mOutFile . Yaml.encodePretty
(Yaml.setConfCompare compare Yaml.defConfig)
ViewOutputFormatJson ->
writeLazyByteStringOutput mOutFile . encodePretty'
(defConfig {confCompare = compare})) .
unVotingProcedures $
voteProcedures
voteProcedures
8 changes: 4 additions & 4 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,15 +1189,15 @@ runTransactionViewCmd
-- is arguably not part of the transaction body.
firstExceptT TxCmdWriteFileError . newExceptT $
case outputFormat of
TxViewOutputFormatYaml -> friendlyTxBody FriendlyYaml mOutFile (toCardanoEra era) txbody
TxViewOutputFormatJson -> friendlyTxBody FriendlyJson mOutFile (toCardanoEra era) txbody
ViewOutputFormatYaml -> friendlyTxBody FriendlyYaml mOutFile (toCardanoEra era) txbody
ViewOutputFormatJson -> friendlyTxBody FriendlyJson mOutFile (toCardanoEra era) txbody
InputTxFile (File txFilePath) -> do
txFile <- liftIO $ fileOrPipe txFilePath
InAnyShelleyBasedEra era tx <- lift (readFileTx txFile) & onLeft (left . TxCmdCddlError)
firstExceptT TxCmdWriteFileError . newExceptT $
case outputFormat of
TxViewOutputFormatYaml -> friendlyTx FriendlyYaml mOutFile (toCardanoEra era) tx
TxViewOutputFormatJson -> friendlyTx FriendlyJson mOutFile (toCardanoEra era) tx
ViewOutputFormatYaml -> friendlyTx FriendlyYaml mOutFile (toCardanoEra era) tx
ViewOutputFormatJson -> friendlyTx FriendlyJson mOutFile (toCardanoEra era) tx

-- ----------------------------------------------------------------------------
-- Witness commands
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Legacy/Commands/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ data LegacyTransactionCmds
| TransactionTxIdCmd
InputTxBodyOrTxFile
| TransactionViewCmd
TxViewOutputFormat
ViewOutputFormat
(Maybe (File () Out))
InputTxBodyOrTxFile

Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Legacy/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ runLegacyTransactionTxIdCmd txfile =
txfile
)

runLegacyTransactionViewCmd :: TxViewOutputFormat -> Maybe (File () Out) -> InputTxBodyOrTxFile -> ExceptT TxCmdError IO ()
runLegacyTransactionViewCmd :: ViewOutputFormat -> Maybe (File () Out) -> InputTxBodyOrTxFile -> ExceptT TxCmdError IO ()
runLegacyTransactionViewCmd
yamlOrJson
mOutFile
Expand Down
29 changes: 12 additions & 17 deletions cardano-cli/src/Cardano/CLI/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
module Cardano.CLI.Parser
( readerFromAttoParser
, readFractionAsRational
, readGovernanceActionViewOutputFormat
, readKeyOutputFormat
, readIdOutputFormat
, readTxViewOutputFormat
, readRational
, readRationalUnitInterval
, readStringOfMaxLength
, readViewOutputFormat
, readURIOfMaxLength
, eDNSName
, readGovernanceActionViewOutputFormat
) where

import Cardano.CLI.Types.Common
Expand Down Expand Up @@ -51,30 +52,24 @@ readKeyOutputFormat = do
, ". Accepted output formats are \"text-envelope\" and \"bech32\"."
]

readTxViewOutputFormat :: Opt.ReadM TxViewOutputFormat
readTxViewOutputFormat = do
s <- Opt.str @String
case s of
"json" -> pure TxViewOutputFormatJson
"yaml" -> pure TxViewOutputFormatYaml
_ ->
fail $ mconcat
[ "Invalid transaction view output format: " <> show s
, ". Accepted output formats are \"json\" and \"yaml\"."
]
readTxViewOutputFormat :: Opt.ReadM ViewOutputFormat
readTxViewOutputFormat = readViewOutputFormat "transaction"

readGovernanceActionViewOutputFormat :: Opt.ReadM GovernanceActionViewOutputFormat
readGovernanceActionViewOutputFormat = do
readViewOutputFormat :: String -> Opt.ReadM ViewOutputFormat
readViewOutputFormat kind = do
s <- Opt.str @String
case s of
"json" -> pure GovernanceActionViewOutputFormatJson
"yaml" -> pure GovernanceActionViewOutputFormatYaml
"json" -> pure ViewOutputFormatJson
"yaml" -> pure ViewOutputFormatYaml
_ ->
fail $ mconcat
[ "Invalid governance action view output format: " <> show s
[ "Invalid ", kind, " output format: " <> show s
, ". Accepted output formats are \"json\" and \"yaml\"."
]

readGovernanceActionViewOutputFormat :: Opt.ReadM ViewOutputFormat
readGovernanceActionViewOutputFormat = readViewOutputFormat "governance action view"

readURIOfMaxLength :: Int -> Opt.ReadM Text
readURIOfMaxLength maxLen =
Text.pack <$> readStringOfMaxLength maxLen
Expand Down
14 changes: 4 additions & 10 deletions cardano-cli/src/Cardano/CLI/Types/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module Cardano.CLI.Types.Common
, GenesisDir(..)
, GenesisFile (..)
, GenesisKeyFile(..)
, GovernanceActionViewOutputFormat(..)
, InputTxBodyOrTxFile (..)
, KeyOutputFormat(..)
, MetadataFile(..)
Expand Down Expand Up @@ -70,10 +69,10 @@ module Cardano.CLI.Types.Common
, TxOutCount(..)
, TxOutDatumAnyEra (..)
, TxShelleyWitnessCount(..)
, TxViewOutputFormat(..)
, UpdateProposalFile (..)
, VerificationKeyBase64(..)
, VerificationKeyFile
, ViewOutputFormat(..)
, VoteUrl(..)
, VoteText(..)
, VoteHashSource(..)
Expand Down Expand Up @@ -459,14 +458,9 @@ data TxMempoolQuery =
| TxMempoolQueryInfo
deriving Show

data TxViewOutputFormat
= TxViewOutputFormatJson
| TxViewOutputFormatYaml
deriving Show

data GovernanceActionViewOutputFormat
= GovernanceActionViewOutputFormatJson
| GovernanceActionViewOutputFormatYaml
data ViewOutputFormat
= ViewOutputFormatJson
| ViewOutputFormatYaml
deriving Show
--
-- Shelley CLI flag/option data types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ hprop_golden_governance_governance_vote_view_yaml =
voteViewGold <- H.note "test/cardano-cli-golden/files/golden/governance/vote/voteViewYAML"
voteView <- execCardanoCLI
[ "conway", "governance", "vote", "view"
, "--yaml"
, "--output-format", "yaml"
, "--vote-file", voteFile
]

Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -6399,7 +6399,7 @@ Usage: cardano-cli conway governance vote create (--yes | --no | --abstain)

Vote creation.

Usage: cardano-cli conway governance vote view [--yaml]
Usage: cardano-cli conway governance vote view [--output-format STRING]
--vote-file FILE
[--out-file FILE]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Usage: cardano-cli conway governance vote view [--yaml]
Usage: cardano-cli conway governance vote view [--output-format STRING]
--vote-file FILE
[--out-file FILE]

Vote viewing.

Available options:
--yaml Output vote in YAML format (and not JSON).
--output-format STRING Optional governance vote view output format. Accepted
output formats are "json" and "yaml" (default is
"json").
--vote-file FILE Input filepath of the vote.
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text

0 comments on commit e20d931

Please sign in to comment.