Merge PR #2304: client/keys: remove excess code and uniform code style

This commit is contained in:
Jia Chenhui 2018-09-12 15:14:29 +08:00 committed by Christopher Goes
parent 358b48771c
commit 55b7c6adf1
1 changed files with 40 additions and 46 deletions

View File

@ -5,9 +5,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -19,7 +17,7 @@ const (
FlagAddress = "address" FlagAddress = "address"
// FlagPublicKey represents the user's public key on the command line. // FlagPublicKey represents the user's public key on the command line.
FlagPublicKey = "pubkey" FlagPublicKey = "pubkey"
// FlagBechPrefix defines a desired Bech32 prefix encoding for a key // FlagBechPrefix defines a desired Bech32 prefix encoding for a key.
FlagBechPrefix = "bech" FlagBechPrefix = "bech"
) )
@ -29,39 +27,7 @@ func showKeysCmd() *cobra.Command {
Short: "Show key info for the given name", Short: "Show key info for the given name",
Long: `Return public details of one local key.`, Long: `Return public details of one local key.`,
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: runShowCmd,
name := args[0]
info, err := getKey(name)
if err != nil {
return err
}
showAddress := viper.GetBool(FlagAddress)
showPublicKey := viper.GetBool(FlagPublicKey)
outputSet := cmd.Flag(cli.OutputFlag).Changed
if showAddress && showPublicKey {
return errors.New("cannot use both --address and --pubkey at once")
}
if outputSet && (showAddress || showPublicKey) {
return errors.New("cannot use --output with --address or --pubkey")
}
bechKeyOut, err := getBechKeyOut(viper.GetString(FlagBechPrefix))
if err != nil {
return err
}
switch {
case showAddress:
printKeyAddress(info, bechKeyOut)
case showPublicKey:
printPubKey(info, bechKeyOut)
default:
printKeyInfo(info, bechKeyOut)
}
return nil
},
} }
cmd.Flags().String(FlagBechPrefix, "acc", "The Bech32 prefix encoding for a key (acc|val|cons)") cmd.Flags().String(FlagBechPrefix, "acc", "The Bech32 prefix encoding for a key (acc|val|cons)")
@ -71,6 +37,43 @@ func showKeysCmd() *cobra.Command {
return cmd return cmd
} }
func runShowCmd(cmd *cobra.Command, args []string) error {
name := args[0]
info, err := GetKeyInfo(name)
if err != nil {
return err
}
isShowAddr := viper.GetBool(FlagAddress)
isShowPubKey := viper.GetBool(FlagPublicKey)
isOutputSet := cmd.Flag(cli.OutputFlag).Changed
if isShowAddr && isShowPubKey {
return errors.New("cannot use both --address and --pubkey at once")
}
if isOutputSet && (isShowAddr || isShowPubKey) {
return errors.New("cannot use --output with --address or --pubkey")
}
bechKeyOut, err := getBechKeyOut(viper.GetString(FlagBechPrefix))
if err != nil {
return err
}
switch {
case isShowAddr:
printKeyAddress(info, bechKeyOut)
case isShowPubKey:
printPubKey(info, bechKeyOut)
default:
printKeyInfo(info, bechKeyOut)
}
return nil
}
func getBechKeyOut(bechPrefix string) (bechKeyOutFn, error) { func getBechKeyOut(bechPrefix string) (bechKeyOutFn, error) {
switch bechPrefix { switch bechPrefix {
case "acc": case "acc":
@ -84,15 +87,6 @@ func getBechKeyOut(bechPrefix string) (bechKeyOutFn, error) {
return nil, fmt.Errorf("invalid Bech32 prefix encoding provided: %s", bechPrefix) return nil, fmt.Errorf("invalid Bech32 prefix encoding provided: %s", bechPrefix)
} }
func getKey(name string) (keys.Info, error) {
kb, err := GetKeyBase()
if err != nil {
return nil, err
}
return kb.Get(name)
}
/////////////////////////// ///////////////////////////
// REST // REST
@ -113,7 +107,7 @@ func GetKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
info, err := getKey(name) info, err := GetKeyInfo(name)
// TODO: check for the error if key actually does not exist, instead of // TODO: check for the error if key actually does not exist, instead of
// assuming this as the reason // assuming this as the reason
if err != nil { if err != nil {