Skip to content

Commit

Permalink
Merge PR cosmos#4355: Improve multisig key table format output
Browse files Browse the repository at this point in the history
  • Loading branch information
zxh0 authored and alexanderbez committed May 17, 2019
1 parent c0d8fb8 commit db4c82b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
41 changes: 18 additions & 23 deletions client/keys/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package keys

import (
"fmt"
"os"
"path/filepath"

"github.com/olekukonko/tablewriter"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"

Expand Down Expand Up @@ -89,22 +91,20 @@ func getLazyKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
return keys.New(defaultKeyDBName, filepath.Join(rootDir, "keys")), nil
}

func printKeyTextHeader() {
fmt.Printf("NAME:\tTYPE:\tADDRESS:\t\t\t\t\tPUBKEY:\n")
}

func printMultiSigKeyTextHeader() {
fmt.Printf("WEIGHT:\tTHRESHOLD:\tADDRESS:\t\t\t\t\tPUBKEY:\n")
}

func printMultiSigKeyInfo(keyInfo keys.Info, bechKeyOut bechKeyOutFn) {
ko, err := bechKeyOut(keyInfo)
if err != nil {
panic(err)
}

printMultiSigKeyTextHeader()
printMultiSigKeyOutput(ko)
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) {
Expand All @@ -115,8 +115,7 @@ func printKeyInfo(keyInfo keys.Info, bechKeyOut bechKeyOutFn) {

switch viper.Get(cli.OutputFlag) {
case OutputFormatText:
printKeyTextHeader()
printKeyOutput(ko)
printTextInfos([]keys.KeyOutput{ko})

case OutputFormatJSON:
var out []byte
Expand All @@ -142,10 +141,7 @@ func printInfos(infos []keys.Info) {

switch viper.Get(cli.OutputFlag) {
case OutputFormatText:
printKeyTextHeader()
for _, ko := range kos {
printKeyOutput(ko)
}
printTextInfos(kos)

case OutputFormatJSON:
var out []byte
Expand All @@ -164,14 +160,13 @@ func printInfos(infos []keys.Info) {
}
}

func printKeyOutput(ko keys.KeyOutput) {
fmt.Printf("%s\t%s\t%s\t%s\n", ko.Name, ko.Type, ko.Address, ko.PubKey)
}

func printMultiSigKeyOutput(ko keys.KeyOutput) {
for _, pk := range ko.PubKeys {
fmt.Printf("%d\t%d\t\t%s\t%s\n", pk.Weight, ko.Threshold, pk.Address, pk.PubKey)
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})
}
table.Render()
}

func printKeyAddress(info keys.Info, bechKeyOut bechKeyOutFn) {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ require (
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/magiconair/properties v1.8.0 // indirect
github.com/mattn/go-isatty v0.0.6
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/olekukonko/tablewriter v0.0.1
github.com/otiai10/copy v0.0.0-20180813032824-7e9a647135a1
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95 // indirect
github.com/otiai10/mint v1.2.3 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDe
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-isatty v0.0.6 h1:SrwhHcpV4nWrMGdNcC2kXpMfcBVYGDuTArqyhocJgvA=
github.com/mattn/go-isatty v0.0.6/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88=
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down

0 comments on commit db4c82b

Please sign in to comment.