Skip to content

Commit

Permalink
Add '--outfile' option to the vote view command
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhammann committed Sep 27, 2023
1 parent 69f414f commit bd000cd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data AnyVoteViewCmd era
{ governanceVoteViewCmdYamlOutput :: Bool
, governanceVoteViewCmdEra :: ConwayEraOnwards era
, governanceVoteViewCmdVoteFile :: VoteFile In
, governanceVoteViewCmdOutputFile :: Maybe (File () Out)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pAnyVoteViewCmd cOnwards =
<$> pYamlOutput
<*> pure cOnwards
<*> pFileInDirection "vote-file" "Input filepath of the vote."
<*> pMaybeOutputFile
where
pYamlOutput :: Parser Bool
pYamlOutput =
Expand Down
17 changes: 8 additions & 9 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Vote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
18 changes: 16 additions & 2 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/Vote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bd000cd

Please sign in to comment.