Skip to content

Commit 12ab477

Browse files
committed
New --output-* switches for text-view info command
1 parent 3c09779 commit 12ab477

File tree

8 files changed

+83
-9
lines changed

8 files changed

+83
-9
lines changed

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

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

1111
import Cardano.Api.Shelley
1212

13+
import Cardano.CLI.Type.Common (FormatCbor, FormatText)
14+
1315
import Data.Text (Text)
16+
import Vary
1417

1518
newtype TextViewCmds era
1619
= TextViewInfoCmd TextViewInfoCmdArgs
@@ -19,6 +22,7 @@ newtype TextViewCmds era
1922
data TextViewInfoCmdArgs
2023
= TextViewInfoCmdArgs
2124
{ inputFile :: !FilePath
25+
, outputFormat :: !(Vary [FormatCbor, FormatText])
2226
, mOutFile :: Maybe (File () Out)
2327
}
2428
deriving Show

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ where
1010

1111
import Cardano.CLI.EraBased.Common.Option
1212
import Cardano.CLI.EraBased.TextView.Command
13+
import Cardano.CLI.Option.Flag
1314
import Cardano.CLI.Parser
1415

16+
import Data.Function ((&))
1517
import Options.Applicative hiding (help, str)
1618
import Options.Applicative qualified as Opt
1719

@@ -28,9 +30,20 @@ pTextViewCmds =
2830
, "are stored on disk as TextView files."
2931
]
3032
)
31-
[ Just $
32-
Opt.hsubparser $
33-
commandWithMetavar "decode-cbor" $
34-
Opt.info (fmap TextViewInfoCmd (TextViewInfoCmdArgs <$> pCBORInFile <*> pMaybeOutputFile)) $
35-
Opt.progDesc "Print a TextView file as decoded CBOR."
33+
[ Just
34+
. Opt.hsubparser
35+
. commandWithMetavar "decode-cbor"
36+
$ Opt.info
37+
( fmap
38+
TextViewInfoCmd
39+
$ TextViewInfoCmdArgs
40+
<$> pCBORInFile
41+
<*> pFormatFlags
42+
"text view info output format"
43+
[ flagFormatCbor
44+
, flagFormatText & setDefault
45+
]
46+
<*> pMaybeOutputFile
47+
)
48+
$ Opt.progDesc "Print a TextView file as decoded CBOR."
3649
]

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
{-# LANGUAGE LambdaCase #-}
33
{-# LANGUAGE NamedFieldPuns #-}
44

5+
{- HLINT ignore "Redundant id" -}
6+
57
module Cardano.CLI.EraBased.TextView.Run
68
( runTextViewCmds
79
, runTextViewInfoCmd
@@ -11,10 +13,15 @@ where
1113
import Cardano.Api
1214

1315
import Cardano.CLI.EraBased.TextView.Command
14-
import Cardano.CLI.Helper (pPrintCBOR)
16+
import Cardano.CLI.Helper (cborToText)
17+
import Cardano.CLI.OS.Posix qualified as IO
18+
import Cardano.CLI.Type.Common
1519
import Cardano.CLI.Type.Error.TextViewFileError
1620

1721
import Data.ByteString.Lazy.Char8 qualified as LBS
22+
import Data.Function ((&))
23+
import Data.Text.Encoding qualified as Text
24+
import Vary qualified
1825

1926
runTextViewCmds :: TextViewCmds era -> ExceptT TextViewFileError IO ()
2027
runTextViewCmds = \case
@@ -27,10 +34,21 @@ runTextViewInfoCmd
2734
runTextViewInfoCmd
2835
TextViewInfoCmdArgs
2936
{ inputFile
37+
, outputFormat
3038
, mOutFile
3139
} = do
3240
tv <- firstExceptT TextViewReadFileError $ newExceptT (readTextEnvelopeFromFile inputFile)
3341
let lbCBOR = LBS.fromStrict (textEnvelopeRawCBOR tv)
34-
case mOutFile of
35-
Just (File oFpath) -> liftIO $ LBS.writeFile oFpath lbCBOR
36-
Nothing -> firstExceptT TextViewCBORPrettyPrintError $ pPrintCBOR lbCBOR
42+
43+
outputContent <-
44+
outputFormat
45+
& ( id
46+
. Vary.on (\FormatCbor -> pure lbCBOR)
47+
. Vary.on (\FormatText -> LBS.fromStrict . Text.encodeUtf8 <$> cborToText lbCBOR)
48+
$ Vary.exhaustiveCase
49+
)
50+
& firstExceptT TextViewCBORPrettyPrintError
51+
52+
let writeOutput = maybe (LBS.hPut IO.stdout) (LBS.writeFile . unFile) mOutFile
53+
54+
liftIO $ writeOutput outputContent

cardano-cli/src/Cardano/CLI/Helper.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
module Cardano.CLI.Helper
88
( HelpersError (..)
9+
, cborToText
910
, printWarning
1011
, deprecationWarning
1112
, ensureNewFile
@@ -106,6 +107,25 @@ pPrintCBOR bs = do
106107
unless (LB.null remaining) $
107108
pPrintCBOR remaining
108109

110+
cborToText :: LB.ByteString -> ExceptT HelpersError IO Text
111+
cborToText bs = do
112+
as <- cborToTextList bs
113+
let cs = filter (not . Text.null) as
114+
pure $ mconcat $ fmap (<> "\n") cs
115+
116+
cborToTextList :: LB.ByteString -> ExceptT HelpersError IO [Text]
117+
cborToTextList bs = do
118+
case deserialiseFromBytes decodeTerm bs of
119+
Left err -> left $ CBORPrettyPrintError err
120+
Right (remaining, decodedVal) -> do
121+
let text = Text.pack . prettyHexEnc $ encodeTerm decodedVal
122+
123+
if LB.null remaining
124+
then pure [text]
125+
else do
126+
extraText <- cborToTextList remaining
127+
pure $ text : extraText
128+
109129
readCBOR :: FilePath -> ExceptT HelpersError IO LB.ByteString
110130
readCBOR fp =
111131
handleIOExceptT

cardano-cli/test/cardano-cli-golden/Test/Golden/Shelley/TextView/DecodeCbor.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ hprop_golden_shelleyTextViewDecodeCbor = propertyOnce $ H.moduleWorkspace "tmp"
2222
[ "latest"
2323
, "text-view"
2424
, "decode-cbor"
25+
, "--output-text"
2526
, "--file"
2627
, unsignedTxFile
2728
]

cardano-cli/test/cardano-cli-golden/files/golden/help.cli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,9 @@ Usage: cardano-cli conway text-view decode-cbor
26222622
are stored on disk as TextView files.
26232623

26242624
Usage: cardano-cli conway text-view decode-cbor --in-file FILEPATH
2625+
[ --output-cbor
2626+
| --output-text
2627+
]
26252628
[--out-file FILEPATH]
26262629

26272630
Print a TextView file as decoded CBOR.
@@ -4855,6 +4858,9 @@ Usage: cardano-cli latest text-view decode-cbor
48554858
are stored on disk as TextView files.
48564859

48574860
Usage: cardano-cli latest text-view decode-cbor --in-file FILEPATH
4861+
[ --output-cbor
4862+
| --output-text
4863+
]
48584864
[--out-file FILEPATH]
48594865

48604866
Print a TextView file as decoded CBOR.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
Usage: cardano-cli conway text-view decode-cbor --in-file FILEPATH
2+
[ --output-cbor
3+
| --output-text
4+
]
25
[--out-file FILEPATH]
36

47
Print a TextView file as decoded CBOR.
58

69
Available options:
710
--in-file FILEPATH CBOR input file.
11+
--output-cbor Format text view info output format to BASE16 CBOR.
12+
--output-text Format text view info output format to TEXT
13+
(default).
814
--out-file FILEPATH Optional output file. Default is to write to stdout.
915
-h,--help Show this help text
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
Usage: cardano-cli latest text-view decode-cbor --in-file FILEPATH
2+
[ --output-cbor
3+
| --output-text
4+
]
25
[--out-file FILEPATH]
36

47
Print a TextView file as decoded CBOR.
58

69
Available options:
710
--in-file FILEPATH CBOR input file.
11+
--output-cbor Format text view info output format to BASE16 CBOR.
12+
--output-text Format text view info output format to TEXT
13+
(default).
814
--out-file FILEPATH Optional output file. Default is to write to stdout.
915
-h,--help Show this help text

0 commit comments

Comments
 (0)