Skip to content

Commit 4feede6

Browse files
authored
Merge pull request #1143 from IntersectMBO/newhoggy/standardise-default-output-format-for-query-commands
Standardise default output format for commands
2 parents 0126543 + c6e2b72 commit 4feede6

37 files changed

+130
-194
lines changed

cardano-cli/src/Cardano/CLI/EraBased/Common/Option.hs

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ import Data.Text (Text)
5757
import Data.Text qualified as Text
5858
import Data.Time.Clock (UTCTime)
5959
import Data.Time.Format (defaultTimeLocale, parseTimeOrError)
60-
import Data.Type.Equality
6160
import Data.Word
6261
import GHC.Exts (IsList (..))
6362
import GHC.Natural (Natural)
@@ -74,7 +73,6 @@ import Text.Parsec.Token qualified as Parsec
7473
import Text.Read (readEither, readMaybe)
7574
import Text.Read qualified as Read
7675

77-
import Type.Reflection qualified as TR
7876
import Vary (Vary, (:|))
7977
import Vary qualified
8078

@@ -1770,15 +1768,6 @@ pPoolIdOutputFormat =
17701768
, Opt.value IdOutputFormatBech32
17711769
]
17721770

1773-
-- | @pOutputFormatJsonOrText kind@ is a parser to specify in which format
1774-
-- to view some data (json or text). @kind@ is the kind of data considered.
1775-
pOutputFormatJsonOrText :: String -> Parser (Vary [FormatJson, FormatText])
1776-
pOutputFormatJsonOrText kind =
1777-
asum
1778-
[ make' FormatJson "JSON" "json" (Just " Default format when writing to a file") kind
1779-
, make' FormatText "TEXT" "text" (Just " Default format when writing to stdout") kind
1780-
]
1781-
17821771
-- | Make a parser for an output format.
17831772
make'
17841773
:: a :| fs
@@ -1845,74 +1834,6 @@ flagFormatYaml
18451834
flagFormatYaml =
18461835
mkFlag "output-yaml" "YAML" FormatYaml
18471836

1848-
-- | @pTxIdOutputFormatJsonOrText kind@ is a parser to specify in which format
1849-
-- to write @transaction txid@'s output on standard output.
1850-
pTxIdOutputFormatJsonOrText :: Parser (Vary [FormatJson, FormatText])
1851-
pTxIdOutputFormatJsonOrText =
1852-
asum [make FormatJson "JSON" "json", make FormatText "TEXT" "text"]
1853-
<|> pure (Vary.from default_)
1854-
where
1855-
default_ :: FormatText
1856-
default_ = FormatText
1857-
make
1858-
:: forall a fs
1859-
. a :| fs
1860-
=> Typeable a
1861-
=> a
1862-
-> String
1863-
-> String
1864-
-> Parser (Vary fs)
1865-
make format desc flag_ =
1866-
Opt.flag' (Vary.from format) $
1867-
mconcat
1868-
[ Opt.help $ "Format output as " <> desc <> maybeDefault <> "."
1869-
, Opt.long ("output-" <> flag_)
1870-
]
1871-
where
1872-
maybeDefault =
1873-
case TR.eqTypeRep (TR.typeRep @a) (TR.typeRep @FormatText) of
1874-
Just HRefl -> " (the default)"
1875-
Nothing -> ""
1876-
1877-
pTxViewOutputFormat :: Parser (Vary [FormatJson, FormatYaml])
1878-
pTxViewOutputFormat = pViewOutputFormat "transaction"
1879-
1880-
pGovernanceActionViewOutputFormat :: Parser (Vary [FormatJson, FormatYaml])
1881-
pGovernanceActionViewOutputFormat = pViewOutputFormat "governance action"
1882-
1883-
pGovernanceVoteViewOutputFormat :: Parser (Vary [FormatJson, FormatYaml])
1884-
pGovernanceVoteViewOutputFormat = pViewOutputFormat "governance vote"
1885-
1886-
-- | @pViewOutputFormat kind@ is a parser to specify in which format
1887-
-- to view some data (json or yaml). @what@ is the kind of data considered.
1888-
pViewOutputFormat :: String -> Parser (Vary [FormatJson, FormatYaml])
1889-
pViewOutputFormat kind =
1890-
asum
1891-
[ make FormatJson "JSON" "json" Nothing
1892-
, make FormatYaml "YAML" "yaml" (Just " Defaults to JSON if unspecified.")
1893-
]
1894-
where
1895-
make
1896-
:: a :| fs
1897-
=> FormatJson :| fs
1898-
=> a
1899-
-> String
1900-
-> String
1901-
-> Maybe String
1902-
-> Parser (Vary fs)
1903-
make format desc flag_ extraHelp =
1904-
Opt.flag (Vary.from FormatJson) (Vary.from format) $
1905-
mconcat
1906-
[ Opt.help $
1907-
"Format "
1908-
<> kind
1909-
<> " view output to "
1910-
<> desc
1911-
<> "."
1912-
<> fromMaybe "" extraHelp
1913-
, Opt.long ("output-" <> flag_)
1914-
]
1915-
19161837
pMaybeOutputFile :: Parser (Maybe (File content Out))
19171838
pMaybeOutputFile =
19181839
optional $

cardano-cli/src/Cardano/CLI/EraBased/Governance/Actions/Option.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import Cardano.Api.Shelley
1313

1414
import Cardano.CLI.EraBased.Common.Option
1515
import Cardano.CLI.EraBased.Governance.Actions.Command qualified as Cmd
16+
import Cardano.CLI.Option.Flag (setDefault)
1617
import Cardano.CLI.Parser
1718
import Cardano.CLI.Type.Common
1819

1920
import Data.Foldable
21+
import Data.Function ((&))
2022
import GHC.Natural (Natural)
2123
import Options.Applicative
2224
import Options.Applicative qualified as Opt
@@ -55,7 +57,11 @@ pGovernanceActionViewCmd era = do
5557
( fmap Cmd.GovernanceActionViewCmd $
5658
Cmd.GovernanceActionViewCmdArgs eon
5759
<$> pFileInDirection "action-file" "Path to action file."
58-
<*> pGovernanceActionViewOutputFormat
60+
<*> pFormatFlags
61+
"governance action view output"
62+
[ flagFormatJson & setDefault
63+
, flagFormatYaml
64+
]
5965
<*> pMaybeOutputFile
6066
)
6167
$ Opt.progDesc "View a governance action."

cardano-cli/src/Cardano/CLI/EraBased/Governance/Vote/Option.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ import Cardano.CLI.EraBased.Governance.Vote.Command
1414
, GovernanceVoteCreateCmdArgs (GovernanceVoteCreateCmdArgs)
1515
, GovernanceVoteViewCmdArgs (GovernanceVoteViewCmdArgs)
1616
)
17+
import Cardano.CLI.Option.Flag (setDefault)
1718
import Cardano.CLI.Parser
1819
import Cardano.CLI.Type.Governance
1920

2021
import Control.Applicative (optional)
2122
import Data.Foldable
23+
import Data.Function ((&))
2224
import Options.Applicative (Parser)
2325
import Options.Applicative qualified as Opt
2426

@@ -89,6 +91,10 @@ pGovernanceVoteViewCmd era = do
8991
pGovernanceVoteViewCmdArgs :: ConwayEraOnwards era -> Parser (GovernanceVoteViewCmdArgs era)
9092
pGovernanceVoteViewCmdArgs cOnwards =
9193
GovernanceVoteViewCmdArgs cOnwards
92-
<$> pGovernanceVoteViewOutputFormat
94+
<$> pFormatFlags
95+
"governance vote view output"
96+
[ flagFormatJson & setDefault
97+
, flagFormatYaml
98+
]
9399
<*> pFileInDirection "vote-file" "Input filepath of the vote."
94100
<*> pMaybeOutputFile

cardano-cli/src/Cardano/CLI/EraBased/Query/Command.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ data QueryLeadershipScheduleCmdArgs = QueryLeadershipScheduleCmdArgs
9393
, poolColdVerKeyFile :: !(VerificationKeyOrHashOrFile StakePoolKey)
9494
, vrkSkeyFp :: !(SigningKeyFile In)
9595
, whichSchedule :: !EpochLeadershipSchedule
96-
, format :: Maybe (Vary [FormatJson, FormatText])
96+
, format :: !(Vary [FormatJson, FormatText])
9797
, mOutFile :: !(Maybe (File () Out))
9898
}
9999
deriving (Generic, Show)
@@ -112,14 +112,14 @@ data QueryTipCmdArgs = QueryTipCmdArgs
112112

113113
data QueryStakePoolsCmdArgs = QueryStakePoolsCmdArgs
114114
{ commons :: !QueryCommons
115-
, format :: Maybe (Vary [FormatJson, FormatText])
115+
, format :: !(Vary [FormatJson, FormatText])
116116
, mOutFile :: !(Maybe (File () Out))
117117
}
118118
deriving (Generic, Show)
119119

120120
data QueryStakeDistributionCmdArgs = QueryStakeDistributionCmdArgs
121121
{ commons :: !QueryCommons
122-
, format :: Maybe (Vary [FormatJson, FormatText])
122+
, format :: !(Vary [FormatJson, FormatText])
123123
, mOutFile :: !(Maybe (File () Out))
124124
}
125125
deriving (Generic, Show)
@@ -134,7 +134,7 @@ data QueryStakeAddressInfoCmdArgs = QueryStakeAddressInfoCmdArgs
134134
data QueryUTxOCmdArgs = QueryUTxOCmdArgs
135135
{ commons :: !QueryCommons
136136
, queryFilter :: !QueryUTxOFilter
137-
, format :: Vary [FormatCbor, FormatJson, FormatText]
137+
, format :: !(Vary [FormatCbor, FormatJson, FormatText])
138138
, mOutFile :: !(Maybe (File () Out))
139139
}
140140
deriving (Generic, Show)
@@ -195,7 +195,7 @@ data QuerySlotNumberCmdArgs = QuerySlotNumberCmdArgs
195195
data QueryRefScriptSizeCmdArgs = QueryRefScriptSizeCmdArgs
196196
{ commons :: !QueryCommons
197197
, transactionInputs :: !(Set TxIn)
198-
, format :: Maybe (Vary [FormatJson, FormatText])
198+
, format :: !(Vary [FormatJson, FormatText])
199199
, mOutFile :: !(Maybe (File () Out))
200200
}
201201
deriving (Generic, Show)

cardano-cli/src/Cardano/CLI/EraBased/Query/Option.hs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,23 @@ pQueryStakePoolsCmd era envCli =
381381
fmap QueryStakePoolsCmd $
382382
QueryStakePoolsCmdArgs
383383
<$> pQueryCommons era envCli
384-
<*> (optional $ pOutputFormatJsonOrText "stake-pools")
384+
<*> pFormatFlags
385+
"stake-pools query output"
386+
[ flagFormatJson & setDefault
387+
, flagFormatText
388+
]
385389
<*> pMaybeOutputFile
386390

387391
pQueryStakeDistributionCmd :: ShelleyBasedEra era -> EnvCli -> Parser (QueryCmds era)
388392
pQueryStakeDistributionCmd era envCli =
389393
fmap QueryStakeDistributionCmd $
390394
QueryStakeDistributionCmdArgs
391395
<$> pQueryCommons era envCli
392-
<*> (optional $ pOutputFormatJsonOrText "stake-distribution")
396+
<*> pFormatFlags
397+
"stake-distribution query output"
398+
[ flagFormatJson & setDefault
399+
, flagFormatText
400+
]
393401
<*> pMaybeOutputFile
394402

395403
pQueryStakeAddressInfoCmd :: ShelleyBasedEra era -> EnvCli -> Parser (QueryCmds era)
@@ -488,7 +496,11 @@ pLeadershipScheduleCmd era envCli =
488496
<*> pStakePoolVerificationKeyOrHashOrFile Nothing
489497
<*> pVrfSigningKeyFile
490498
<*> pWhichLeadershipSchedule
491-
<*> (optional $ pOutputFormatJsonOrText "leadership-schedule")
499+
<*> pFormatFlags
500+
"leadership-schedule query output"
501+
[ flagFormatJson & setDefault
502+
, flagFormatText
503+
]
492504
<*> pMaybeOutputFile
493505

494506
pKesPeriodInfoCmd :: ShelleyBasedEra era -> EnvCli -> Parser (QueryCmds era)
@@ -519,7 +531,11 @@ pQueryRefScriptSizeCmd era envCli =
519531
QueryRefScriptSizeCmdArgs
520532
<$> pQueryCommons era envCli
521533
<*> (fromList <$> some pByTxIn)
522-
<*> (optional $ pOutputFormatJsonOrText "reference inputs")
534+
<*> pFormatFlags
535+
"reference inputs query output"
536+
[ flagFormatJson & setDefault
537+
, flagFormatText
538+
]
523539
<*> pMaybeOutputFile
524540
where
525541
pByTxIn :: Parser TxIn

cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ module Cardano.CLI.EraBased.Query.Run
3333
, runQueryTxMempoolCmd
3434
, runQueryUTxOCmd
3535
, DelegationsAndRewards (..)
36-
, newOutputFormat
3736
, renderQueryCmdError
3837
, renderOpCertIntervalInformation
3938
, percentage
@@ -1300,7 +1299,7 @@ runQueryStakePoolsCmd
13001299

13011300
poolIds <- easyRunQuery (queryStakePools sbe)
13021301

1303-
pure $ writeStakePools (newOutputFormat format mOutFile) mOutFile poolIds
1302+
pure $ writeStakePools format mOutFile poolIds
13041303
)
13051304
& onLeft (left . QueryCmdAcquireFailure)
13061305
& onLeft left
@@ -1333,16 +1332,16 @@ writeFormattedOutput
13331332
:: MonadIOTransError QueryCmdError t m
13341333
=> ToJSON a
13351334
=> Pretty a
1336-
=> Maybe (Vary [FormatJson, FormatText])
1335+
=> Vary [FormatJson, FormatText]
13371336
-> Maybe (File b Out)
13381337
-> a
13391338
-> t m ()
1340-
writeFormattedOutput mFormat mOutFile value =
1339+
writeFormattedOutput format mOutFile value =
13411340
modifyError QueryCmdWriteFileError . hoistIOEither $
13421341
writeLazyByteStringOutput mOutFile toWrite
13431342
where
13441343
toWrite :: LBS.ByteString =
1345-
newOutputFormat mFormat mOutFile
1344+
format
13461345
& ( id
13471346
. Vary.on (\FormatJson -> encodePretty value)
13481347
. Vary.on (\FormatText -> fromString . docToString $ pretty value)
@@ -1375,7 +1374,7 @@ runQueryStakeDistributionCmd
13751374
result <- easyRunQuery (queryStakeDistribution sbe)
13761375

13771376
pure $ do
1378-
writeStakeDistribution (newOutputFormat format mOutFile) mOutFile result
1377+
writeStakeDistribution format mOutFile result
13791378
)
13801379
& onLeft (left . QueryCmdAcquireFailure)
13811380
& onLeft left
@@ -1515,7 +1514,7 @@ runQueryLeadershipScheduleCmd
15151514
where
15161515
start = SystemStart $ sgSystemStart shelleyGenesis
15171516
toWrite =
1518-
newOutputFormat format mOutFile'
1517+
format
15191518
& ( id
15201519
. Vary.on (\FormatJson -> encodePretty $ leadershipScheduleToJson schedule eInfo start)
15211520
. Vary.on (\FormatText -> strictTextToLazyBytestring $ leadershipScheduleToText schedule eInfo start)
@@ -1987,19 +1986,6 @@ requireEon minEra era =
19871986
(mkEraMismatchError NodeEraMismatchError{nodeEra = era, era = minEra})
19881987
(forEraMaybeEon era)
19891988

1990-
-- | The output format to use, for commands with a recently introduced --output-[json,text] flag
1991-
-- and that used to have the following default: --out-file implies JSON,
1992-
-- output to stdout implied text.
1993-
newOutputFormat
1994-
:: Maybe (Vary [FormatJson, FormatText])
1995-
-> Maybe a
1996-
-> Vary [FormatJson, FormatText]
1997-
newOutputFormat format mOutFile =
1998-
case (format, mOutFile) of
1999-
(Just f, _) -> f -- Take flag from CLI if specified
2000-
(Nothing, Nothing) -> Vary.from FormatText -- No CLI flag, writing to stdout: write text
2001-
(Nothing, Just _) -> Vary.from FormatJson -- No CLI flag, writing to a file: write JSON
2002-
20031989
strictTextToLazyBytestring :: Text -> LBS.ByteString
20041990
strictTextToLazyBytestring t = BS.fromChunks [Text.encodeUtf8 t]
20051991

cardano-cli/src/Cardano/CLI/EraBased/Transaction/Command.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ data TransactionCalculateMinFeeCmdArgs = TransactionCalculateMinFeeCmdArgs
248248
, txByronWitnessCount :: !TxByronWitnessCount
249249
, referenceScriptSize :: !ReferenceScriptSize
250250
-- ^ The total size in bytes of the transaction reference scripts.
251-
, outputFormat :: !(Maybe (Vary [FormatJson, FormatText]))
251+
, outputFormat :: !(Vary [FormatJson, FormatText])
252252
, outFile :: !(Maybe (File () Out))
253253
}
254254
deriving Show

cardano-cli/src/Cardano/CLI/EraBased/Transaction/Option.hs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ import Cardano.Api.Experimental qualified as Exp
1616
import Cardano.CLI.Environment (EnvCli (..))
1717
import Cardano.CLI.EraBased.Common.Option
1818
import Cardano.CLI.EraBased.Transaction.Command
19+
import Cardano.CLI.Option.Flag
1920
import Cardano.CLI.Parser
2021
import Cardano.CLI.Type.Common
2122

2223
import Control.Monad
2324
import Data.Foldable
25+
import Data.Function ((&))
2426
import Data.Functor
2527
import Options.Applicative hiding (help, str)
2628
import Options.Applicative qualified as Opt
@@ -380,7 +382,11 @@ pTransactionCalculateMinFee =
380382
<*> pTxShelleyWitnessCount
381383
<*> pTxByronWitnessCount
382384
<*> pReferenceScriptSize
383-
<*> (optional $ pOutputFormatJsonOrText "calculate-min-fee")
385+
<*> pFormatFlags
386+
"calculate-min-fee query output"
387+
[ flagFormatJson & setDefault
388+
, flagFormatText
389+
]
384390
<*> optional pOutputFile
385391
-- Deprecated options:
386392
<* optional pNetworkIdDeprecated
@@ -423,7 +429,11 @@ pTransactionId =
423429
fmap TransactionTxIdCmd $
424430
TransactionTxIdCmdArgs
425431
<$> pInputTxOrTxBodyFile
426-
<*> pTxIdOutputFormatJsonOrText
432+
<*> pFormatFlags
433+
"output"
434+
[ flagFormatJson & setDefault
435+
, flagFormatText
436+
]
427437

428438
pIsCborOutCanonical :: Parser TxCborFormat
429439
pIsCborOutCanonical =

cardano-cli/src/Cardano/CLI/EraBased/Transaction/Run.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ import Cardano.Api.Shelley
4949

5050
import Cardano.Binary qualified as CBOR
5151
import Cardano.CLI.EraBased.Genesis.Internal.Common (readProtocolParameters)
52-
import Cardano.CLI.EraBased.Query.Run
5352
import Cardano.CLI.EraBased.Script.Certificate.Read
5453
import Cardano.CLI.EraBased.Script.Certificate.Type (CertificateScriptWitness (..))
5554
import Cardano.CLI.EraBased.Script.Mint.Read
@@ -1579,7 +1578,7 @@ runTransactionCalculateMinFeeCmd
15791578
textToWrite = docToText $ pretty fee
15801579
jsonToWrite = encodePretty $ Aeson.object ["fee" .= fee]
15811580

1582-
newOutputFormat outputFormat outFile
1581+
outputFormat
15831582
& ( id
15841583
. Vary.on
15851584
( \FormatJson -> case outFile of

0 commit comments

Comments
 (0)