Skip to content

yaml output format for more commands #1192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cardano-cli/src/Cardano/CLI/EraBased/Query/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ data QueryTipCmdArgs = QueryTipCmdArgs

data QueryStakePoolsCmdArgs = QueryStakePoolsCmdArgs
{ commons :: !QueryCommons
, outputFormat :: !(Vary [FormatJson, FormatText])
, outputFormat :: !(Vary [FormatJson, FormatText, FormatYaml])
, mOutFile :: !(Maybe (File () Out))
}
deriving (Generic, Show)

data QueryStakeDistributionCmdArgs = QueryStakeDistributionCmdArgs
{ commons :: !QueryCommons
, outputFormat :: !(Vary [FormatJson, FormatText])
, outputFormat :: !(Vary [FormatJson, FormatText, FormatYaml])
, mOutFile :: !(Maybe (File () Out))
}
deriving (Generic, Show)
Expand All @@ -138,14 +138,14 @@ data QueryStakeAddressInfoCmdArgs = QueryStakeAddressInfoCmdArgs
data QueryUTxOCmdArgs = QueryUTxOCmdArgs
{ commons :: !QueryCommons
, queryFilter :: !QueryUTxOFilter
, outputFormat :: !(Vary [FormatCborBin, FormatCborHex, FormatJson, FormatText])
, outputFormat :: !(Vary [FormatCborBin, FormatCborHex, FormatJson, FormatText, FormatYaml])
, mOutFile :: !(Maybe (File () Out))
}
deriving (Generic, Show)

data QueryLedgerStateCmdArgs = QueryLedgerStateCmdArgs
{ commons :: !QueryCommons
, outputFormat :: !(Vary [FormatJson, FormatText])
, outputFormat :: !(Vary [FormatJson, FormatText, FormatYaml])
, mOutFile :: !(Maybe (File () Out))
}
deriving (Generic, Show)
Expand Down Expand Up @@ -206,7 +206,7 @@ data QuerySlotNumberCmdArgs = QuerySlotNumberCmdArgs
data QueryRefScriptSizeCmdArgs = QueryRefScriptSizeCmdArgs
{ commons :: !QueryCommons
, transactionInputs :: !(Set TxIn)
, outputFormat :: !(Vary [FormatJson, FormatText])
, outputFormat :: !(Vary [FormatJson, FormatText, FormatYaml])
, mOutFile :: !(Maybe (File () Out))
}
deriving (Generic, Show)
Expand Down
5 changes: 5 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Query/Option.hs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ pQueryUTxOCmd era envCli =
, flagFormatCborHex
, flagFormatJson & setDefault
, flagFormatText
, flagFormatYaml
]
<*> pMaybeOutputFile

Expand All @@ -399,6 +400,7 @@ pQueryStakePoolsCmd era envCli =
"stake-pools"
[ flagFormatJson & setDefault
, flagFormatText
, flagFormatYaml
]
<*> pMaybeOutputFile

Expand All @@ -411,6 +413,7 @@ pQueryStakeDistributionCmd era envCli =
"stake-distribution"
[ flagFormatJson & setDefault
, flagFormatText
, flagFormatYaml
]
<*> pMaybeOutputFile

Expand All @@ -436,6 +439,7 @@ pQueryLedgerStateCmd era envCli =
"ledger-state"
[ flagFormatJson & setDefault
, flagFormatText
, flagFormatYaml
]
<*> pMaybeOutputFile

Expand Down Expand Up @@ -592,6 +596,7 @@ pQueryRefScriptSizeCmd era envCli =
"reference-script-size"
[ flagFormatJson & setDefault
, flagFormatText
, flagFormatYaml
]
<*> pMaybeOutputFile
where
Expand Down
40 changes: 26 additions & 14 deletions cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ import Cardano.Slotting.Time (RelativeTime (..), toRelativeTime)

import Control.Monad (forM, join)
import Data.Aeson as Aeson
import Data.Aeson.Encode.Pretty qualified as Aeson
import Data.Bifunctor (Bifunctor (..))
import Data.ByteString.Base16.Lazy qualified as Base16
import Data.ByteString.Lazy qualified as BS
Expand Down Expand Up @@ -844,6 +843,7 @@ runQueryLedgerStateCmd
& ( id
. Vary.on (\FormatJson -> ledgerStateAsJsonByteString serialisedDebugLedgerState)
. Vary.on (\FormatText -> ledgerStateAsTextByteString serialisedDebugLedgerState)
. Vary.on (\FormatYaml -> ledgerStateAsYamlByteString serialisedDebugLedgerState)
$ Vary.exhaustiveCase
)
)
Expand All @@ -861,7 +861,7 @@ ledgerStateAsJsonByteString
ledgerStateAsJsonByteString serialisedDebugLedgerState =
case decodeDebugLedgerState serialisedDebugLedgerState of
Left (bs, _decoderError) -> firstExceptT QueryCmdHelpersError $ cborToTextByteString bs
Right decodededgerState -> pure $ Aeson.encode decodededgerState <> "\n"
Right decodededgerState -> pure $ Json.encodeJson decodededgerState <> "\n"

ledgerStateAsTextByteString
:: Applicative f
Expand All @@ -870,6 +870,15 @@ ledgerStateAsTextByteString serialisedDebugLedgerState =
let SerialisedDebugLedgerState serLedgerState = serialisedDebugLedgerState
in pure $ unSerialised serLedgerState

ledgerStateAsYamlByteString
:: IsShelleyBasedEra era
=> SerialisedDebugLedgerState era
-> ExceptT QueryCmdError IO LBS.ByteString
ledgerStateAsYamlByteString serialisedDebugLedgerState =
case decodeDebugLedgerState serialisedDebugLedgerState of
Left (bs, _decoderError) -> firstExceptT QueryCmdHelpersError $ cborToTextByteString bs
Right decodededgerState -> pure $ Json.encodeYaml decodededgerState

runQueryLedgerPeerSnapshot
:: ()
=> Cmd.QueryLedgerPeerSnapshotCmdArgs
Expand Down Expand Up @@ -1250,7 +1259,7 @@ writePoolState outputFormat mOutFile serialisedCurrentEpochState = do

writeFilteredUTxOs
:: Api.ShelleyBasedEra era
-> Vary [FormatCborBin, FormatCborHex, FormatJson, FormatText]
-> Vary [FormatCborBin, FormatCborHex, FormatJson, FormatText, FormatYaml]
-> Maybe (File () Out)
-> UTxO era
-> ExceptT QueryCmdError IO ()
Expand All @@ -1263,6 +1272,7 @@ writeFilteredUTxOs sbe format mOutFile utxo = do
. Vary.on (\FormatCborHex -> Base16.encode . CBOR.serialize $ toLedgerUTxO sbe utxo)
. Vary.on (\FormatJson -> Json.encodeJson utxo)
. Vary.on (\FormatText -> strictTextToLazyBytestring $ filteredUTxOsToText sbe utxo)
. Vary.on (\FormatYaml -> Json.encodeYaml utxo)
$ Vary.exhaustiveCase
)

Expand Down Expand Up @@ -1385,35 +1395,35 @@ runQueryStakePoolsCmd

-- TODO: replace with writeFormattedOutput
writeStakePools
:: Vary [FormatJson, FormatText]
:: Vary [FormatJson, FormatText, FormatYaml]
-> Maybe (File () Out)
-> Set PoolId
-> ExceptT QueryCmdError IO ()
writeStakePools format mOutFile stakePools = do
let output =
format
& ( id
. Vary.on (\FormatJson -> writeJson)
. Vary.on (\FormatText -> writeText)
. Vary.on (\FormatJson -> Json.encodeJson)
. Vary.on (\FormatText -> encodeText)
. Vary.on (\FormatYaml -> Json.encodeYaml)
$ Vary.exhaustiveCase
)
$ stakePools

firstExceptT QueryCmdWriteFileError
. newExceptT
$ writeLazyByteStringOutput mOutFile output
where
writeJson =
Aeson.encodePretty stakePools
writeText =
LBS.unlines $
map (strictTextToLazyBytestring . serialiseToBech32) $
toList stakePools
encodeText =
LBS.unlines
. map (strictTextToLazyBytestring . serialiseToBech32)
. toList

writeFormattedOutput
:: MonadIOTransError QueryCmdError t m
=> ToJSON a
=> Pretty a
=> Vary [FormatJson, FormatText]
=> Vary [FormatJson, FormatText, FormatYaml]
-> Maybe (File b Out)
-> a
-> t m ()
Expand All @@ -1423,6 +1433,7 @@ writeFormattedOutput format mOutFile value = do
& ( id
. Vary.on (\FormatJson -> Json.encodeJson value)
. Vary.on (\FormatText -> fromString . docToString $ pretty value)
. Vary.on (\FormatYaml -> Json.encodeYaml value)
$ Vary.exhaustiveCase
)

Expand Down Expand Up @@ -1462,7 +1473,7 @@ runQueryStakeDistributionCmd
& onLeft left

writeStakeDistribution
:: Vary [FormatJson, FormatText]
:: Vary [FormatJson, FormatText, FormatYaml]
-> Maybe (File () Out)
-> Map PoolId Rational
-> ExceptT QueryCmdError IO ()
Expand All @@ -1472,6 +1483,7 @@ writeStakeDistribution format mOutFile stakeDistrib = do
& ( id
. Vary.on (\FormatJson -> Json.encodeJson stakeDistrib)
. Vary.on (\FormatText -> strictTextToLazyBytestring stakeDistributionText)
. Vary.on (\FormatYaml -> Json.encodeYaml stakeDistrib)
$ Vary.exhaustiveCase
)

Expand Down
4 changes: 2 additions & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/TextView/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ where

import Cardano.Api.Shelley

import Cardano.CLI.Type.Common (FormatCborHex, FormatText)
import Cardano.CLI.Type.Common (FormatCborHex, FormatJson, FormatText, FormatYaml)

import Data.Text (Text)
import Vary
Expand All @@ -22,7 +22,7 @@ newtype TextViewCmds era
data TextViewDecodeCborCmdArgs
= TextViewDecodeCborCmdArgs
{ inputFile :: !FilePath
, outputFormat :: !(Vary [FormatCborHex, FormatText])
, outputFormat :: !(Vary [FormatCborHex, FormatJson, FormatText, FormatYaml])
, mOutFile :: Maybe (File () Out)
}
deriving Show
Expand Down
2 changes: 2 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/TextView/Option.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ pTextViewCmds =
<*> pFormatFlags
"text view info output format"
[ flagFormatCborHex
, flagFormatJson
, flagFormatText & setDefault
, flagFormatYaml
]
<*> pMaybeOutputFile
)
Expand Down
3 changes: 3 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/TextView/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Cardano.Api

import Cardano.CLI.EraBased.TextView.Command
import Cardano.CLI.Helper (cborToText)
import Cardano.CLI.Json.Encode qualified as Json
import Cardano.CLI.Type.Common
import Cardano.CLI.Type.Error.TextViewFileError

Expand Down Expand Up @@ -43,7 +44,9 @@ runTextViewInfoCmd
outputFormat
& ( id
. Vary.on (\FormatCborHex -> pure lbCBOR)
. Vary.on (\FormatJson -> pure $ Json.encodeJson tv)
. Vary.on (\FormatText -> LBS.fromStrict . Text.encodeUtf8 <$> cborToText lbCBOR)
. Vary.on (\FormatYaml -> pure $ Json.encodeYaml tv)
$ Vary.exhaustiveCase
)
& firstExceptT TextViewCBORPrettyPrintError
Expand Down
4 changes: 2 additions & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/Transaction/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ data TransactionCalculateMinFeeCmdArgs = TransactionCalculateMinFeeCmdArgs
, txByronWitnessCount :: !TxByronWitnessCount
, referenceScriptSize :: !ReferenceScriptSize
-- ^ The total size in bytes of the transaction reference scripts.
, outputFormat :: !(Vary [FormatJson, FormatText])
, outputFormat :: !(Vary [FormatJson, FormatText, FormatYaml])
, outFile :: !(Maybe (File () Out))
}
deriving Show
Expand Down Expand Up @@ -314,7 +314,7 @@ newtype TransactionHashScriptDataCmdArgs = TransactionHashScriptDataCmdArgs

data TransactionTxIdCmdArgs = TransactionTxIdCmdArgs
{ inputTxBodyOrTxFile :: InputTxBodyOrTxFile
, outputFormat :: !(Vary [FormatJson, FormatText])
, outputFormat :: !(Vary [FormatJson, FormatText, FormatYaml])
}
deriving Show

Expand Down
2 changes: 2 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Transaction/Option.hs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ pTransactionCalculateMinFee =
"calculate-min-fee query output"
[ flagFormatJson & setDefault
, flagFormatText
, flagFormatYaml
]
<*> optional pOutputFile
-- Deprecated options:
Expand Down Expand Up @@ -546,6 +547,7 @@ pTransactionId =
"output"
[ flagFormatJson & setDefault
, flagFormatText
, flagFormatYaml
]

pIsCborOutCanonical :: Parser TxCborFormat
Expand Down
21 changes: 17 additions & 4 deletions cardano-cli/src/Cardano/CLI/EraBased/Transaction/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import Cardano.CLI.EraBased.Transaction.Internal.HashCheck
, checkProposalHashes
, checkVotingProcedureHashes
)
import Cardano.CLI.Json.Encode qualified as Json
import Cardano.CLI.Orphan ()
import Cardano.CLI.Read
import Cardano.CLI.Type.Common
Expand Down Expand Up @@ -1530,16 +1531,18 @@ runTransactionCalculateMinFeeCmd

let fee = shelleyfee + byronfee
textToWrite = docToText $ pretty fee
jsonToWrite = encodePretty $ Aeson.object ["fee" .= fee]
content = Aeson.object ["fee" .= fee]

outputFormat
& ( id
. Vary.on
( \FormatJson -> case outFile of
Nothing ->
liftIO $ LBS.putStrLn jsonToWrite
liftIO $ LBS.putStrLn $ Json.encodeJson content
Just file ->
firstExceptT TxCmdWriteFileError . newExceptT $ writeLazyByteStringFile file jsonToWrite
firstExceptT TxCmdWriteFileError . newExceptT $
writeLazyByteStringFile file $
Json.encodeJson content
)
. Vary.on
( \FormatText -> case outFile of
Expand All @@ -1548,6 +1551,15 @@ runTransactionCalculateMinFeeCmd
Just file ->
firstExceptT TxCmdWriteFileError . newExceptT $ writeTextFile file textToWrite
)
. Vary.on
( \FormatYaml -> case outFile of
Nothing ->
liftIO $ LBS.putStrLn $ Json.encodeYaml content
Just file ->
firstExceptT TxCmdWriteFileError . newExceptT $
writeLazyByteStringFile file $
Json.encodeYaml content
)
$ Vary.exhaustiveCase
)

Expand Down Expand Up @@ -1825,8 +1837,9 @@ runTransactionTxIdCmd
liftIO $
outputFormat
& ( id
. Vary.on (\FormatJson -> LBS.putStrLn $ Aeson.encode $ TxSubmissionResult txId)
. Vary.on (\FormatJson -> LBS.putStrLn $ Json.encodeJson $ TxSubmissionResult txId)
. Vary.on (\FormatText -> BS.putStrLn $ serialiseToRawBytesHex txId)
. Vary.on (\FormatYaml -> LBS.putStrLn $ Json.encodeYaml $ TxSubmissionResult txId)
$ Vary.exhaustiveCase
)

Expand Down
Loading
Loading