Skip to content

Commit 5d779ea

Browse files
authored
Merge pull request #1172 from IntersectMBO/newhoggy/standard-output-format-for-query-ledger-peer-snapshot-command
Standard output format for `query ledger-peer-snapshot` command
2 parents 0bf3e93 + 3affc83 commit 5d779ea

File tree

8 files changed

+76
-33
lines changed

8 files changed

+76
-33
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ data QueryLedgerStateCmdArgs = QueryLedgerStateCmdArgs
149149

150150
data QueryLedgerPeerSnapshotCmdArgs = QueryLedgerPeerSnapshotCmdArgs
151151
{ commons :: !QueryCommons
152-
, outFile :: !(Maybe (File () Out))
152+
, outputFormat :: !(Vary [FormatJson, FormatYaml])
153+
, mOutFile :: !(Maybe (File () Out))
153154
}
154155
deriving (Generic, Show)
155156

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,11 @@ pQueryLedgerPeerSnapshotCmd era envCli =
427427
fmap QueryLedgerPeerSnapshotCmd $
428428
QueryLedgerPeerSnapshotCmdArgs
429429
<$> pQueryCommons era envCli
430+
<*> pFormatFlags
431+
"ledger-peer-snapshot output"
432+
[ flagFormatJson & setDefault
433+
, flagFormatYaml
434+
]
430435
<*> pMaybeOutputFile
431436

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

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

Lines changed: 41 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 ((^.))
@@ -852,25 +853,50 @@ runQueryLedgerPeerSnapshot
852853
{ Cmd.nodeConnInfo
853854
, Cmd.target
854855
}
855-
, Cmd.outFile
856+
, Cmd.outputFormat
857+
, Cmd.mOutFile
856858
} = do
857-
join $
858-
lift
859-
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
860-
AnyCardanoEra era <-
861-
lift queryCurrentEra
862-
& onLeft (left . QueryCmdUnsupportedNtcVersion)
859+
result <-
860+
join $
861+
lift
862+
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
863+
AnyCardanoEra era <-
864+
lift queryCurrentEra
865+
& onLeft (left . QueryCmdUnsupportedNtcVersion)
863866

864-
sbe <-
865-
requireShelleyBasedEra era
866-
& onNothing (left QueryCmdByronEra)
867+
sbe <-
868+
requireShelleyBasedEra era
869+
& onNothing (left QueryCmdByronEra)
867870

868-
result <- easyRunQuery (queryLedgerPeerSnapshot sbe)
871+
result <- easyRunQuery (queryLedgerPeerSnapshot sbe)
869872

870-
pure $ shelleyBasedEraConstraints sbe (writeLedgerPeerSnapshot outFile) result
871-
)
872-
& onLeft (left . QueryCmdAcquireFailure)
873-
& onLeft left
873+
pure $
874+
shelleyBasedEraConstraints sbe $
875+
case decodeBigLedgerPeerSnapshot result of
876+
Left (bs, _decoderError) -> pure $ Left bs
877+
Right snapshot -> pure $ Right snapshot
878+
)
879+
& onLeft (left . QueryCmdAcquireFailure)
880+
& onLeft left
881+
882+
case result of
883+
Left (bs :: LBS.ByteString) -> do
884+
firstExceptT QueryCmdHelpersError $ pPrintCBOR bs
885+
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+
)
893+
894+
let writeOutputContents =
895+
case mOutFile of
896+
Nothing -> liftIO . LBS.putStrLn
897+
Just (File outFile) -> liftIO . LBS.writeFile outFile
898+
899+
writeOutputContents outputContents
874900

875901
runQueryProtocolStateCmd
876902
:: ()
@@ -1077,23 +1103,6 @@ writeStakeAddressInfo
10771103
mDRep = Map.lookup addr voteDelegatees
10781104
]
10791105

1080-
-- | Writes JSON-encoded big ledger peer snapshot
1081-
writeLedgerPeerSnapshot
1082-
:: Maybe (File () Out)
1083-
-> Serialised LedgerPeerSnapshot
1084-
-> ExceptT QueryCmdError IO ()
1085-
writeLedgerPeerSnapshot mOutPath serBigLedgerPeerSnapshot = do
1086-
case decodeBigLedgerPeerSnapshot serBigLedgerPeerSnapshot of
1087-
Left (bs, _decoderError) ->
1088-
firstExceptT QueryCmdHelpersError $ pPrintCBOR bs
1089-
Right snapshot ->
1090-
case mOutPath of
1091-
Nothing -> liftIO . LBS.putStrLn $ Aeson.encode snapshot
1092-
Just fpath ->
1093-
firstExceptT QueryCmdWriteFileError $
1094-
newExceptT . writeLazyByteStringFile fpath $
1095-
encodePretty snapshot
1096-
10971106
writeStakeSnapshots
10981107
:: forall era
10991108
. Maybe (File () Out)

cardano-cli/src/Cardano/CLI/Helper.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Cardano.CLI.Helper
88
( HelpersError (..)
99
, cborToText
1010
, cborToTextByteString
11+
, cborToTextLazyByteString
1112
, printWarning
1213
, deprecationWarning
1314
, ensureNewFile
@@ -35,11 +36,14 @@ import Data.Bifunctor (Bifunctor (..))
3536
import Data.ByteString (ByteString)
3637
import Data.ByteString qualified as BS
3738
import Data.ByteString.Lazy qualified as LB
39+
import Data.ByteString.Lazy qualified as LBS
3840
import Data.Functor (void)
3941
import Data.Text (Text)
4042
import Data.Text qualified as Text
4143
import Data.Text.Encoding qualified as Text
4244
import Data.Text.IO qualified as Text
45+
import Data.Text.Lazy qualified as LT
46+
import Data.Text.Lazy.Encoding qualified as LT
4347
import Data.Typeable (Typeable)
4448
import System.Console.ANSI
4549
import System.Console.ANSI qualified as ANSI
@@ -114,6 +118,10 @@ cborToTextByteString bs = do
114118
text <- cborToText bs
115119
pure $ LB.fromStrict $ Text.encodeUtf8 text
116120

121+
cborToTextLazyByteString :: LB.ByteString -> ExceptT HelpersError IO LBS.ByteString
122+
cborToTextLazyByteString =
123+
fmap (LT.encodeUtf8 . LT.fromStrict) . cborToText
124+
117125
cborToText :: LB.ByteString -> ExceptT HelpersError IO Text
118126
cborToText bs = do
119127
as <- cborToTextList bs

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ Usage: cardano-cli query ledger-peer-snapshot
493493
[ --volatile-tip
494494
| --immutable-tip
495495
]
496+
[--output-json | --output-yaml]
496497
[--out-file FILEPATH]
497498

498499
Dump the current snapshot of big ledger peers. These are the largest pools
@@ -2048,6 +2049,9 @@ Usage: cardano-cli conway query ledger-peer-snapshot
20482049
[ --volatile-tip
20492050
| --immutable-tip
20502051
]
2052+
[ --output-json
2053+
| --output-yaml
2054+
]
20512055
[--out-file FILEPATH]
20522056

20532057
Dump the current snapshot of ledger peers.These are the largest pools that
@@ -4285,6 +4289,9 @@ Usage: cardano-cli latest query ledger-peer-snapshot
42854289
[ --volatile-tip
42864290
| --immutable-tip
42874291
]
4292+
[ --output-json
4293+
| --output-yaml
4294+
]
42884295
[--out-file FILEPATH]
42894296

42904297
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Usage: cardano-cli conway query ledger-peer-snapshot
88
[ --volatile-tip
99
| --immutable-tip
1010
]
11+
[ --output-json
12+
| --output-yaml
13+
]
1114
[--out-file FILEPATH]
1215

1316
Dump the current snapshot of ledger peers.These are the largest pools that
@@ -30,5 +33,7 @@ Available options:
3033
--volatile-tip Use the volatile tip as a target. (This is the
3134
default)
3235
--immutable-tip Use the immutable tip as a target.
36+
--output-json Format ledger-peer-snapshot output to JSON (default).
37+
--output-yaml Format ledger-peer-snapshot output to YAML.
3338
--out-file FILEPATH Optional output file. Default is to write to stdout.
3439
-h,--help Show this help text

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

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

1316
Dump the current snapshot of ledger peers.These are the largest pools that
@@ -30,5 +33,7 @@ Available options:
3033
--volatile-tip Use the volatile tip as a target. (This is the
3134
default)
3235
--immutable-tip Use the immutable tip as a target.
36+
--output-json Format ledger-peer-snapshot output to JSON (default).
37+
--output-yaml Format ledger-peer-snapshot output to YAML.
3338
--out-file FILEPATH Optional output file. Default is to write to stdout.
3439
-h,--help Show this help text

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

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

1314
Dump the current snapshot of big ledger peers. These are the largest pools
@@ -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-peer-snapshot output to JSON (default).
35+
--output-yaml Format ledger-peer-snapshot output to YAML.
3336
--out-file FILEPATH Optional output file. Default is to write to stdout.
3437
-h,--help Show this help text

0 commit comments

Comments
 (0)