From e9810ac25c219cb9ac82a5591ce35c0a2a82a3cc Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Fri, 31 May 2019 14:14:35 +0100 Subject: [PATCH] Merge PR #4433: Adopt YAML as human-readable text output --- client/context/context.go | 3 +- client/keys/parse.go | 18 ++++++---- client/keys/show.go | 5 --- client/keys/utils.go | 30 ++++------------ go.mod | 2 +- server/tm_cmds.go | 26 ++++++++------ types/address.go | 74 +++++++++++++++++++++++++++++++++++++++ types/address_test.go | 23 ++++++++++-- version/command.go | 18 ++++++---- 9 files changed, 143 insertions(+), 56 deletions(-) diff --git a/client/context/context.go b/client/context/context.go index ed027b13b78f..0b633fb20a9b 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -16,6 +16,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" "github.com/spf13/viper" + "gopkg.in/yaml.v2" "github.com/tendermint/tendermint/libs/cli" "github.com/tendermint/tendermint/libs/log" @@ -272,7 +273,7 @@ func (ctx CLIContext) PrintOutput(toPrint fmt.Stringer) (err error) { switch ctx.OutputFormat { case "text": - out = []byte(toPrint.String()) + out, err = yaml.Marshal(&toPrint) case "json": if ctx.Indent { diff --git a/client/keys/parse.go b/client/keys/parse.go index 68ee77a8d607..be7c57b0ea49 100644 --- a/client/keys/parse.go +++ b/client/keys/parse.go @@ -8,6 +8,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "gopkg.in/yaml.v2" "github.com/tendermint/tendermint/libs/bech32" "github.com/tendermint/tendermint/libs/cli" @@ -112,23 +113,26 @@ func runFromHex(hexstr string) bool { } func displayParseKeyInfo(stringer fmt.Stringer) { + var out []byte + var err error + switch viper.Get(cli.OutputFlag) { case OutputFormatText: - fmt.Printf("%s\n", stringer) + out, err = yaml.Marshal(&stringer) case OutputFormatJSON: - var out []byte - var err error if viper.GetBool(flags.FlagIndentResponse) { out, err = cdc.MarshalJSONIndent(stringer, "", " ") - if err != nil { - panic(err) - } } else { out = cdc.MustMarshalJSON(stringer) } - fmt.Println(string(out)) } + + if err != nil { + panic(err) + } + + fmt.Println(string(out)) } diff --git a/client/keys/show.go b/client/keys/show.go index 3086726fd327..cf5043c2abdc 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -28,7 +28,6 @@ const ( FlagDevice = "device" flagMultiSigThreshold = "multisig-threshold" - flagShowMultiSig = "show-multisig" defaultMultiSigKeyName = "multi" ) @@ -49,7 +48,6 @@ consisting of all the keys provided by name and multisig threshold.`, cmd.Flags().BoolP(FlagPublicKey, "p", false, "Output the public key only (overrides --output)") cmd.Flags().BoolP(FlagDevice, "d", false, "Output the address in a ledger device") cmd.Flags().Uint(flagMultiSigThreshold, 1, "K out of N required signatures") - cmd.Flags().BoolP(flagShowMultiSig, "m", false, "Output multisig pubkey constituents, threshold, and weights") cmd.Flags().Bool(flags.FlagIndentResponse, false, "Add indent to JSON response") return cmd @@ -87,7 +85,6 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) { isShowAddr := viper.GetBool(FlagAddress) isShowPubKey := viper.GetBool(FlagPublicKey) isShowDevice := viper.GetBool(FlagDevice) - isShowMultiSig := viper.GetBool(flagShowMultiSig) isOutputSet := false tmp := cmd.Flag(cli.OutputFlag) @@ -113,8 +110,6 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) { printKeyAddress(info, bechKeyOut) case isShowPubKey: printPubKey(info, bechKeyOut) - case isShowMultiSig: - printMultiSigKeyInfo(info, bechKeyOut) default: printKeyInfo(info, bechKeyOut) } diff --git a/client/keys/utils.go b/client/keys/utils.go index 69f72a6701a0..e7442dec9bd9 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -2,12 +2,11 @@ package keys import ( "fmt" - "os" "path/filepath" - "github.com/olekukonko/tablewriter" "github.com/spf13/viper" "github.com/tendermint/tendermint/libs/cli" + "gopkg.in/yaml.v2" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/input" @@ -92,22 +91,6 @@ func getLazyKeyBaseFromDir(rootDir string) (keys.Keybase, error) { return keys.New(defaultKeyDBName, filepath.Join(rootDir, "keys")), nil } -func printMultiSigKeyInfo(keyInfo keys.Info, bechKeyOut bechKeyOutFn) { - ko, err := bechKeyOut(keyInfo) - if err != nil { - panic(err) - } - - table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"WEIGHT", "THRESHOLD", "ADDRESS", "PUBKEY"}) - threshold := fmt.Sprintf("%d", ko.Threshold) - for _, pk := range ko.PubKeys { - weight := fmt.Sprintf("%d", pk.Weight) - table.Append([]string{weight, threshold, pk.Address, pk.PubKey}) - } - table.Render() -} - func printKeyInfo(keyInfo keys.Info, bechKeyOut bechKeyOutFn) { ko, err := bechKeyOut(keyInfo) if err != nil { @@ -157,17 +140,16 @@ func printInfos(infos []keys.Info) { if err != nil { panic(err) } - fmt.Println(string(out)) + fmt.Printf("%s", out) } } func printTextInfos(kos []keys.KeyOutput) { - table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"NAME", "TYPE", "ADDRESS", "PUBKEY"}) - for _, ko := range kos { - table.Append([]string{ko.Name, ko.Type, ko.Address, ko.PubKey}) + out, err := yaml.Marshal(&kos) + if err != nil { + panic(err) } - table.Render() + fmt.Println(string(out)) } func printKeyAddress(info keys.Info, bechKeyOut bechKeyOutFn) { diff --git a/go.mod b/go.mod index fb811b9e82ac..69411b1a95f7 100644 --- a/go.mod +++ b/go.mod @@ -45,7 +45,7 @@ require ( github.com/tendermint/tendermint v0.31.5 golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 google.golang.org/grpc v1.19.0 // indirect - gopkg.in/yaml.v2 v2.2.2 // indirect + gopkg.in/yaml.v2 v2.2.2 ) replace golang.org/x/crypto => github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5 diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 92d51d69e16b..7b3bd181a543 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -7,6 +7,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "gopkg.in/yaml.v2" tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" "github.com/tendermint/tendermint/libs/cli" @@ -18,14 +19,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -const ( - versionString = `Tendermint: %s -ABCI: %s -BlockProtocol: %d -P2PProtocol: %d -` -) - // ShowNodeIDCmd - ported from Tendermint, dump node ID to stdout func ShowNodeIDCmd(ctx *Context) *cobra.Command { return &cobra.Command{ @@ -110,9 +103,22 @@ against which this app has been compiled. `, RunE: func(cmd *cobra.Command, args []string) error { - fmt.Printf(versionString, tversion.Version, tversion.ABCIVersion, - tversion.BlockProtocol.Uint64(), tversion.P2PProtocol.Uint64()) + bs, err := yaml.Marshal(&struct { + Tendermint string + ABCI string + BlockProtocol uint64 + P2PProtocol uint64 + }{ + Tendermint: tversion.Version, + ABCI: tversion.ABCIVersion, + BlockProtocol: tversion.BlockProtocol.Uint64(), + P2PProtocol: tversion.P2PProtocol.Uint64(), + }) + if err != nil { + return err + } + fmt.Println(string(bs)) return nil }, } diff --git a/types/address.go b/types/address.go index 7fa6074332ec..78accb7fa96f 100644 --- a/types/address.go +++ b/types/address.go @@ -8,6 +8,8 @@ import ( "fmt" "strings" + "gopkg.in/yaml.v2" + "github.com/tendermint/tendermint/crypto" cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino" @@ -71,6 +73,10 @@ var _ Address = AccAddress{} var _ Address = ValAddress{} var _ Address = ConsAddress{} +var _ yaml.Marshaler = AccAddress{} +var _ yaml.Marshaler = ValAddress{} +var _ yaml.Marshaler = ConsAddress{} + // ---------------------------------------------------------------------------- // account // ---------------------------------------------------------------------------- @@ -165,6 +171,11 @@ func (aa AccAddress) MarshalJSON() ([]byte, error) { return json.Marshal(aa.String()) } +// MarshalYAML marshals to YAML using Bech32. +func (aa AccAddress) MarshalYAML() (interface{}, error) { + return aa.String(), nil +} + // UnmarshalJSON unmarshals from JSON assuming Bech32 encoding. func (aa *AccAddress) UnmarshalJSON(data []byte) error { var s string @@ -182,6 +193,23 @@ func (aa *AccAddress) UnmarshalJSON(data []byte) error { return nil } +// UnmarshalYAML unmarshals from JSON assuming Bech32 encoding. +func (aa *AccAddress) UnmarshalYAML(data []byte) error { + var s string + err := yaml.Unmarshal(data, &s) + if err != nil { + return err + } + + aa2, err := AccAddressFromBech32(s) + if err != nil { + return err + } + + *aa = aa2 + return nil +} + // Bytes returns the raw address bytes. func (aa AccAddress) Bytes() []byte { return aa @@ -296,6 +324,11 @@ func (va ValAddress) MarshalJSON() ([]byte, error) { return json.Marshal(va.String()) } +// MarshalYAML marshals to YAML using Bech32. +func (va ValAddress) MarshalYAML() (interface{}, error) { + return va.String(), nil +} + // UnmarshalJSON unmarshals from JSON assuming Bech32 encoding. func (va *ValAddress) UnmarshalJSON(data []byte) error { var s string @@ -314,6 +347,24 @@ func (va *ValAddress) UnmarshalJSON(data []byte) error { return nil } +// UnmarshalYAML unmarshals from YAML assuming Bech32 encoding. +func (va *ValAddress) UnmarshalYAML(data []byte) error { + var s string + + err := yaml.Unmarshal(data, &s) + if err != nil { + return err + } + + va2, err := ValAddressFromBech32(s) + if err != nil { + return err + } + + *va = va2 + return nil +} + // Bytes returns the raw address bytes. func (va ValAddress) Bytes() []byte { return va @@ -433,6 +484,11 @@ func (ca ConsAddress) MarshalJSON() ([]byte, error) { return json.Marshal(ca.String()) } +// MarshalYAML marshals to YAML using Bech32. +func (ca ConsAddress) MarshalYAML() (interface{}, error) { + return ca.String(), nil +} + // UnmarshalJSON unmarshals from JSON assuming Bech32 encoding. func (ca *ConsAddress) UnmarshalJSON(data []byte) error { var s string @@ -451,6 +507,24 @@ func (ca *ConsAddress) UnmarshalJSON(data []byte) error { return nil } +// UnmarshalYAML unmarshals from YAML assuming Bech32 encoding. +func (ca *ConsAddress) UnmarshalYAML(data []byte) error { + var s string + + err := yaml.Unmarshal(data, &s) + if err != nil { + return err + } + + ca2, err := ConsAddressFromBech32(s) + if err != nil { + return err + } + + *ca = ca2 + return nil +} + // Bytes returns the raw address bytes. func (ca ConsAddress) Bytes() []byte { return ca diff --git a/types/address_test.go b/types/address_test.go index 406ff1b7e73d..c7541ace87a8 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -4,12 +4,14 @@ import ( "encoding/hex" "fmt" "math/rand" - "testing" - "strings" + "testing" "github.com/stretchr/testify/require" + "gopkg.in/yaml.v2" + "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/crypto/secp256k1" "github.com/cosmos/cosmos-sdk/types" ) @@ -93,6 +95,23 @@ func TestRandBech32PubkeyConsistency(t *testing.T) { } } +func TestYAMLMarshalers(t *testing.T) { + addr := secp256k1.GenPrivKey().PubKey().Address() + + acc := types.AccAddress(addr) + val := types.ValAddress(addr) + cons := types.ConsAddress(addr) + + got, _ := yaml.Marshal(&acc) + require.Equal(t, acc.String()+"\n", string(got)) + + got, _ = yaml.Marshal(&val) + require.Equal(t, val.String()+"\n", string(got)) + + got, _ = yaml.Marshal(&cons) + require.Equal(t, cons.String()+"\n", string(got)) +} + func TestRandBech32AccAddrConsistency(t *testing.T) { var pub ed25519.PubKeyEd25519 diff --git a/version/command.go b/version/command.go index 703dda192252..e4883f6f2ab9 100644 --- a/version/command.go +++ b/version/command.go @@ -6,6 +6,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "gopkg.in/yaml.v2" "github.com/tendermint/tendermint/libs/cli" ) @@ -28,16 +29,21 @@ var Cmd = &cobra.Command{ return nil } - if viper.GetString(cli.OutputFlag) != "json" { - fmt.Println(verInfo) - return nil + var bz []byte + var err error + + switch viper.GetString(cli.OutputFlag) { + case "json": + bz, err = json.Marshal(verInfo) + default: + bz, err = yaml.Marshal(&verInfo) } - bz, err := json.Marshal(verInfo) if err != nil { return err } - fmt.Println(string(bz)) - return nil + + _, err = fmt.Println(string(bz)) + return err }, }