Skip to content

Commit 2cbf6ba

Browse files
authored
Merge pull request #1192 from IntersectMBO/newhoggy/more-yaml-outputs
`yaml` output format for more commands
2 parents e0f4fa9 + 4b93f0f commit 2cbf6ba

32 files changed

+190
-47
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ data QueryTipCmdArgs = QueryTipCmdArgs
115115

116116
data QueryStakePoolsCmdArgs = QueryStakePoolsCmdArgs
117117
{ commons :: !QueryCommons
118-
, outputFormat :: !(Vary [FormatJson, FormatText])
118+
, outputFormat :: !(Vary [FormatJson, FormatText, FormatYaml])
119119
, mOutFile :: !(Maybe (File () Out))
120120
}
121121
deriving (Generic, Show)
122122

123123
data QueryStakeDistributionCmdArgs = QueryStakeDistributionCmdArgs
124124
{ commons :: !QueryCommons
125-
, outputFormat :: !(Vary [FormatJson, FormatText])
125+
, outputFormat :: !(Vary [FormatJson, FormatText, FormatYaml])
126126
, mOutFile :: !(Maybe (File () Out))
127127
}
128128
deriving (Generic, Show)
@@ -138,14 +138,14 @@ data QueryStakeAddressInfoCmdArgs = QueryStakeAddressInfoCmdArgs
138138
data QueryUTxOCmdArgs = QueryUTxOCmdArgs
139139
{ commons :: !QueryCommons
140140
, queryFilter :: !QueryUTxOFilter
141-
, outputFormat :: !(Vary [FormatCborBin, FormatCborHex, FormatJson, FormatText])
141+
, outputFormat :: !(Vary [FormatCborBin, FormatCborHex, FormatJson, FormatText, FormatYaml])
142142
, mOutFile :: !(Maybe (File () Out))
143143
}
144144
deriving (Generic, Show)
145145

146146
data QueryLedgerStateCmdArgs = QueryLedgerStateCmdArgs
147147
{ commons :: !QueryCommons
148-
, outputFormat :: !(Vary [FormatJson, FormatText])
148+
, outputFormat :: !(Vary [FormatJson, FormatText, FormatYaml])
149149
, mOutFile :: !(Maybe (File () Out))
150150
}
151151
deriving (Generic, Show)
@@ -206,7 +206,7 @@ data QuerySlotNumberCmdArgs = QuerySlotNumberCmdArgs
206206
data QueryRefScriptSizeCmdArgs = QueryRefScriptSizeCmdArgs
207207
{ commons :: !QueryCommons
208208
, transactionInputs :: !(Set TxIn)
209-
, outputFormat :: !(Vary [FormatJson, FormatText])
209+
, outputFormat :: !(Vary [FormatJson, FormatText, FormatYaml])
210210
, mOutFile :: !(Maybe (File () Out))
211211
}
212212
deriving (Generic, Show)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ pQueryUTxOCmd era envCli =
387387
, flagFormatCborHex
388388
, flagFormatJson & setDefault
389389
, flagFormatText
390+
, flagFormatYaml
390391
]
391392
<*> pMaybeOutputFile
392393

@@ -399,6 +400,7 @@ pQueryStakePoolsCmd era envCli =
399400
"stake-pools"
400401
[ flagFormatJson & setDefault
401402
, flagFormatText
403+
, flagFormatYaml
402404
]
403405
<*> pMaybeOutputFile
404406

@@ -411,6 +413,7 @@ pQueryStakeDistributionCmd era envCli =
411413
"stake-distribution"
412414
[ flagFormatJson & setDefault
413415
, flagFormatText
416+
, flagFormatYaml
414417
]
415418
<*> pMaybeOutputFile
416419

@@ -436,6 +439,7 @@ pQueryLedgerStateCmd era envCli =
436439
"ledger-state"
437440
[ flagFormatJson & setDefault
438441
, flagFormatText
442+
, flagFormatYaml
439443
]
440444
<*> pMaybeOutputFile
441445

@@ -592,6 +596,7 @@ pQueryRefScriptSizeCmd era envCli =
592596
"reference-script-size"
593597
[ flagFormatJson & setDefault
594598
, flagFormatText
599+
, flagFormatYaml
595600
]
596601
<*> pMaybeOutputFile
597602
where

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ import Cardano.Slotting.Time (RelativeTime (..), toRelativeTime)
6969

7070
import Control.Monad (forM, join)
7171
import Data.Aeson as Aeson
72-
import Data.Aeson.Encode.Pretty qualified as Aeson
7372
import Data.Bifunctor (Bifunctor (..))
7473
import Data.ByteString.Base16.Lazy qualified as Base16
7574
import Data.ByteString.Lazy qualified as BS
@@ -844,6 +843,7 @@ runQueryLedgerStateCmd
844843
& ( id
845844
. Vary.on (\FormatJson -> ledgerStateAsJsonByteString serialisedDebugLedgerState)
846845
. Vary.on (\FormatText -> ledgerStateAsTextByteString serialisedDebugLedgerState)
846+
. Vary.on (\FormatYaml -> ledgerStateAsYamlByteString serialisedDebugLedgerState)
847847
$ Vary.exhaustiveCase
848848
)
849849
)
@@ -861,7 +861,7 @@ ledgerStateAsJsonByteString
861861
ledgerStateAsJsonByteString serialisedDebugLedgerState =
862862
case decodeDebugLedgerState serialisedDebugLedgerState of
863863
Left (bs, _decoderError) -> firstExceptT QueryCmdHelpersError $ cborToTextByteString bs
864-
Right decodededgerState -> pure $ Aeson.encode decodededgerState <> "\n"
864+
Right decodededgerState -> pure $ Json.encodeJson decodededgerState <> "\n"
865865

866866
ledgerStateAsTextByteString
867867
:: Applicative f
@@ -870,6 +870,15 @@ ledgerStateAsTextByteString serialisedDebugLedgerState =
870870
let SerialisedDebugLedgerState serLedgerState = serialisedDebugLedgerState
871871
in pure $ unSerialised serLedgerState
872872

873+
ledgerStateAsYamlByteString
874+
:: IsShelleyBasedEra era
875+
=> SerialisedDebugLedgerState era
876+
-> ExceptT QueryCmdError IO LBS.ByteString
877+
ledgerStateAsYamlByteString serialisedDebugLedgerState =
878+
case decodeDebugLedgerState serialisedDebugLedgerState of
879+
Left (bs, _decoderError) -> firstExceptT QueryCmdHelpersError $ cborToTextByteString bs
880+
Right decodededgerState -> pure $ Json.encodeYaml decodededgerState
881+
873882
runQueryLedgerPeerSnapshot
874883
:: ()
875884
=> Cmd.QueryLedgerPeerSnapshotCmdArgs
@@ -1250,7 +1259,7 @@ writePoolState outputFormat mOutFile serialisedCurrentEpochState = do
12501259

12511260
writeFilteredUTxOs
12521261
:: Api.ShelleyBasedEra era
1253-
-> Vary [FormatCborBin, FormatCborHex, FormatJson, FormatText]
1262+
-> Vary [FormatCborBin, FormatCborHex, FormatJson, FormatText, FormatYaml]
12541263
-> Maybe (File () Out)
12551264
-> UTxO era
12561265
-> ExceptT QueryCmdError IO ()
@@ -1263,6 +1272,7 @@ writeFilteredUTxOs sbe format mOutFile utxo = do
12631272
. Vary.on (\FormatCborHex -> Base16.encode . CBOR.serialize $ toLedgerUTxO sbe utxo)
12641273
. Vary.on (\FormatJson -> Json.encodeJson utxo)
12651274
. Vary.on (\FormatText -> strictTextToLazyBytestring $ filteredUTxOsToText sbe utxo)
1275+
. Vary.on (\FormatYaml -> Json.encodeYaml utxo)
12661276
$ Vary.exhaustiveCase
12671277
)
12681278

@@ -1385,35 +1395,35 @@ runQueryStakePoolsCmd
13851395

13861396
-- TODO: replace with writeFormattedOutput
13871397
writeStakePools
1388-
:: Vary [FormatJson, FormatText]
1398+
:: Vary [FormatJson, FormatText, FormatYaml]
13891399
-> Maybe (File () Out)
13901400
-> Set PoolId
13911401
-> ExceptT QueryCmdError IO ()
13921402
writeStakePools format mOutFile stakePools = do
13931403
let output =
13941404
format
13951405
& ( id
1396-
. Vary.on (\FormatJson -> writeJson)
1397-
. Vary.on (\FormatText -> writeText)
1406+
. Vary.on (\FormatJson -> Json.encodeJson)
1407+
. Vary.on (\FormatText -> encodeText)
1408+
. Vary.on (\FormatYaml -> Json.encodeYaml)
13981409
$ Vary.exhaustiveCase
13991410
)
1411+
$ stakePools
14001412

14011413
firstExceptT QueryCmdWriteFileError
14021414
. newExceptT
14031415
$ writeLazyByteStringOutput mOutFile output
14041416
where
1405-
writeJson =
1406-
Aeson.encodePretty stakePools
1407-
writeText =
1408-
LBS.unlines $
1409-
map (strictTextToLazyBytestring . serialiseToBech32) $
1410-
toList stakePools
1417+
encodeText =
1418+
LBS.unlines
1419+
. map (strictTextToLazyBytestring . serialiseToBech32)
1420+
. toList
14111421

14121422
writeFormattedOutput
14131423
:: MonadIOTransError QueryCmdError t m
14141424
=> ToJSON a
14151425
=> Pretty a
1416-
=> Vary [FormatJson, FormatText]
1426+
=> Vary [FormatJson, FormatText, FormatYaml]
14171427
-> Maybe (File b Out)
14181428
-> a
14191429
-> t m ()
@@ -1423,6 +1433,7 @@ writeFormattedOutput format mOutFile value = do
14231433
& ( id
14241434
. Vary.on (\FormatJson -> Json.encodeJson value)
14251435
. Vary.on (\FormatText -> fromString . docToString $ pretty value)
1436+
. Vary.on (\FormatYaml -> Json.encodeYaml value)
14261437
$ Vary.exhaustiveCase
14271438
)
14281439

@@ -1462,7 +1473,7 @@ runQueryStakeDistributionCmd
14621473
& onLeft left
14631474

14641475
writeStakeDistribution
1465-
:: Vary [FormatJson, FormatText]
1476+
:: Vary [FormatJson, FormatText, FormatYaml]
14661477
-> Maybe (File () Out)
14671478
-> Map PoolId Rational
14681479
-> ExceptT QueryCmdError IO ()
@@ -1472,6 +1483,7 @@ writeStakeDistribution format mOutFile stakeDistrib = do
14721483
& ( id
14731484
. Vary.on (\FormatJson -> Json.encodeJson stakeDistrib)
14741485
. Vary.on (\FormatText -> strictTextToLazyBytestring stakeDistributionText)
1486+
. Vary.on (\FormatYaml -> Json.encodeYaml stakeDistrib)
14751487
$ Vary.exhaustiveCase
14761488
)
14771489

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ where
1010

1111
import Cardano.Api.Shelley
1212

13-
import Cardano.CLI.Type.Common (FormatCborHex, FormatText)
13+
import Cardano.CLI.Type.Common (FormatCborHex, FormatJson, FormatText, FormatYaml)
1414

1515
import Data.Text (Text)
1616
import Vary
@@ -22,7 +22,7 @@ newtype TextViewCmds era
2222
data TextViewDecodeCborCmdArgs
2323
= TextViewDecodeCborCmdArgs
2424
{ inputFile :: !FilePath
25-
, outputFormat :: !(Vary [FormatCborHex, FormatText])
25+
, outputFormat :: !(Vary [FormatCborHex, FormatJson, FormatText, FormatYaml])
2626
, mOutFile :: Maybe (File () Out)
2727
}
2828
deriving Show

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ pTextViewCmds =
4141
<*> pFormatFlags
4242
"text view info output format"
4343
[ flagFormatCborHex
44+
, flagFormatJson
4445
, flagFormatText & setDefault
46+
, flagFormatYaml
4547
]
4648
<*> pMaybeOutputFile
4749
)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Cardano.Api
1414

1515
import Cardano.CLI.EraBased.TextView.Command
1616
import Cardano.CLI.Helper (cborToText)
17+
import Cardano.CLI.Json.Encode qualified as Json
1718
import Cardano.CLI.Type.Common
1819
import Cardano.CLI.Type.Error.TextViewFileError
1920

@@ -43,7 +44,9 @@ runTextViewInfoCmd
4344
outputFormat
4445
& ( id
4546
. Vary.on (\FormatCborHex -> pure lbCBOR)
47+
. Vary.on (\FormatJson -> pure $ Json.encodeJson tv)
4648
. Vary.on (\FormatText -> LBS.fromStrict . Text.encodeUtf8 <$> cborToText lbCBOR)
49+
. Vary.on (\FormatYaml -> pure $ Json.encodeYaml tv)
4750
$ Vary.exhaustiveCase
4851
)
4952
& firstExceptT TextViewCBORPrettyPrintError

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ data TransactionCalculateMinFeeCmdArgs = TransactionCalculateMinFeeCmdArgs
252252
, txByronWitnessCount :: !TxByronWitnessCount
253253
, referenceScriptSize :: !ReferenceScriptSize
254254
-- ^ The total size in bytes of the transaction reference scripts.
255-
, outputFormat :: !(Vary [FormatJson, FormatText])
255+
, outputFormat :: !(Vary [FormatJson, FormatText, FormatYaml])
256256
, outFile :: !(Maybe (File () Out))
257257
}
258258
deriving Show
@@ -314,7 +314,7 @@ newtype TransactionHashScriptDataCmdArgs = TransactionHashScriptDataCmdArgs
314314

315315
data TransactionTxIdCmdArgs = TransactionTxIdCmdArgs
316316
{ inputTxBodyOrTxFile :: InputTxBodyOrTxFile
317-
, outputFormat :: !(Vary [FormatJson, FormatText])
317+
, outputFormat :: !(Vary [FormatJson, FormatText, FormatYaml])
318318
}
319319
deriving Show
320320

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ pTransactionCalculateMinFee =
387387
"calculate-min-fee query output"
388388
[ flagFormatJson & setDefault
389389
, flagFormatText
390+
, flagFormatYaml
390391
]
391392
<*> optional pOutputFile
392393
-- Deprecated options:
@@ -546,6 +547,7 @@ pTransactionId =
546547
"output"
547548
[ flagFormatJson & setDefault
548549
, flagFormatText
550+
, flagFormatYaml
549551
]
550552

551553
pIsCborOutCanonical :: Parser TxCborFormat

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import Cardano.CLI.EraBased.Transaction.Internal.HashCheck
7777
, checkProposalHashes
7878
, checkVotingProcedureHashes
7979
)
80+
import Cardano.CLI.Json.Encode qualified as Json
8081
import Cardano.CLI.Orphan ()
8182
import Cardano.CLI.Read
8283
import Cardano.CLI.Type.Common
@@ -1530,16 +1531,18 @@ runTransactionCalculateMinFeeCmd
15301531

15311532
let fee = shelleyfee + byronfee
15321533
textToWrite = docToText $ pretty fee
1533-
jsonToWrite = encodePretty $ Aeson.object ["fee" .= fee]
1534+
content = Aeson.object ["fee" .= fee]
15341535

15351536
outputFormat
15361537
& ( id
15371538
. Vary.on
15381539
( \FormatJson -> case outFile of
15391540
Nothing ->
1540-
liftIO $ LBS.putStrLn jsonToWrite
1541+
liftIO $ LBS.putStrLn $ Json.encodeJson content
15411542
Just file ->
1542-
firstExceptT TxCmdWriteFileError . newExceptT $ writeLazyByteStringFile file jsonToWrite
1543+
firstExceptT TxCmdWriteFileError . newExceptT $
1544+
writeLazyByteStringFile file $
1545+
Json.encodeJson content
15431546
)
15441547
. Vary.on
15451548
( \FormatText -> case outFile of
@@ -1548,6 +1551,15 @@ runTransactionCalculateMinFeeCmd
15481551
Just file ->
15491552
firstExceptT TxCmdWriteFileError . newExceptT $ writeTextFile file textToWrite
15501553
)
1554+
. Vary.on
1555+
( \FormatYaml -> case outFile of
1556+
Nothing ->
1557+
liftIO $ LBS.putStrLn $ Json.encodeYaml content
1558+
Just file ->
1559+
firstExceptT TxCmdWriteFileError . newExceptT $
1560+
writeLazyByteStringFile file $
1561+
Json.encodeYaml content
1562+
)
15511563
$ Vary.exhaustiveCase
15521564
)
15531565

@@ -1825,8 +1837,9 @@ runTransactionTxIdCmd
18251837
liftIO $
18261838
outputFormat
18271839
& ( id
1828-
. Vary.on (\FormatJson -> LBS.putStrLn $ Aeson.encode $ TxSubmissionResult txId)
1840+
. Vary.on (\FormatJson -> LBS.putStrLn $ Json.encodeJson $ TxSubmissionResult txId)
18291841
. Vary.on (\FormatText -> BS.putStrLn $ serialiseToRawBytesHex txId)
1842+
. Vary.on (\FormatYaml -> LBS.putStrLn $ Json.encodeYaml $ TxSubmissionResult txId)
18301843
$ Vary.exhaustiveCase
18311844
)
18321845

0 commit comments

Comments
 (0)