diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs index 2eb0e7d864..c794e1858f 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs @@ -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) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs index 3c5b6514f3..f11cff0ddf 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs @@ -154,6 +154,7 @@ pQueryStakePoolsCmd era envCli = <*> pConsensusModeParams <*> pNetworkId envCli <*> pTarget era + <*> (optional $ pQueryOutputFormat "stake-pools") <*> pMaybeOutputFile pQueryStakeDistributionCmd :: CardanoEra era -> EnvCli -> Parser (QueryCmds era) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs b/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs index 1477d27462..28e79f04d4 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs @@ -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 @@ -1132,6 +1126,7 @@ runQueryStakePoolsCmd , Cmd.consensusModeParams , Cmd.networkId , Cmd.target + , Cmd.format , Cmd.mOutFile } = do let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath @@ -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 @@ -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] diff --git a/cardano-cli/src/Cardano/CLI/Legacy/Commands/Query.hs b/cardano-cli/src/Cardano/CLI/Legacy/Commands/Query.hs index 0c07c42165..73d8988a26 100644 --- a/cardano-cli/src/Cardano/CLI/Legacy/Commands/Query.hs +++ b/cardano-cli/src/Cardano/CLI/Legacy/Commands/Query.hs @@ -85,6 +85,7 @@ data LegacyQueryStakePoolsCmdArgs = LegacyQueryStakePoolsCmdArgs { nodeSocketPath :: !SocketPath , consensusModeParams :: !ConsensusModeParams , networkId :: !NetworkId + , format :: Maybe QueryOutputFormat , mOutFile :: !(Maybe (File () Out)) } deriving (Generic, Show) diff --git a/cardano-cli/src/Cardano/CLI/Legacy/Options.hs b/cardano-cli/src/Cardano/CLI/Legacy/Options.hs index 3a4f27e427..15fcd7866a 100644 --- a/cardano-cli/src/Cardano/CLI/Legacy/Options.hs +++ b/cardano-cli/src/Cardano/CLI/Legacy/Options.hs @@ -658,6 +658,7 @@ pQueryCmds envCli = <$> pSocketPath envCli <*> pConsensusModeParams <*> pNetworkId envCli + <*> (optional $ pQueryOutputFormat "stake-pools") <*> pMaybeOutputFile pQueryStakeDistribution :: Parser LegacyQueryCmds diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help.cli index 7bf228de77..ca88f295b3 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help.cli @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/allegra_query_stake-pools.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/allegra_query_stake-pools.cli index 0e5c19968a..83f04b8a76 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/allegra_query_stake-pools.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/allegra_query_stake-pools.cli @@ -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 @@ -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 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/alonzo_query_stake-pools.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/alonzo_query_stake-pools.cli index 656f8fb844..dc58f7a5b1 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/alonzo_query_stake-pools.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/alonzo_query_stake-pools.cli @@ -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 @@ -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 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/babbage_query_stake-pools.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/babbage_query_stake-pools.cli index 14341c6d6a..6fb2178cec 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/babbage_query_stake-pools.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/babbage_query_stake-pools.cli @@ -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 @@ -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 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_stake-pools.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_stake-pools.cli index 573dab1ef7..769c86a40c 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_stake-pools.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_query_stake-pools.cli @@ -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 @@ -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 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query_stake-pools.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query_stake-pools.cli index f125cf704d..b4b5f261c6 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query_stake-pools.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_query_stake-pools.cli @@ -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 @@ -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 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/legacy_query_stake-pools.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/legacy_query_stake-pools.cli index 899c68a849..c47b70e98f 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/legacy_query_stake-pools.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/legacy_query_stake-pools.cli @@ -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 @@ -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 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/mary_query_stake-pools.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/mary_query_stake-pools.cli index cd20c2fab3..55ffc3c662 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/mary_query_stake-pools.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/mary_query_stake-pools.cli @@ -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 @@ -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 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/query_stake-pools.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/query_stake-pools.cli index 90fb418cb0..80b009ba43 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/query_stake-pools.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/query_stake-pools.cli @@ -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 @@ -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 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/shelley_query_stake-pools.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/shelley_query_stake-pools.cli index e12c8079f2..6113c8a910 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/shelley_query_stake-pools.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/shelley_query_stake-pools.cli @@ -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 @@ -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