Merge PR #4433: Adopt YAML as human-readable text output

This commit is contained in:
Alessio Treglia
2019-05-31 09:14:34 -04:00
committed by Alexander Bezobchuk
parent 9969ef9b19
commit e9810ac25c
9 changed files with 143 additions and 56 deletions
+2 -1
View File
@@ -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 {
+11 -7
View File
@@ -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))
}
-5
View File
@@ -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)
}
+6 -24
View File
@@ -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) {