Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt YAML as human-readable text output #4433

Merged
merged 6 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
yaml "gopkg.in/yaml.v2"
alessio marked this conversation as resolved.
Show resolved Hide resolved

"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"
yaml "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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are removing this btw?

Copy link
Contributor Author

@alessio alessio May 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need an extra flag anymore to display a tree of multisig keys

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. this comes out of the box

- name: multi
  type: multi
  address: cosmos195eza9hyjx9w4amt3he37hqdcv66tjx8uwpapw
  pubkey: cosmospub1ytql0csgqyfzd666axrjzq3zqh4r75vtgxx2cndqpgy60yc9tfu9xxgwmqqnshgkdw79wnuv8ufzd666axrjzq39tr0mg9xyvelgmq4qxvm9ytvf2flduzwfmhd62fkvpjunfmzg0u8h900d
  mnemonic: ""
  threshold: 1
  pubkeys:
  - address: cosmos1dj4ghqevu9y788gkc6gafdpgx6umh8qplffye4
    pubkey: cosmospub1addwnpepqg3qt63l2x95rr9vfksq5zd8jvz457znry8dsqfct5txh0zhf7xr7yssng9
    weight: 1
  - address: cosmos104ytdpvrx9284zd50v9ep8c6j7pua7dkk0x3ek
    pubkey: cosmospub1addwnpepqgj43ha5znzxvl5ds2srxdjj9ky4ylk7p8yamka9ymxqewf5a3y870kwj76
    weight: 1

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"
yaml "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"
yaml "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
18 changes: 12 additions & 6 deletions version/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"

"github.com/tendermint/tendermint/libs/cli"
)
Expand All @@ -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
},
}