Skip to content

Commit

Permalink
Merge pull request #617 from IntersectMBO/smelc/query-stake-pools-con…
Browse files Browse the repository at this point in the history
…trol-output-format

query stake-pools: add --output-[json,text] flag to control format of the output
  • Loading branch information
smelc committed Feb 22, 2024
2 parents 92975de + b3964aa commit 615beb8
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 17 deletions.
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ data QueryStakePoolsCmdArgs = QueryStakePoolsCmdArgs
, consensusModeParams :: !ConsensusModeParams
, networkId :: !NetworkId
, target :: !(Consensus.Target ChainPoint)
, format :: Maybe QueryOutputFormat
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

Expand Down
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ pQueryStakePoolsCmd era envCli =
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> pTarget era
<*> (optional $ pQueryOutputFormat "stake-pools")
<*> pMaybeOutputFile

pQueryStakeDistributionCmd :: CardanoEra era -> EnvCli -> Parser (QueryCmds era)
Expand Down
48 changes: 31 additions & 17 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,15 +1027,9 @@ writeFilteredUTxOs sbe format mOutFile utxo =
shelleyBasedEraConstraints sbe $
firstExceptT QueryCmdWriteFileError . newExceptT .
writeLazyByteStringOutput mOutFile $
case format' of
case newOutputFormat format mOutFile of
QueryOutputFormatJson -> encodePretty utxo
QueryOutputFormatText -> BS.fromChunks [Text.encodeUtf8 $ filteredUTxOsToText sbe utxo]
where
format' =
case (format, mOutFile) of
(Just f, _) -> f -- Take flag from CLI if specified
(Nothing, Nothing) -> QueryOutputFormatText -- No CLI flag, writing to stdout: write text
(Nothing, Just _) -> QueryOutputFormatJson -- No CLI flag, writing to a file: write JSON
QueryOutputFormatText -> strictTextToLazyBytestring $ filteredUTxOsToText sbe utxo

filteredUTxOsToText :: Api.ShelleyBasedEra era -> UTxO era -> Text
filteredUTxOsToText sbe (UTxO utxo) = do
Expand Down Expand Up @@ -1132,6 +1126,7 @@ runQueryStakePoolsCmd
, Cmd.consensusModeParams
, Cmd.networkId
, Cmd.target
, Cmd.format
, Cmd.mOutFile
} = do
let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath
Expand All @@ -1148,21 +1143,27 @@ runQueryStakePoolsCmd
& onLeft (left . QueryCmdEraMismatch)

pure $ do
writeStakePools mOutFile poolIds
writeStakePools (newOutputFormat format mOutFile) mOutFile poolIds
) & onLeft (left . QueryCmdAcquireFailure)
& onLeft left

writeStakePools
:: Maybe (File () Out)
:: QueryOutputFormat
-> Maybe (File () Out)
-> Set PoolId
-> ExceptT QueryCmdError IO ()
writeStakePools (Just (File outFile)) stakePools =
handleIOExceptT (QueryCmdWriteFileError . FileIOError outFile) $
LBS.writeFile outFile (encodePretty stakePools)

writeStakePools Nothing stakePools =
forM_ (Set.toList stakePools) $ \poolId ->
liftIO . putStrLn $ Text.unpack (serialiseToBech32 poolId)
writeStakePools format mOutFile stakePools =
firstExceptT QueryCmdWriteFileError . newExceptT $
writeLazyByteStringOutput mOutFile toWrite
where
toWrite :: LBS.ByteString =
case format of
QueryOutputFormatText ->
LBS.unlines
$ map (strictTextToLazyBytestring . serialiseToBech32)
$ Set.toList stakePools
QueryOutputFormatJson ->
encodePretty stakePools

runQueryStakeDistributionCmd :: ()
=> Cmd.QueryStakeDistributionCmdArgs
Expand Down Expand Up @@ -1614,3 +1615,16 @@ requireEon minEra era =
hoistMaybe
(QueryCmdLocalStateQueryError $ mkEraMismatchError NodeEraMismatchError { nodeEra = era, era = minEra })
(forEraMaybeEon era)

-- | The output format to use, for commands with a recently introduced --output-[json,text] flag
-- and that used to have the following default: --out-file implies JSON,
-- output to stdout implied text.
newOutputFormat :: Maybe QueryOutputFormat -> Maybe a -> QueryOutputFormat
newOutputFormat format mOutFile =
case (format, mOutFile) of
(Just f, _) -> f -- Take flag from CLI if specified
(Nothing, Nothing) -> QueryOutputFormatText -- No CLI flag, writing to stdout: write text
(Nothing, Just _) -> QueryOutputFormatJson -- No CLI flag, writing to a file: write JSON

strictTextToLazyBytestring :: Text -> LBS.ByteString
strictTextToLazyBytestring t = BS.fromChunks [Text.encodeUtf8 t]
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/Legacy/Commands/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ data LegacyQueryStakePoolsCmdArgs = LegacyQueryStakePoolsCmdArgs
{ nodeSocketPath :: !SocketPath
, consensusModeParams :: !ConsensusModeParams
, networkId :: !NetworkId
, format :: Maybe QueryOutputFormat
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

Expand Down
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/Legacy/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ pQueryCmds envCli =
<$> pSocketPath envCli
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> (optional $ pQueryOutputFormat "stake-pools")
<*> pMaybeOutputFile

pQueryStakeDistribution :: Parser LegacyQueryCmds
Expand Down
9 changes: 9 additions & 0 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ Usage: cardano-cli shelley query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand Down Expand Up @@ -1632,6 +1633,7 @@ Usage: cardano-cli allegra query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand Down Expand Up @@ -2788,6 +2790,7 @@ Usage: cardano-cli mary query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand Down Expand Up @@ -3935,6 +3938,7 @@ Usage: cardano-cli alonzo query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand Down Expand Up @@ -5118,6 +5122,7 @@ Usage: cardano-cli babbage query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand Down Expand Up @@ -6538,6 +6543,7 @@ Usage: cardano-cli conway query stake-pools --socket-path SOCKET_PATH
| --testnet-magic NATURAL
)
[--volatile-tip | --immutable-tip]
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand Down Expand Up @@ -7903,6 +7909,7 @@ Usage: cardano-cli latest query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand Down Expand Up @@ -8917,6 +8924,7 @@ Usage: cardano-cli legacy query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand Down Expand Up @@ -10166,6 +10174,7 @@ Usage: cardano-cli query tip --socket-path SOCKET_PATH
Usage: cardano-cli query stake-pools --socket-path SOCKET_PATH
[--cardano-mode [--epoch-slots SLOTS]]
(--mainnet | --testnet-magic NATURAL)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Usage: cardano-cli allegra query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand All @@ -22,5 +23,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-pools query output to JSON. Default
format when writing to a file
--output-text Format stake-pools query output to TEXT. Default
format when writing to stdout
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Usage: cardano-cli alonzo query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand All @@ -22,5 +23,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-pools query output to JSON. Default
format when writing to a file
--output-text Format stake-pools query output to TEXT. Default
format when writing to stdout
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Usage: cardano-cli babbage query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand All @@ -22,5 +23,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-pools query output to JSON. Default
format when writing to a file
--output-text Format stake-pools query output to TEXT. Default
format when writing to stdout
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Usage: cardano-cli conway query stake-pools --socket-path SOCKET_PATH
| --testnet-magic NATURAL
)
[--volatile-tip | --immutable-tip]
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand All @@ -26,5 +27,9 @@ Available options:
--volatile-tip Use the volatile tip as a target. (This is the
default)
--immutable-tip Use the immutable tip as a target.
--output-json Format stake-pools query output to JSON. Default
format when writing to a file
--output-text Format stake-pools query output to TEXT. Default
format when writing to stdout
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Usage: cardano-cli latest query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand All @@ -22,5 +23,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-pools query output to JSON. Default
format when writing to a file
--output-text Format stake-pools query output to TEXT. Default
format when writing to stdout
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Usage: cardano-cli legacy query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand All @@ -22,5 +23,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-pools query output to JSON. Default
format when writing to a file
--output-text Format stake-pools query output to TEXT. Default
format when writing to stdout
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Usage: cardano-cli mary query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand All @@ -22,5 +23,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-pools query output to JSON. Default
format when writing to a file
--output-text Format stake-pools query output to TEXT. Default
format when writing to stdout
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Usage: cardano-cli query stake-pools --socket-path SOCKET_PATH
[--cardano-mode [--epoch-slots SLOTS]]
(--mainnet | --testnet-magic NATURAL)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand All @@ -19,5 +20,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-pools query output to JSON. Default
format when writing to a file
--output-text Format stake-pools query output to TEXT. Default
format when writing to stdout
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Usage: cardano-cli shelley query stake-pools --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current set of stake pool ids
Expand All @@ -22,5 +23,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-pools query output to JSON. Default
format when writing to a file
--output-text Format stake-pools query output to TEXT. Default
format when writing to stdout
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text

0 comments on commit 615beb8

Please sign in to comment.