From 61418709e67cdcafe82fd4d9fd6b9882279c296b Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Thu, 5 Apr 2018 17:10:37 +0100 Subject: [PATCH] client/keys: print out pubkeys in JSON format Add flag to the show command to print out public keys in JSON format. See tendermint/go-crypto#79 for more information. --- client/keys/show.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/client/keys/show.go b/client/keys/show.go index bb60b5bc0bbe..350b3d37f8dd 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -2,13 +2,17 @@ package keys import ( "encoding/json" + "fmt" "net/http" "github.com/gorilla/mux" - "github.com/pkg/errors" + "github.com/spf13/cobra" + "github.com/spf13/viper" keys "github.com/tendermint/go-crypto/keys" +) - "github.com/spf13/cobra" +const ( + flagExportPubKey = "export-pubkey" ) var showKeysCmd = &cobra.Command{ @@ -16,6 +20,11 @@ var showKeysCmd = &cobra.Command{ Short: "Show key info for the given name", Long: `Return public details of one local key.`, RunE: runShowCmd, + Args: cobra.ExactArgs(1), +} + +func init() { + showKeysCmd.Flags().Bool(flagExportPubKey, false, "Export public key.") } func getKey(name string) (keys.Info, error) { @@ -30,16 +39,20 @@ func getKey(name string) (keys.Info, error) { // CMD func runShowCmd(cmd *cobra.Command, args []string) error { - if len(args) != 1 || len(args[0]) == 0 { - return errors.New("You must provide a name for the key") + info, err := getKey(args[0]) + if err != nil { + return err } - name := args[0] - - info, err := getKey(name) - if err == nil { - printInfo(info) + if viper.GetBool(flagExportPubKey) { + out, err := info.PubKey.MarshalJSON() + if err != nil { + return err + } + fmt.Println(string(out)) + return nil } - return err + printInfo(info) + return nil } // REST