Skip to content

Commit

Permalink
Merge PR cosmos#4433: Adopt YAML as human-readable text output
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio authored and alexanderbez committed May 31, 2019
1 parent 9969ef9 commit e9810ac
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 56 deletions.
3 changes: 2 additions & 1 deletion client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 11 additions & 7 deletions client/keys/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
5 changes: 0 additions & 5 deletions client/keys/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const (
FlagDevice = "device"

flagMultiSigThreshold = "multisig-threshold"
flagShowMultiSig = "show-multisig"

defaultMultiSigKeyName = "multi"
)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
30 changes: 6 additions & 24 deletions client/keys/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 16 additions & 10 deletions server/tm_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down Expand Up @@ -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
},
}
Expand Down
74 changes: 74 additions & 0 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"strings"

"gopkg.in/yaml.v2"

"github.com/tendermint/tendermint/crypto"
cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"

Expand Down Expand Up @@ -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
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
23 changes: 21 additions & 2 deletions types/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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

Expand Down
Loading

0 comments on commit e9810ac

Please sign in to comment.