Skip to content

More accurate format flags #1183

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 5 commits into from
May 15, 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
14 changes: 10 additions & 4 deletions cardano-cli/src/Cardano/CLI/EraBased/Common/Option.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1843,11 +1843,17 @@ flagFormatBech32
flagFormatBech32 =
mkFlag "output-bech32" "BECH32" FormatBech32

flagFormatCbor
:: FormatCbor :| fs
flagFormatCborHex
:: FormatCborHex :| fs
=> Flag (Vary fs)
flagFormatCbor =
mkFlag "output-cbor" "BASE16 CBOR" FormatCbor
flagFormatCborHex =
mkFlag "output-cbor-hex" "BASE16 CBOR" FormatCborHex

flagFormatCborBin
:: FormatCborBin :| fs
=> Flag (Vary fs)
flagFormatCborBin =
mkFlag "output-cbor-bin" "CBOR" FormatCborBin

flagFormatHex
:: FormatHex :| fs
Expand Down
3 changes: 2 additions & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Query/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ data QueryStakeAddressInfoCmdArgs = QueryStakeAddressInfoCmdArgs
data QueryUTxOCmdArgs = QueryUTxOCmdArgs
{ commons :: !QueryCommons
, queryFilter :: !QueryUTxOFilter
, outputFormat :: !(Vary [FormatCbor, FormatJson, FormatText])
, outputFormat :: !(Vary [FormatCborBin, FormatCborHex, FormatJson, FormatText])
, mOutFile :: !(Maybe (File () Out))
}
deriving (Generic, Show)
Expand All @@ -159,6 +159,7 @@ data QueryLedgerPeerSnapshotCmdArgs = QueryLedgerPeerSnapshotCmdArgs

data QueryProtocolStateCmdArgs = QueryProtocolStateCmdArgs
{ commons :: !QueryCommons
, outputFormat :: !(Vary [FormatCborBin, FormatCborHex, FormatJson, FormatYaml])
, mOutFile :: !(Maybe (File () Out))
}
deriving (Generic, Show)
Expand Down
10 changes: 9 additions & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Query/Option.hs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ pQueryUTxOCmd era envCli =
<*> pQueryUTxOFilter
<*> pFormatFlags
"utxo query output"
[ flagFormatCbor
[ flagFormatCborBin
, flagFormatCborHex
, flagFormatJson & setDefault
, flagFormatText
]
Expand Down Expand Up @@ -454,6 +455,13 @@ pQueryProtocolStateCmd era envCli =
fmap QueryProtocolStateCmd $
QueryProtocolStateCmdArgs
<$> pQueryCommons era envCli
<*> pFormatFlags
"protocol-state query output"
[ flagFormatCborBin
, flagFormatCborHex
, flagFormatJson & setDefault
, flagFormatYaml
]
<*> pMaybeOutputFile

pAllStakePoolsOrSome :: Parser (AllOrOnly (Hash StakePoolKey))
Expand Down
83 changes: 49 additions & 34 deletions cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -936,24 +936,58 @@ runQueryProtocolStateCmd
{ Cmd.nodeConnInfo
, Cmd.target
}
, Cmd.outputFormat
, Cmd.mOutFile
}
) = do
join $
lift
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
AnyCardanoEra era <- easyRunQueryCurrentEra
() <-
join $
lift
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
AnyCardanoEra era <- easyRunQueryCurrentEra

sbe <-
requireShelleyBasedEra era
& onNothing (left QueryCmdByronEra)
sbe <-
requireShelleyBasedEra era
& onNothing (left QueryCmdByronEra)

result <- easyRunQuery (queryProtocolState sbe)
ps <- easyRunQuery (queryProtocolState sbe)

pure $ shelleyBasedEraConstraints sbe $ writeProtocolState sbe mOutFile result
)
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left
pure $ do
output <-
shelleyBasedEraConstraints sbe
$ outputFormat
& ( id
. Vary.on (\FormatCborBin -> protocolStateToCborBinary)
. Vary.on (\FormatCborHex -> fmap Base16.encode . protocolStateToCborBinary)
. Vary.on (\FormatJson -> fmap (Json.encodeJson . toJSON) . protocolStateToChainDepState sbe)
. Vary.on (\FormatYaml -> fmap (Json.encodeYaml . toJSON) . protocolStateToChainDepState sbe)
$ Vary.exhaustiveCase
)
$ ps

firstExceptT QueryCmdWriteFileError
. newExceptT
$ writeLazyByteStringOutput mOutFile output
)
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left

pure ()
where
protocolStateToChainDepState
:: ShelleyBasedEra era
-> ProtocolState era
-> ExceptT QueryCmdError IO (Consensus.ChainDepState (ConsensusProtocol era))
protocolStateToChainDepState sbe ps =
shelleyBasedEraConstraints sbe $ do
pure (decodeProtocolState ps)
& onLeft (left . QueryCmdProtocolStateDecodeFailure)

protocolStateToCborBinary
:: ProtocolState era
-> ExceptT QueryCmdError IO LBS.ByteString
protocolStateToCborBinary (ProtocolState pstate) =
pure $ unSerialised pstate

-- | Query the current delegations and reward accounts, filtered by a given
-- set of addresses, from a Shelley node via the local state query protocol.
Expand Down Expand Up @@ -1217,29 +1251,9 @@ writePoolState outputFormat mOutFile serialisedCurrentEpochState = do
. newExceptT
$ writeLazyByteStringOutput mOutFile output

writeProtocolState
:: ShelleyBasedEra era
-> Maybe (File () Out)
-> ProtocolState era
-> ExceptT QueryCmdError IO ()
writeProtocolState sbe mOutFile ps@(ProtocolState pstate) =
shelleyBasedEraConstraints sbe $
case mOutFile of
Nothing -> decodePState ps
Just (File fpath) -> writePState fpath pstate
where
writePState fpath pstate' =
handleIOExceptT (QueryCmdWriteFileError . FileIOError fpath)
. LBS.writeFile fpath
$ unSerialised pstate'
decodePState ps' =
case decodeProtocolState ps' of
Left (bs, _) -> firstExceptT QueryCmdHelpersError $ pPrintCBOR bs
Right chainDepstate -> liftIO . LBS.putStrLn $ Aeson.encodePretty chainDepstate

writeFilteredUTxOs
:: Api.ShelleyBasedEra era
-> Vary [FormatCbor, FormatJson, FormatText]
-> Vary [FormatCborBin, FormatCborHex, FormatJson, FormatText]
-> Maybe (File () Out)
-> UTxO era
-> ExceptT QueryCmdError IO ()
Expand All @@ -1248,7 +1262,8 @@ writeFilteredUTxOs sbe format mOutFile utxo = do
shelleyBasedEraConstraints sbe $
format
& ( id
. Vary.on (\FormatCbor -> Base16.encode . CBOR.serialize $ toLedgerUTxO sbe utxo)
. Vary.on (\FormatCborBin -> CBOR.serialize $ toLedgerUTxO sbe utxo)
. Vary.on (\FormatCborHex -> Base16.encode . CBOR.serialize $ toLedgerUTxO sbe utxo)
. Vary.on (\FormatJson -> Json.encodeJson utxo)
. Vary.on (\FormatText -> strictTextToLazyBytestring $ filteredUTxOsToText sbe utxo)
$ 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 (FormatCbor, FormatText)
import Cardano.CLI.Type.Common (FormatCborHex, FormatText)

import Data.Text (Text)
import Vary
Expand All @@ -22,7 +22,7 @@ newtype TextViewCmds era
data TextViewDecodeCborCmdArgs
= TextViewDecodeCborCmdArgs
{ inputFile :: !FilePath
, outputFormat :: !(Vary [FormatCbor, FormatText])
, outputFormat :: !(Vary [FormatCborHex, FormatText])
, mOutFile :: Maybe (File () Out)
}
deriving Show
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/TextView/Option.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pTextViewCmds =
<$> pCBORInFile
<*> pFormatFlags
"text view info output format"
[ flagFormatCbor
[ flagFormatCborHex
, flagFormatText & setDefault
]
<*> pMaybeOutputFile
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/TextView/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ runTextViewInfoCmd
output <-
outputFormat
& ( id
. Vary.on (\FormatCbor -> pure lbCBOR)
. Vary.on (\FormatCborHex -> pure lbCBOR)
. Vary.on (\FormatText -> LBS.fromStrict . Text.encodeUtf8 <$> cborToText lbCBOR)
$ Vary.exhaustiveCase
)
Expand Down
8 changes: 6 additions & 2 deletions cardano-cli/src/Cardano/CLI/Type/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module Cardano.CLI.Type.Common
, File (..)
, FileDirection (..)
, FormatBech32 (..)
, FormatCbor (..)
, FormatCborBin (..)
, FormatCborHex (..)
, FormatHex (..)
, FormatJson (..)
, FormatText (..)
Expand Down Expand Up @@ -479,7 +480,10 @@ data FormatBech32 = FormatBech32
data FormatHex = FormatHex
deriving (Enum, Eq, Ord, Show)

data FormatCbor = FormatCbor
data FormatCborBin = FormatCborBin
deriving (Enum, Eq, Ord, Show)

data FormatCborHex = FormatCborHex
deriving (Enum, Eq, Ord, Show)

data FormatJson = FormatJson
Expand Down
31 changes: 26 additions & 5 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@ Usage: cardano-cli query utxo [--cardano-mode [--epoch-slots SLOTS]]
| (--address ADDRESS)
| (--tx-in TX_IN)
)
[--output-cbor | --output-json | --output-text]
[ --output-cbor-bin
| --output-cbor-hex
| --output-json
| --output-text
]
[--out-file FILEPATH]

Get a portion of the current UTxO: by tx in, by address or the whole.
Expand All @@ -384,6 +388,11 @@ Usage: cardano-cli query protocol-state [--cardano-mode [--epoch-slots SLOTS]]
(--mainnet | --testnet-magic NATURAL)
--socket-path SOCKET_PATH
[--volatile-tip | --immutable-tip]
[ --output-cbor-bin
| --output-cbor-hex
| --output-json
| --output-yaml
]
[--out-file FILEPATH]

Dump the current protocol state of the node (Ledger.ChainDepState -- advanced
Expand Down Expand Up @@ -2162,6 +2171,11 @@ Usage: cardano-cli conway query protocol-state
[ --volatile-tip
| --immutable-tip
]
[ --output-cbor-bin
| --output-cbor-hex
| --output-json
| --output-yaml
]
[--out-file FILEPATH]

Dump the current protocol state of the node (Ledger.ChainDepState -- advanced
Expand Down Expand Up @@ -2374,7 +2388,8 @@ Usage: cardano-cli conway query utxo [--cardano-mode [--epoch-slots SLOTS]]
| (--address ADDRESS)
| (--tx-in TX_IN)
)
[ --output-cbor
[ --output-cbor-bin
| --output-cbor-hex
| --output-json
| --output-text
]
Expand Down Expand Up @@ -2660,7 +2675,7 @@ Usage: cardano-cli conway text-view decode-cbor
are stored on disk as TextView files.

Usage: cardano-cli conway text-view decode-cbor --in-file FILEPATH
[ --output-cbor
[ --output-cbor-hex
| --output-text
]
[--out-file FILEPATH]
Expand Down Expand Up @@ -4423,6 +4438,11 @@ Usage: cardano-cli latest query protocol-state
[ --volatile-tip
| --immutable-tip
]
[ --output-cbor-bin
| --output-cbor-hex
| --output-json
| --output-yaml
]
[--out-file FILEPATH]

Dump the current protocol state of the node (Ledger.ChainDepState -- advanced
Expand Down Expand Up @@ -4635,7 +4655,8 @@ Usage: cardano-cli latest query utxo [--cardano-mode [--epoch-slots SLOTS]]
| (--address ADDRESS)
| (--tx-in TX_IN)
)
[ --output-cbor
[ --output-cbor-bin
| --output-cbor-hex
| --output-json
| --output-text
]
Expand Down Expand Up @@ -4921,7 +4942,7 @@ Usage: cardano-cli latest text-view decode-cbor
are stored on disk as TextView files.

Usage: cardano-cli latest text-view decode-cbor --in-file FILEPATH
[ --output-cbor
[ --output-cbor-hex
| --output-text
]
[--out-file FILEPATH]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Usage: cardano-cli conway query protocol-state
[ --volatile-tip
| --immutable-tip
]
[ --output-cbor-bin
| --output-cbor-hex
| --output-json
| --output-yaml
]
[--out-file FILEPATH]

Dump the current protocol state of the node (Ledger.ChainDepState -- advanced
Expand All @@ -30,5 +35,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-cbor-bin Format protocol-state query output to CBOR.
--output-cbor-hex Format protocol-state query output to BASE16 CBOR.
--output-json Format protocol-state query output to JSON (default).
--output-yaml Format protocol-state query output to YAML.
--out-file FILEPATH 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 @@ -6,7 +6,8 @@ Usage: cardano-cli conway query utxo [--cardano-mode [--epoch-slots SLOTS]]
| (--address ADDRESS)
| (--tx-in TX_IN)
)
[ --output-cbor
[ --output-cbor-bin
| --output-cbor-hex
| --output-json
| --output-text
]
Expand Down Expand Up @@ -35,7 +36,8 @@ Available options:
testnets).
--address ADDRESS Filter by Cardano address(es) (Bech32-encoded).
--tx-in TX_IN Filter by transaction input (TxId#TxIx).
--output-cbor Format utxo query output to BASE16 CBOR.
--output-cbor-bin Format utxo query output to CBOR.
--output-cbor-hex Format utxo query output to BASE16 CBOR.
--output-json Format utxo query output to JSON (default).
--output-text Format utxo query output to TEXT.
--out-file FILEPATH Optional output file. Default is to write to stdout.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Usage: cardano-cli conway text-view decode-cbor --in-file FILEPATH
[ --output-cbor
[ --output-cbor-hex
| --output-text
]
[--out-file FILEPATH]
Expand All @@ -8,7 +8,7 @@ Usage: cardano-cli conway text-view decode-cbor --in-file FILEPATH

Available options:
--in-file FILEPATH CBOR input file.
--output-cbor Format text view info output format to BASE16 CBOR.
--output-cbor-hex Format text view info output format to BASE16 CBOR.
--output-text Format text view info output format to TEXT
(default).
--out-file FILEPATH Optional output file. Default is to write to stdout.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Usage: cardano-cli latest query protocol-state
[ --volatile-tip
| --immutable-tip
]
[ --output-cbor-bin
| --output-cbor-hex
| --output-json
| --output-yaml
]
[--out-file FILEPATH]

Dump the current protocol state of the node (Ledger.ChainDepState -- advanced
Expand All @@ -30,5 +35,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-cbor-bin Format protocol-state query output to CBOR.
--output-cbor-hex Format protocol-state query output to BASE16 CBOR.
--output-json Format protocol-state query output to JSON (default).
--output-yaml Format protocol-state query output to YAML.
--out-file FILEPATH Optional output file. Default is to write to stdout.
-h,--help Show this help text
Loading
Loading