Skip to content

Commit

Permalink
query stake-pools: add --output-[json,text] flag to control format of…
Browse files Browse the repository at this point in the history
… the output
  • Loading branch information
smelc committed Feb 22, 2024
1 parent 7de2a83 commit 51fc248
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 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
25 changes: 16 additions & 9 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,7 @@ runQueryStakePoolsCmd
, Cmd.consensusModeParams
, Cmd.networkId
, Cmd.target
, Cmd.format
, Cmd.mOutFile
} = do
let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath
Expand All @@ -1142,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 ->
encodePretty stakePools
QueryOutputFormatJson ->
LBS.unlines
$ map (textToLazyBytestring . serialiseToBech32)
$ Set.toList stakePools

runQueryStakeDistributionCmd :: ()
=> Cmd.QueryStakeDistributionCmdArgs
Expand Down
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

0 comments on commit 51fc248

Please sign in to comment.