From a16cbe71f93384043ea1a6c9cad7fb2e5d6bc319 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 28 Apr 2022 15:56:33 +0200 Subject: [PATCH 1/3] feat: Improve CLI tx output --- client/context.go | 7 ++++++ client/context_test.go | 53 +++++++++++++++++++++++++++++++++++++----- client/tx/tx.go | 7 ++++-- 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/client/context.go b/client/context.go index 1d5925b933fa..be5c84af6070 100644 --- a/client/context.go +++ b/client/context.go @@ -2,6 +2,7 @@ package client import ( "bufio" + "encoding/json" "fmt" "io" "os" @@ -305,6 +306,12 @@ func (ctx Context) PrintObjectLegacy(toPrint interface{}) error { return ctx.printOutput(out) } +// PrintRaw is a variant of PrintProto that doesn't require a proto.Message type +// and uses a raw JSON message. No marshaling is performed. +func (ctx Context) PrintRaw(toPrint json.RawMessage) error { + return ctx.printOutput(toPrint) +} + func (ctx Context) printOutput(out []byte) error { var err error if ctx.OutputFormat == "text" { diff --git a/client/context_test.go b/client/context_test.go index 0efae1e63466..82feeebe4dfe 100644 --- a/client/context_test.go +++ b/client/context_test.go @@ -3,6 +3,7 @@ package client_test import ( "bytes" "context" + "encoding/json" "os" "strings" "testing" @@ -25,7 +26,7 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } -func TestContext_PrintObject(t *testing.T) { +func TestContext_PrintProto(t *testing.T) { ctx := client.Context{} animal := &testdata.Dog{ @@ -39,9 +40,7 @@ func TestContext_PrintObject(t *testing.T) { X: 10, } - // // proto - // registry := testdata.NewTestInterfaceRegistry() ctx = ctx.WithCodec(codec.NewProtoCodec(registry)) @@ -68,15 +67,28 @@ func TestContext_PrintObject(t *testing.T) { size: big x: "10" `, buf.String()) +} + +func TestContext_PrintObjectLegacy(t *testing.T) { + ctx := client.Context{} + + animal := &testdata.Dog{ + Size_: "big", + Name: "Spot", + } + any, err := types.NewAnyWithValue(animal) + require.NoError(t, err) + hasAnimal := &testdata.HasAnimal{ + Animal: any, + X: 10, + } - // // amino - // amino := testdata.NewTestAmino() ctx = ctx.WithLegacyAmino(&codec.LegacyAmino{Amino: amino}) // json - buf = &bytes.Buffer{} + buf := &bytes.Buffer{} ctx = ctx.WithOutput(buf) ctx.OutputFormat = "json" err = ctx.PrintObjectLegacy(hasAnimal) @@ -103,6 +115,35 @@ value: `, buf.String()) } +func TestContext_PrintRaw(t *testing.T) { + ctx := client.Context{} + hasAnimal := json.RawMessage(`{"animal":{"@type":"/testdata.Dog","size":"big","name":"Spot"},"x":"10"}`) + + // json + buf := &bytes.Buffer{} + ctx = ctx.WithOutput(buf) + ctx.OutputFormat = "json" + err := ctx.PrintRaw(hasAnimal) + require.NoError(t, err) + require.Equal(t, + `{"animal":{"@type":"/testdata.Dog","size":"big","name":"Spot"},"x":"10"} +`, buf.String()) + + // yaml + buf = &bytes.Buffer{} + ctx = ctx.WithOutput(buf) + ctx.OutputFormat = "text" + err = ctx.PrintRaw(hasAnimal) + require.NoError(t, err) + require.Equal(t, + `animal: + '@type': /testdata.Dog + name: Spot + size: big +x: "10" +`, buf.String()) +} + func TestCLIQueryConn(t *testing.T) { cfg := network.DefaultConfig() cfg.NumValidators = 1 diff --git a/client/tx/tx.go b/client/tx/tx.go index d9a82c49fc0c..4605d4991ce4 100644 --- a/client/tx/tx.go +++ b/client/tx/tx.go @@ -3,6 +3,7 @@ package tx import ( "bufio" "context" + "encoding/json" "errors" "fmt" "os" @@ -87,12 +88,14 @@ func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error { } if !clientCtx.SkipConfirm { - out, err := clientCtx.TxConfig.TxJSONEncoder()(tx.GetTx()) + txBytes, err := clientCtx.TxConfig.TxJSONEncoder()(tx.GetTx()) if err != nil { return err } - _, _ = fmt.Fprintf(os.Stderr, "%s\n\n", out) + if err := clientCtx.PrintRaw(json.RawMessage(txBytes)); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "%s\n", txBytes) + } buf := bufio.NewReader(os.Stdin) ok, err := input.GetConfirmation("confirm transaction before signing and broadcasting", buf, os.Stderr) From e869fe72bcfcf6d83d89f4d09b97dfb284c4aa23 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 28 Apr 2022 16:04:22 +0200 Subject: [PATCH 2/3] add changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2b6d7b003e0..27900f2e166b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -171,7 +171,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### CLI Breaking Changes -* [\#9695](https://github.com/cosmos/cosmos-sdk/pull/9695) ` keys migrate` CLI command now takes no arguments +* (cli) [\#11818](https://github.com/cosmos/cosmos-sdk/pull/11818) CLI transactions preview now respect the chosen `--output` flag format (json or text) +* [\#9695](https://github.com/cosmos/cosmos-sdk/pull/9695) ` keys migrate` CLI command now taks no arguments * [\#9246](https://github.com/cosmos/cosmos-sdk/pull/9246) Removed the CLI flag `--setup-config-only` from the `testnet` command and added the subcommand `init-files`. * [\#9780](https://github.com/cosmos/cosmos-sdk/pull/9780) Use sigs.k8s.io for yaml, which might lead to minor YAML output changes * [\#10625](https://github.com/cosmos/cosmos-sdk/pull/10625) Rename `--fee-account` CLI flag to `--fee-granter` From f0dc191731bd499a61eb3b04206aa976d6b9ce5f Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 28 Apr 2022 16:16:51 +0200 Subject: [PATCH 3/3] Update CHANGELOG.md Co-authored-by: Aleksandr Bezobchuk --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27900f2e166b..0da7c62b0b1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -171,8 +171,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### CLI Breaking Changes -* (cli) [\#11818](https://github.com/cosmos/cosmos-sdk/pull/11818) CLI transactions preview now respect the chosen `--output` flag format (json or text) -* [\#9695](https://github.com/cosmos/cosmos-sdk/pull/9695) ` keys migrate` CLI command now taks no arguments +* (cli) [\#11818](https://github.com/cosmos/cosmos-sdk/pull/11818) CLI transactions preview now respect the chosen `--output` flag format (json or text). +* [\#9695](https://github.com/cosmos/cosmos-sdk/pull/9695) ` keys migrate` CLI command now takes no arguments. * [\#9246](https://github.com/cosmos/cosmos-sdk/pull/9246) Removed the CLI flag `--setup-config-only` from the `testnet` command and added the subcommand `init-files`. * [\#9780](https://github.com/cosmos/cosmos-sdk/pull/9780) Use sigs.k8s.io for yaml, which might lead to minor YAML output changes * [\#10625](https://github.com/cosmos/cosmos-sdk/pull/10625) Rename `--fee-account` CLI flag to `--fee-granter`