Skip to content

Commit b7eccd3

Browse files
committed
Consistent output for the query ledger-state command
1 parent c9ba6dd commit b7eccd3

File tree

7 files changed

+50
-35
lines changed

7 files changed

+50
-35
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ data QueryUTxOCmdArgs = QueryUTxOCmdArgs
142142

143143
data QueryLedgerStateCmdArgs = QueryLedgerStateCmdArgs
144144
{ commons :: !QueryCommons
145+
, outputFormat :: !(Vary [FormatJson, FormatText])
145146
, mOutFile :: !(Maybe (File () Out))
146147
}
147148
deriving (Generic, Show)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ pQueryLedgerStateCmd era envCli =
415415
fmap QueryLedgerStateCmd $
416416
QueryLedgerStateCmdArgs
417417
<$> pQueryCommons era envCli
418+
<*> pFormatFlags
419+
"ledger-state query output"
420+
[ flagFormatJson & setDefault
421+
, flagFormatText
422+
]
418423
<*> pMaybeOutputFile
419424

420425
pQueryLedgerPeerSnapshotCmd :: ShelleyBasedEra era -> EnvCli -> Parser (QueryCmds era)

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

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -799,42 +799,39 @@ runQueryLedgerStateCmd
799799
Nothing -> pure LBS.putStr
800800
Just (File fpath) -> pure $ LBS.writeFile fpath
801801

802-
join $
803-
lift
804-
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
805-
AnyCardanoEra era <- easyRunQueryCurrentEra
806-
807-
sbe <-
808-
requireShelleyBasedEra era
809-
& onNothing (left QueryCmdByronEra)
810-
811-
result <- easyRunQuery (queryDebugLedgerState sbe)
812-
813-
-- format
814-
-- & ( id
815-
-- . Vary.on (\FormatCbor -> LBS.fromStrict . Base16.encode . CBOR.serialize' $ toLedgerUTxO sbe utxo)
816-
-- . Vary.on (\FormatJson -> encodePretty utxo)
817-
-- . Vary.on (\FormatText -> strictTextToLazyBytestring $ filteredUTxOsToText sbe utxo)
818-
-- $ Vary.exhaustiveCase
819-
-- )
802+
contents <-
803+
join $
804+
lift
805+
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
806+
AnyCardanoEra era <- easyRunQueryCurrentEra
807+
808+
sbe <-
809+
requireShelleyBasedEra era
810+
& onNothing (left QueryCmdByronEra)
811+
812+
result <- easyRunQuery (queryDebugLedgerState sbe)
813+
814+
pure $
815+
shelleyBasedEraConstraints sbe $
816+
outputFormat
817+
& ( id
818+
. Vary.on
819+
( \FormatJson -> case decodeDebugLedgerState result of
820+
Left (bs, _decoderError) -> firstExceptT QueryCmdHelpersError $ cborToTextByteString bs
821+
Right ledgerState -> pure $ Aeson.encode ledgerState <> "\n"
822+
)
823+
. Vary.on
824+
( \FormatText ->
825+
let SerialisedDebugLedgerState serLedgerState = result
826+
in pure $ unSerialised serLedgerState
827+
)
828+
$ Vary.exhaustiveCase
829+
)
830+
)
831+
& onLeft (left . QueryCmdAcquireFailure)
832+
& onLeft left
820833

821-
pure $ do
822-
shelleyBasedEraConstraints sbe $
823-
case mOutFile of
824-
Nothing ->
825-
case decodeDebugLedgerState result of
826-
Left (bs, _decoderError) -> do
827-
output <- firstExceptT QueryCmdHelpersError $ cborToTextByteString bs
828-
liftIO $ writeOutputContents output
829-
Right ledgerState ->
830-
liftIO $ writeOutputContents $ Aeson.encode ledgerState <> "\n"
831-
Just (File _) ->
832-
case result of
833-
SerialisedDebugLedgerState serLedgerState ->
834-
liftIO $ writeOutputContents $ unSerialised serLedgerState
835-
)
836-
& onLeft (left . QueryCmdAcquireFailure)
837-
& onLeft left
834+
liftIO $ writeOutputContents contents
838835

839836
runQueryLedgerPeerSnapshot
840837
:: ()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ Usage: cardano-cli query ledger-state [--cardano-mode [--epoch-slots SLOTS]]
371371
(--mainnet | --testnet-magic NATURAL)
372372
--socket-path SOCKET_PATH
373373
[--volatile-tip | --immutable-tip]
374+
[--output-json | --output-text]
374375
[--out-file FILEPATH]
375376

376377
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
@@ -2062,6 +2063,7 @@ Usage: cardano-cli conway query ledger-state
20622063
[ --volatile-tip
20632064
| --immutable-tip
20642065
]
2066+
[--output-json | --output-text]
20652067
[--out-file FILEPATH]
20662068

20672069
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
@@ -4298,6 +4300,7 @@ Usage: cardano-cli latest query ledger-state
42984300
[ --volatile-tip
42994301
| --immutable-tip
43004302
]
4303+
[--output-json | --output-text]
43014304
[--out-file FILEPATH]
43024305

43034306
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Usage: cardano-cli conway query ledger-state
88
[ --volatile-tip
99
| --immutable-tip
1010
]
11+
[--output-json | --output-text]
1112
[--out-file FILEPATH]
1213

1314
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
@@ -30,5 +31,7 @@ Available options:
3031
--volatile-tip Use the volatile tip as a target. (This is the
3132
default)
3233
--immutable-tip Use the immutable tip as a target.
34+
--output-json Format ledger-state query output to JSON (default).
35+
--output-text Format ledger-state query output to TEXT.
3336
--out-file FILEPATH Optional output file. Default is to write to stdout.
3437
-h,--help Show this help text

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Usage: cardano-cli latest query ledger-state
88
[ --volatile-tip
99
| --immutable-tip
1010
]
11+
[--output-json | --output-text]
1112
[--out-file FILEPATH]
1213

1314
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
@@ -30,5 +31,7 @@ Available options:
3031
--volatile-tip Use the volatile tip as a target. (This is the
3132
default)
3233
--immutable-tip Use the immutable tip as a target.
34+
--output-json Format ledger-state query output to JSON (default).
35+
--output-text Format ledger-state query output to TEXT.
3336
--out-file FILEPATH Optional output file. Default is to write to stdout.
3437
-h,--help Show this help text

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Usage: cardano-cli query ledger-state [--cardano-mode [--epoch-slots SLOTS]]
22
(--mainnet | --testnet-magic NATURAL)
33
--socket-path SOCKET_PATH
44
[--volatile-tip | --immutable-tip]
5+
[--output-json | --output-text]
56
[--out-file FILEPATH]
67

78
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
@@ -24,5 +25,7 @@ Available options:
2425
--volatile-tip Use the volatile tip as a target. (This is the
2526
default)
2627
--immutable-tip Use the immutable tip as a target.
28+
--output-json Format ledger-state query output to JSON (default).
29+
--output-text Format ledger-state query output to TEXT.
2730
--out-file FILEPATH Optional output file. Default is to write to stdout.
2831
-h,--help Show this help text

0 commit comments

Comments
 (0)