From bd000cdb689294f9e7b97e07a069757e1df341a6 Mon Sep 17 00:00:00 2001 From: Carl Hammann Date: Wed, 27 Sep 2023 15:12:51 +0200 Subject: [PATCH] Add '--outfile' option to the vote view command --- .../CLI/EraBased/Commands/Governance/Vote.hs | 1 + .../CLI/EraBased/Options/Governance/Vote.hs | 1 + .../CLI/EraBased/Run/Governance/Vote.hs | 17 ++++++++--------- .../Test/Golden/Governance/Vote.hs | 18 ++++++++++++++++-- .../cardano-cli-golden/files/golden/help.cli | 4 +++- .../help/conway_governance_vote_view.cli | 5 ++++- 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/Vote.hs b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/Vote.hs index e85814e21b..b8ecf4e68e 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/Vote.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/Vote.hs @@ -25,6 +25,7 @@ data AnyVoteViewCmd era { governanceVoteViewCmdYamlOutput :: Bool , governanceVoteViewCmdEra :: ConwayEraOnwards era , governanceVoteViewCmdVoteFile :: VoteFile In + , governanceVoteViewCmdOutputFile :: Maybe (File () Out) } diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/Vote.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/Vote.hs index e397295cfc..6646f844a6 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/Vote.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/Vote.hs @@ -69,6 +69,7 @@ pAnyVoteViewCmd cOnwards = <$> pYamlOutput <*> pure cOnwards <*> pFileInDirection "vote-file" "Input filepath of the vote." + <*> pMaybeOutputFile where pYamlOutput :: Parser Bool pYamlOutput = diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Vote.hs b/cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Vote.hs index d4dee5bb00..83654c6161 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Vote.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Vote.hs @@ -20,13 +20,10 @@ import Cardano.CLI.Types.Governance import Cardano.CLI.Types.Key import Cardano.Ledger.Keys (coerceKeyRole) -import Control.Monad.Trans (liftIO) import Control.Monad.Trans.Except import Control.Monad.Trans.Except.Extra import Data.Aeson.Encode.Pretty import Data.Bifunctor -import qualified Data.ByteString as BS -import qualified Data.ByteString.Lazy as LBS import Data.Function import qualified Data.Yaml.Pretty as Yaml @@ -37,8 +34,8 @@ runGovernanceVoteCmds = \case GovernanceVoteCreateCmd anyVote -> runGovernanceVoteCreateCmd anyVote & firstExceptT CmdGovernanceVoteError - GovernanceVoteViewCmd (AnyVoteViewCmd printYaml w voteFile) -> - runGovernanceVoteViewCmd printYaml w voteFile + GovernanceVoteViewCmd (AnyVoteViewCmd printYaml w voteFile mOutFile) -> + runGovernanceVoteViewCmd printYaml w voteFile mOutFile & firstExceptT CmdGovernanceVoteError runGovernanceVoteCreateCmd @@ -86,16 +83,18 @@ runGovernanceVoteViewCmd :: Bool -> ConwayEraOnwards era -> VoteFile In + -> Maybe (File () Out) -> ExceptT GovernanceVoteCmdError IO () -runGovernanceVoteViewCmd outputYaml w fp = do +runGovernanceVoteViewCmd outputYaml w fp mOutFile = do let sbe = conwayEraOnwardsToShelleyBasedEra w shelleyBasedEraConstraints sbe $ do voteProcedures <- firstExceptT GovernanceVoteCmdReadVoteFileError . newExceptT $ readVotingProceduresFile w fp - liftIO . + firstExceptT GovernanceVoteCmdWriteError . + newExceptT . (if outputYaml - then BS.putStr . Yaml.encodePretty (Yaml.setConfCompare compare Yaml.defConfig) - else LBS.putStr . encodePretty' (defConfig {confCompare = compare})) . + then writeByteStringOutput mOutFile . Yaml.encodePretty (Yaml.setConfCompare compare Yaml.defConfig) + else writeLazyByteStringOutput mOutFile . encodePretty' (defConfig {confCompare = compare})) . unVotingProcedures $ voteProcedures diff --git a/cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Vote.hs b/cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Vote.hs index e279307a37..334e6a8ac6 100644 --- a/cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Vote.hs +++ b/cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Vote.hs @@ -28,8 +28,8 @@ hprop_golden_governance_governance_vote_create = H.diffFileVsGoldenFile voteFile voteGold -hprop_golden_governance_governance_vote_view_json :: Property -hprop_golden_governance_governance_vote_view_json = +hprop_golden_governance_governance_vote_view_json_stdout :: Property +hprop_golden_governance_governance_vote_view_json_stdout = propertyOnce $ do voteFile <- noteInputFile "test/cardano-cli-golden/files/golden/governance/vote/vote" voteViewGold <- H.note "test/cardano-cli-golden/files/golden/governance/vote/voteViewJSON" @@ -40,6 +40,20 @@ hprop_golden_governance_governance_vote_view_json = H.diffVsGoldenFile voteView voteViewGold +hprop_golden_governance_governance_vote_view_json_outfile :: Property +hprop_golden_governance_governance_vote_view_json_outfile = + propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do + voteFile <- noteInputFile "test/cardano-cli-golden/files/golden/governance/vote/vote" + voteViewFile <- H.noteTempFile tempDir "voteView" + voteViewGold <- H.note "test/cardano-cli-golden/files/golden/governance/vote/voteViewJSON" + void $ execCardanoCLI + [ "conway", "governance", "vote", "view" + , "--vote-file", voteFile + , "--out-file", voteViewFile + ] + + H.diffFileVsGoldenFile voteViewFile voteViewGold + hprop_golden_governance_governance_vote_view_yaml :: Property hprop_golden_governance_governance_vote_view_yaml = propertyOnce $ do diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help.cli index cba9779c0e..702d45b45d 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help.cli @@ -6739,7 +6739,9 @@ Usage: cardano-cli conway governance vote create (--yes | --no | --abstain) Vote creation. -Usage: cardano-cli conway governance vote view [--yaml] --vote-file FILE +Usage: cardano-cli conway governance vote view [--yaml] + --vote-file FILE + [--out-file FILE] Vote viewing. diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_governance_vote_view.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_governance_vote_view.cli index 779da04705..35b4b36607 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_governance_vote_view.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_governance_vote_view.cli @@ -1,8 +1,11 @@ -Usage: cardano-cli conway governance vote view [--yaml] --vote-file FILE +Usage: cardano-cli conway governance vote view [--yaml] + --vote-file FILE + [--out-file FILE] Vote viewing. Available options: --yaml Output vote in YAML format (and not 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