Skip to content

Commit c1d7502

Browse files
committed
Standardise on default output format of json for 'query ledger-peer-snapshot' command. Also support json-pretty and yaml output.
1 parent 79aa303 commit c1d7502

File tree

7 files changed

+82
-32
lines changed

7 files changed

+82
-32
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
@@ -148,6 +148,7 @@ data QueryLedgerStateCmdArgs = QueryLedgerStateCmdArgs
148148

149149
data QueryLedgerPeerSnapshotCmdArgs = QueryLedgerPeerSnapshotCmdArgs
150150
{ commons :: !QueryCommons
151+
, outputFormat :: !(Vary [FormatJson, FormatJsonPretty, FormatYaml])
151152
, mOutFile :: !(Maybe (File () Out))
152153
}
153154
deriving (Generic, Show)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,12 @@ pQueryLedgerPeerSnapshotCmd era envCli =
422422
fmap QueryLedgerPeerSnapshotCmd $
423423
QueryLedgerPeerSnapshotCmdArgs
424424
<$> pQueryCommons era envCli
425+
<*> pFormatFlags
426+
"ledger-peer-snapshot output"
427+
[ flagFormatJson & setDefault
428+
, flagFormatJsonPretty
429+
, flagFormatYaml
430+
]
425431
<*> pMaybeOutputFile
426432

427433
pQueryProtocolStateCmd :: ShelleyBasedEra era -> EnvCli -> Parser (QueryCmds era)

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

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import Data.Text.Encoding qualified as Text
9292
import Data.Text.IO qualified as T
9393
import Data.Text.Lazy.IO qualified as LT
9494
import Data.Time.Clock
95+
import Data.Yaml qualified as Yaml
9596
import GHC.Exts (IsList (..))
9697
import GHC.Generics
9798
import Lens.Micro ((^.))
@@ -821,25 +822,51 @@ runQueryLedgerPeerSnapshot
821822
{ Cmd.nodeConnInfo
822823
, Cmd.target
823824
}
825+
, Cmd.outputFormat
824826
, Cmd.mOutFile
825827
} = do
826-
join $
827-
lift
828-
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
829-
AnyCardanoEra era <-
830-
lift queryCurrentEra
831-
& onLeft (left . QueryCmdUnsupportedNtcVersion)
832-
833-
sbe <-
834-
requireShelleyBasedEra era
835-
& onNothing (left QueryCmdByronEra)
828+
result <-
829+
join $
830+
lift
831+
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
832+
AnyCardanoEra era <-
833+
lift queryCurrentEra
834+
& onLeft (left . QueryCmdUnsupportedNtcVersion)
835+
836+
sbe <-
837+
requireShelleyBasedEra era
838+
& onNothing (left QueryCmdByronEra)
839+
840+
result <- easyRunQuery (queryLedgerPeerSnapshot sbe)
841+
842+
pure $
843+
shelleyBasedEraConstraints sbe $
844+
case decodeBigLedgerPeerSnapshot result of
845+
Left (bs, _decoderError) -> pure $ Left bs
846+
Right snapshot -> pure $ Right snapshot
847+
)
848+
& onLeft (left . QueryCmdAcquireFailure)
849+
& onLeft left
850+
851+
case result of
852+
Left (bs :: LBS.ByteString) -> do
853+
firstExceptT QueryCmdHelpersError $ pPrintCBOR bs
854+
Right (snapshot :: LedgerPeerSnapshot) -> do
855+
outputContents <-
856+
outputFormat
857+
& ( id
858+
. Vary.on (\FormatJson -> pure $ Aeson.encode snapshot)
859+
. Vary.on (\FormatJsonPretty -> pure $ encodePretty snapshot)
860+
. Vary.on (\FormatYaml -> pure $ LBS.fromStrict $ Yaml.encode snapshot)
861+
$ Vary.exhaustiveCase
862+
)
836863

837-
result <- easyRunQuery (queryLedgerPeerSnapshot sbe)
864+
let writeOutputContents =
865+
case mOutFile of
866+
Nothing -> liftIO . LBS.putStrLn
867+
Just (File outFile) -> liftIO . LBS.writeFile outFile
838868

839-
pure $ shelleyBasedEraConstraints sbe (writeLedgerPeerSnapshot mOutFile) result
840-
)
841-
& onLeft (left . QueryCmdAcquireFailure)
842-
& onLeft left
869+
writeOutputContents outputContents
843870

844871
runQueryProtocolStateCmd
845872
:: ()
@@ -1065,23 +1092,6 @@ writeLedgerState mOutFile qState@(SerialisedDebugLedgerState serLedgerState) =
10651092
LBS.writeFile fpath $
10661093
unSerialised serLedgerState
10671094

1068-
-- | Writes JSON-encoded big ledger peer snapshot
1069-
writeLedgerPeerSnapshot
1070-
:: Maybe (File () Out)
1071-
-> Serialised LedgerPeerSnapshot
1072-
-> ExceptT QueryCmdError IO ()
1073-
writeLedgerPeerSnapshot mOutPath serBigLedgerPeerSnapshot = do
1074-
case decodeBigLedgerPeerSnapshot serBigLedgerPeerSnapshot of
1075-
Left (bs, _decoderError) ->
1076-
firstExceptT QueryCmdHelpersError $ pPrintCBOR bs
1077-
Right snapshot ->
1078-
case mOutPath of
1079-
Nothing -> liftIO . LBS.putStrLn $ Aeson.encode snapshot
1080-
Just fpath ->
1081-
firstExceptT QueryCmdWriteFileError $
1082-
newExceptT . writeLazyByteStringFile fpath $
1083-
encodePretty snapshot
1084-
10851095
writeStakeSnapshots
10861096
:: forall era
10871097
. Maybe (File () Out)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ Usage: cardano-cli query ledger-peer-snapshot
492492
[ --volatile-tip
493493
| --immutable-tip
494494
]
495+
[ --output-json
496+
| --output-json-pretty
497+
| --output-yaml
498+
]
495499
[--out-file FILEPATH]
496500

497501
Dump the current snapshot of big ledger peers. These are the largest pools
@@ -2047,6 +2051,10 @@ Usage: cardano-cli conway query ledger-peer-snapshot
20472051
[ --volatile-tip
20482052
| --immutable-tip
20492053
]
2054+
[ --output-json
2055+
| --output-json-pretty
2056+
| --output-yaml
2057+
]
20502058
[--out-file FILEPATH]
20512059

20522060
Dump the current snapshot of ledger peers.These are the largest pools that
@@ -4283,6 +4291,10 @@ Usage: cardano-cli latest query ledger-peer-snapshot
42834291
[ --volatile-tip
42844292
| --immutable-tip
42854293
]
4294+
[ --output-json
4295+
| --output-json-pretty
4296+
| --output-yaml
4297+
]
42864298
[--out-file FILEPATH]
42874299

42884300
Dump the current snapshot of ledger peers.These are the largest pools that

cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_ledger-peer-snapshot.cli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Usage: cardano-cli conway query ledger-peer-snapshot
88
[ --volatile-tip
99
| --immutable-tip
1010
]
11+
[ --output-json
12+
| --output-json-pretty
13+
| --output-yaml
14+
]
1115
[--out-file FILEPATH]
1216

1317
Dump the current snapshot of ledger peers.These are the largest pools that
@@ -30,5 +34,8 @@ Available options:
3034
--volatile-tip Use the volatile tip as a target. (This is the
3135
default)
3236
--immutable-tip Use the immutable tip as a target.
37+
--output-json Format ledger-peer-snapshot output to JSON (default).
38+
--output-json-pretty Format ledger-peer-snapshot output to JSON.
39+
--output-yaml Format ledger-peer-snapshot output to YAML.
3340
--out-file FILEPATH Optional output file. Default is to write to stdout.
3441
-h,--help Show this help text

cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query_ledger-peer-snapshot.cli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Usage: cardano-cli latest query ledger-peer-snapshot
88
[ --volatile-tip
99
| --immutable-tip
1010
]
11+
[ --output-json
12+
| --output-json-pretty
13+
| --output-yaml
14+
]
1115
[--out-file FILEPATH]
1216

1317
Dump the current snapshot of ledger peers.These are the largest pools that
@@ -30,5 +34,8 @@ Available options:
3034
--volatile-tip Use the volatile tip as a target. (This is the
3135
default)
3236
--immutable-tip Use the immutable tip as a target.
37+
--output-json Format ledger-peer-snapshot output to JSON (default).
38+
--output-json-pretty Format ledger-peer-snapshot output to JSON.
39+
--output-yaml Format ledger-peer-snapshot output to YAML.
3340
--out-file FILEPATH Optional output file. Default is to write to stdout.
3441
-h,--help Show this help text

cardano-cli/test/cardano-cli-golden/files/golden/help/query_ledger-peer-snapshot.cli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Usage: cardano-cli query ledger-peer-snapshot
88
[ --volatile-tip
99
| --immutable-tip
1010
]
11+
[ --output-json
12+
| --output-json-pretty
13+
| --output-yaml
14+
]
1115
[--out-file FILEPATH]
1216

1317
Dump the current snapshot of big ledger peers. These are the largest pools
@@ -30,5 +34,8 @@ Available options:
3034
--volatile-tip Use the volatile tip as a target. (This is the
3135
default)
3236
--immutable-tip Use the immutable tip as a target.
37+
--output-json Format ledger-peer-snapshot output to JSON (default).
38+
--output-json-pretty Format ledger-peer-snapshot output to JSON.
39+
--output-yaml Format ledger-peer-snapshot output to YAML.
3340
--out-file FILEPATH Optional output file. Default is to write to stdout.
3441
-h,--help Show this help text

0 commit comments

Comments
 (0)