Skip to content

Commit 6cf0724

Browse files
committed
Rollout --output-json-pretty and --output-yaml to these commands:
* `compatible conway governance action view` * `compatible conway governance vote view` * `conway governance action view` * `conway governance vote view` * `conway query leadership-schedule` * `debug transaction view` * `latest governance action view` * `latest governance vote view` * `latest query leadership-schedule` * `query leadership-schedule`
1 parent b08e8e3 commit 6cf0724

File tree

10 files changed

+67
-85
lines changed

10 files changed

+67
-85
lines changed

cardano-cli/src/Cardano/CLI/EraBased/Governance/Vote/Run.hs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@ import Cardano.Api.Shelley
2121
import Cardano.CLI.EraBased.Governance.Vote.Command qualified as Cmd
2222
import Cardano.CLI.EraBased.Script.Vote.Read
2323
import Cardano.CLI.EraIndependent.Hash.Internal.Common (carryHashChecks)
24+
import Cardano.CLI.Json.Encode qualified as Json
2425
import Cardano.CLI.Read (getHashFromStakePoolKeyHashSource)
2526
import Cardano.CLI.Type.Common
2627
import Cardano.CLI.Type.Error.CmdError
2728
import Cardano.CLI.Type.Error.GovernanceVoteCmdError
2829
import Cardano.CLI.Type.Governance
2930
import Cardano.CLI.Type.Key
3031

31-
import Data.Aeson.Encode.Pretty
3232
import Data.Function
33-
import Data.Yaml.Pretty qualified as Yaml
3433
import Vary qualified
3534

3635
runGovernanceVoteCmds
@@ -115,26 +114,14 @@ runGovernanceVoteViewCmd
115114
fmap fst $
116115
firstExceptT GovernanceVoteCmdReadVoteFileError $
117116
readVoteScriptWitness eon (voteFile, Nothing)
118-
firstExceptT GovernanceVoteCmdWriteError
119-
. newExceptT
120-
. ( outFormat
117+
118+
let output =
119+
outFormat
121120
& ( id
122-
. Vary.on (\FormatJson -> writeJson)
123-
. Vary.on (\FormatYaml -> writeYaml)
121+
. Vary.on (\FormatJson -> Json.encodeJson)
122+
. Vary.on (\FormatYaml -> Json.encodeYaml)
124123
$ Vary.exhaustiveCase
125124
)
126-
)
127-
. unVotingProcedures
128-
$ voteProcedures
129-
where
130-
writeJson :: ToJSON a => a -> IO (Either (FileError ()) ())
131-
writeJson =
132-
writeLazyByteStringOutput mOutFile
133-
. encodePretty'
134-
(defConfig{confCompare = compare})
125+
$ unVotingProcedures voteProcedures
135126

136-
writeYaml :: ToJSON a => a -> IO (Either (FileError ()) ())
137-
writeYaml =
138-
writeByteStringOutput mOutFile
139-
. Yaml.encodePretty
140-
(Yaml.setConfCompare compare Yaml.defConfig)
127+
firstExceptT GovernanceVoteCmdWriteError $ newExceptT $ writeLazyByteStringOutput mOutFile output

cardano-cli/src/Cardano/CLI/EraBased/Query/Command.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ data QueryLeadershipScheduleCmdArgs = QueryLeadershipScheduleCmdArgs
9494
, poolColdVerKeyFile :: !StakePoolKeyHashSource
9595
, vrkSkeyFp :: !(SigningKeyFile In)
9696
, whichSchedule :: !EpochLeadershipSchedule
97-
, format :: !(Vary [FormatJson, FormatText])
97+
, format :: !(Vary [FormatJson, FormatText, FormatYaml])
9898
, mOutFile :: !(Maybe (File () Out))
9999
}
100100
deriving (Generic, Show)

cardano-cli/src/Cardano/CLI/EraBased/Query/Option.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ pLeadershipScheduleCmd era envCli =
512512
"leadership-schedule query output"
513513
[ flagFormatJson & setDefault
514514
, flagFormatText
515+
, flagFormatYaml
515516
]
516517
<*> pMaybeOutputFile
517518

cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import Cardano.Binary qualified as CBOR
5252
import Cardano.CLI.EraBased.Genesis.Internal.Common
5353
import Cardano.CLI.EraBased.Query.Command qualified as Cmd
5454
import Cardano.CLI.Helper
55+
import Cardano.CLI.Json.Encode qualified as Json
5556
import Cardano.CLI.Read
5657
( getHashFromStakePoolKeyHashSource
5758
)
@@ -69,7 +70,7 @@ import Cardano.Slotting.Time (RelativeTime (..), toRelativeTime)
6970
import Control.Monad (forM, forM_, join)
7071
import Data.Aeson as Aeson
7172
import Data.Aeson qualified as A
72-
import Data.Aeson.Encode.Pretty (encodePretty)
73+
import Data.Aeson.Encode.Pretty qualified as Aeson
7374
import Data.Bifunctor (Bifunctor (..))
7475
import Data.ByteString.Base16.Lazy qualified as Base16
7576
import Data.ByteString.Char8 qualified as C8
@@ -92,7 +93,6 @@ import Data.Text.Encoding qualified as Text
9293
import Data.Text.IO qualified as T
9394
import Data.Text.Lazy.IO qualified as LT
9495
import Data.Time.Clock
95-
import Data.Yaml qualified as Yaml
9696
import GHC.Exts (IsList (..))
9797
import GHC.Generics
9898
import Lens.Micro ((^.))
@@ -160,7 +160,7 @@ runQueryProtocolParametersCmd
160160
firstExceptT QueryCmdWriteFileError . newExceptT $
161161
writeLazyByteStringOutput mOutFile' $
162162
shelleyBasedEraConstraints sbe $
163-
encodePretty pparams
163+
Aeson.encodePretty pparams
164164

165165
-- | Calculate the percentage sync rendered as text: @min 1 (tipTime/nowTime)@
166166
percentage
@@ -290,7 +290,7 @@ runQueryTipCmd
290290

291291
firstExceptT QueryCmdWriteFileError . newExceptT $
292292
writeLazyByteStringOutput mOutFile $
293-
encodePretty localStateOutput
293+
Aeson.encodePretty localStateOutput
294294

295295
-- | Query the UTxO, filtered by a given set of addresses, from a Shelley node
296296
-- via the local state query protocol.
@@ -387,7 +387,7 @@ runQueryKesPeriodInfoCmd
387387
renderOpCertNodeAndOnDiskCounterInformation (unFile nodeOpCertFp) counterInformation
388388

389389
let qKesInfoOutput = createQueryKesPeriodInfoOutput opCertIntervalInformation counterInformation eInfo gParams
390-
kesPeriodInfoJSON = encodePretty qKesInfoOutput
390+
kesPeriodInfoJSON = Aeson.encodePretty qKesInfoOutput
391391

392392
liftIO $ LBS.putStrLn kesPeriodInfoJSON
393393
forM_
@@ -681,7 +681,7 @@ runQueryTxMempoolCmd
681681
result <- liftIO $ queryTxMonitoringLocal nodeConnInfo localQuery
682682
firstExceptT QueryCmdWriteFileError . newExceptT $
683683
writeLazyByteStringOutput mOutFile $
684-
encodePretty result
684+
Aeson.encodePretty result
685685

686686
runQuerySlotNumberCmd
687687
:: ()
@@ -883,13 +883,14 @@ runQueryLedgerPeerSnapshot
883883
Left (bs :: LBS.ByteString) -> do
884884
firstExceptT QueryCmdHelpersError $ pPrintCBOR bs
885885
Right (snapshot :: LedgerPeerSnapshot) -> do
886-
outputContents <-
887-
outputFormat
888-
& ( id
889-
. Vary.on (\FormatJson -> pure $ encodePretty snapshot)
890-
. Vary.on (\FormatYaml -> pure $ LBS.fromStrict $ Yaml.encode snapshot)
891-
$ Vary.exhaustiveCase
892-
)
886+
let outputContents =
887+
outputFormat
888+
& ( id
889+
. Vary.on (\FormatJson -> Json.encodeJson)
890+
. Vary.on (\FormatYaml -> Json.encodeYaml)
891+
$ Vary.exhaustiveCase
892+
)
893+
$ snapshot
893894

894895
let writeOutputContents =
895896
case mOutFile of
@@ -1047,7 +1048,7 @@ writeStakeAddressInfo
10471048
)
10481049
mOutFile =
10491050
firstExceptT QueryCmdWriteFileError . newExceptT $
1050-
writeLazyByteStringOutput mOutFile (encodePretty $ jsonInfo sbe)
1051+
writeLazyByteStringOutput mOutFile (Aeson.encodePretty $ jsonInfo sbe)
10511052
where
10521053
jsonInfo :: ShelleyBasedEra era -> [Aeson.Value]
10531054
jsonInfo =
@@ -1114,7 +1115,7 @@ writeStakeSnapshots mOutFile qState = do
11141115
& onLeft (left . QueryCmdStakeSnapshotDecodeError)
11151116

11161117
-- Calculate the three pool and active stake values for the given pool
1117-
liftIO . maybe LBS.putStrLn (LBS.writeFile . unFile) mOutFile $ encodePretty snapshot
1118+
liftIO . maybe LBS.putStrLn (LBS.writeFile . unFile) mOutFile $ Aeson.encodePretty snapshot
11181119

11191120
-- | This function obtains the pool parameters, equivalent to the following jq query on the output of query ledger-state
11201121
-- .nesEs.esLState.lsDPState.dpsPState.psStakePoolParams.<pool_id>
@@ -1154,7 +1155,7 @@ writePoolState mOutFile serialisedCurrentEpochState = do
11541155

11551156
firstExceptT QueryCmdWriteFileError . newExceptT $
11561157
writeLazyByteStringOutput mOutFile $
1157-
encodePretty poolStates
1158+
Aeson.encodePretty poolStates
11581159

11591160
writeProtocolState
11601161
:: ShelleyBasedEra era
@@ -1195,7 +1196,7 @@ writeProtocolState sbe mOutFile ps@(ProtocolState pstate) =
11951196
decodePState ps' =
11961197
case decodeProtocolState ps' of
11971198
Left (bs, _) -> firstExceptT QueryCmdHelpersError $ pPrintCBOR bs
1198-
Right chainDepstate -> liftIO . LBS.putStrLn $ encodePretty chainDepstate
1199+
Right chainDepstate -> liftIO . LBS.putStrLn $ Aeson.encodePretty chainDepstate
11991200

12001201
writeFilteredUTxOs
12011202
:: Api.ShelleyBasedEra era
@@ -1211,7 +1212,7 @@ writeFilteredUTxOs sbe format mOutFile utxo =
12111212
$ format
12121213
& ( id
12131214
. Vary.on (\FormatCbor -> Base16.encode . CBOR.serialize $ toLedgerUTxO sbe utxo)
1214-
. Vary.on (\FormatJson -> encodePretty utxo)
1215+
. Vary.on (\FormatJson -> Json.encodeJson utxo)
12151216
. Vary.on (\FormatText -> strictTextToLazyBytestring $ filteredUTxOsToText sbe utxo)
12161217
$ Vary.exhaustiveCase
12171218
)
@@ -1347,7 +1348,7 @@ writeStakePools format mOutFile stakePools =
13471348
$ Vary.exhaustiveCase
13481349
)
13491350
writeJson =
1350-
encodePretty stakePools
1351+
Aeson.encodePretty stakePools
13511352
writeText =
13521353
LBS.unlines $
13531354
map (strictTextToLazyBytestring . serialiseToBech32) $
@@ -1368,7 +1369,7 @@ writeFormattedOutput format mOutFile value =
13681369
toWrite :: LBS.ByteString =
13691370
format
13701371
& ( id
1371-
. Vary.on (\FormatJson -> encodePretty value)
1372+
. Vary.on (\FormatJson -> Json.encodeJson value)
13721373
. Vary.on (\FormatText -> fromString . docToString $ pretty value)
13731374
$ Vary.exhaustiveCase
13741375
)
@@ -1416,7 +1417,7 @@ writeStakeDistribution format mOutFile stakeDistrib =
14161417
toWrite :: LBS.ByteString =
14171418
format
14181419
& ( id
1419-
. Vary.on (\FormatJson -> encodePretty stakeDistrib)
1420+
. Vary.on (\FormatJson -> Json.encodeJson stakeDistrib)
14201421
. Vary.on (\FormatText -> strictTextToLazyBytestring stakeDistributionText)
14211422
$ Vary.exhaustiveCase
14221423
)
@@ -1540,8 +1541,9 @@ runQueryLeadershipScheduleCmd
15401541
toWrite =
15411542
format
15421543
& ( id
1543-
. Vary.on (\FormatJson -> encodePretty $ leadershipScheduleToJson schedule eInfo start)
1544+
. Vary.on (\FormatJson -> Json.encodeJson $ leadershipScheduleToJson schedule eInfo start)
15441545
. Vary.on (\FormatText -> strictTextToLazyBytestring $ leadershipScheduleToText schedule eInfo start)
1546+
. Vary.on (\FormatYaml -> Json.encodeYaml $ leadershipScheduleToJson schedule eInfo start)
15451547
$ Vary.exhaustiveCase
15461548
)
15471549

@@ -1973,10 +1975,10 @@ writeOutput
19731975
-> b
19741976
-> ExceptT QueryCmdError IO ()
19751977
writeOutput mOutFile content = case mOutFile of
1976-
Nothing -> liftIO . LBS.putStrLn . encodePretty $ content
1978+
Nothing -> liftIO . LBS.putStrLn . Aeson.encodePretty $ content
19771979
Just (File f) ->
19781980
handleIOExceptT (QueryCmdWriteFileError . FileIOError f) $
1979-
LBS.writeFile f (encodePretty content)
1981+
LBS.writeFile f (Aeson.encodePretty content)
19801982

19811983
-- Helpers
19821984

cardano-cli/src/Cardano/CLI/Json/Friendly.hs

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
{-# HLINT ignore "Redundant bracket" #-}
1212
{-# HLINT ignore "Redundant id" #-}
13+
{-# HLINT ignore "Use let" #-}
1314

1415
-- | User-friendly pretty-printing for textual user interfaces (TUI)
1516
module Cardano.CLI.Json.Friendly
@@ -26,10 +27,7 @@ module Cardano.CLI.Json.Friendly
2627
--
2728
-- They are more low-level, but can be used in any context.
2829
-- The '*Impl' functions give you access to the Aeson representation
29-
-- of various structures. Then use 'friendlyBS' to format the Aeson
30-
-- values to a ByteString, in a manner consistent with the IO functions
31-
-- of this module.
32-
, friendlyBS
30+
-- of various structures.
3331
, friendlyTxImpl
3432
, friendlyTxBodyImpl
3533
, friendlyProposalImpl
@@ -55,20 +53,18 @@ import Cardano.Api.Shelley
5553
, toShelleyStakeCredential
5654
)
5755

56+
import Cardano.CLI.Json.Encode qualified as Json
5857
import Cardano.CLI.Orphan ()
5958
import Cardano.CLI.Type.Common
6059
import Cardano.CLI.Type.MonadWarning (MonadWarning, runWarningIO)
6160
import Cardano.Crypto.Hash (hashToTextAsHex)
6261

6362
import Data.Aeson (Value (..), object, toJSON, (.=))
6463
import Data.Aeson qualified as Aeson
65-
import Data.Aeson.Encode.Pretty qualified as Aeson
6664
import Data.Aeson.Key qualified as Aeson
6765
import Data.Aeson.KeyMap qualified as KeyMap
6866
import Data.Aeson.Types qualified as Aeson
69-
import Data.ByteString qualified as BS
7067
import Data.ByteString.Char8 qualified as BSC
71-
import Data.ByteString.Lazy qualified as LBS
7268
import Data.Char (isAscii)
7369
import Data.Function ((&))
7470
import Data.Functor ((<&>))
@@ -80,8 +76,6 @@ import Data.Text qualified as Text
8076
import Data.Typeable (Typeable)
8177
import Data.Vector qualified as Vector
8278
import Data.Yaml (array)
83-
import Data.Yaml.Pretty (setConfCompare)
84-
import Data.Yaml.Pretty qualified as Yaml
8579
import GHC.Exts (IsList (..))
8680
import GHC.Real (denominator)
8781
import GHC.Unicode (isAlphaNum)
@@ -95,33 +89,18 @@ friendly
9589
-> Maybe (File () Out)
9690
-> a
9791
-> m (Either (FileError e) ())
98-
friendly format mOutFile =
99-
format
100-
& ( id
101-
. Vary.on (\FormatJson -> writeLazyByteStringOutput mOutFile . Aeson.encodePretty' jsonConfig)
102-
. Vary.on (\FormatYaml -> writeByteStringOutput mOutFile . Yaml.encodePretty yamlConfig)
103-
$ Vary.exhaustiveCase
104-
)
105-
106-
friendlyBS
107-
:: ()
108-
=> Aeson.ToJSON a
109-
=> Vary [FormatJson, FormatYaml]
110-
-> a
111-
-> BS.ByteString
112-
friendlyBS format a =
113-
format
114-
& ( id
115-
. Vary.on (\FormatJson -> BS.concat . LBS.toChunks $ Aeson.encodePretty' jsonConfig a)
116-
. Vary.on (\FormatYaml -> Yaml.encodePretty yamlConfig a)
117-
$ Vary.exhaustiveCase
118-
)
119-
120-
jsonConfig :: Aeson.Config
121-
jsonConfig = Aeson.defConfig{Aeson.confCompare = compare}
122-
123-
yamlConfig :: Yaml.Config
124-
yamlConfig = Yaml.defConfig & setConfCompare compare
92+
friendly format mOutFile value = do
93+
output <-
94+
pure
95+
$ format
96+
& ( id
97+
. Vary.on (\FormatJson -> Json.encodeJson)
98+
. Vary.on (\FormatYaml -> Json.encodeYaml)
99+
$ Vary.exhaustiveCase
100+
)
101+
$ value
102+
103+
writeLazyByteStringOutput mOutFile output
125104

126105
friendlyTx
127106
:: MonadIO m

cardano-cli/test/cardano-cli-golden/Test/Golden/TxView.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,6 @@ hprop_golden_view_conway_proposal =
229229

230230
result <-
231231
execCardanoCLI
232-
["debug", "transaction", "view", "--tx-file", input </> "tx-proposal.json", "--output-json"]
232+
["debug", "transaction", "view", "--tx-file", input </> "tx-proposal.json"]
233233

234234
H.diffVsGoldenFile result (golden </> "tx-proposal.out.json")

cardano-cli/test/cardano-cli-golden/files/golden/help.cli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,10 @@ Usage: cardano-cli query leadership-schedule
429429
)
430430
--vrf-signing-key-file FILEPATH
431431
(--current | --next)
432-
[--output-json | --output-text]
432+
[ --output-json
433+
| --output-text
434+
| --output-yaml
435+
]
433436
[--out-file FILEPATH]
434437

435438
Get the slots the node is expected to mint a block in (advanced command)
@@ -2034,6 +2037,7 @@ Usage: cardano-cli conway query leadership-schedule
20342037
(--current | --next)
20352038
[ --output-json
20362039
| --output-text
2040+
| --output-yaml
20372041
]
20382042
[--out-file FILEPATH]
20392043

@@ -4274,6 +4278,7 @@ Usage: cardano-cli latest query leadership-schedule
42744278
(--current | --next)
42754279
[ --output-json
42764280
| --output-text
4281+
| --output-yaml
42774282
]
42784283
[--out-file FILEPATH]
42794284

cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_leadership-schedule.cli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Usage: cardano-cli conway query leadership-schedule
1818
(--current | --next)
1919
[ --output-json
2020
| --output-text
21+
| --output-yaml
2122
]
2223
[--out-file FILEPATH]
2324

@@ -58,5 +59,6 @@ Available options:
5859
--output-json Format leadership-schedule query output to JSON
5960
(default).
6061
--output-text Format leadership-schedule query output to TEXT.
62+
--output-yaml Format leadership-schedule query output to YAML.
6163
--out-file FILEPATH Optional output file. Default is to write to stdout.
6264
-h,--help Show this help text

0 commit comments

Comments
 (0)