Merge PR #2304: client/keys: remove excess code and uniform code style
This commit is contained in:
parent
358b48771c
commit
55b7c6adf1
|
@ -5,9 +5,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
@ -19,7 +17,7 @@ const (
|
|||
FlagAddress = "address"
|
||||
// FlagPublicKey represents the user's public key on the command line.
|
||||
FlagPublicKey = "pubkey"
|
||||
// FlagBechPrefix defines a desired Bech32 prefix encoding for a key
|
||||
// FlagBechPrefix defines a desired Bech32 prefix encoding for a key.
|
||||
FlagBechPrefix = "bech"
|
||||
)
|
||||
|
||||
|
@ -29,39 +27,7 @@ func showKeysCmd() *cobra.Command {
|
|||
Short: "Show key info for the given name",
|
||||
Long: `Return public details of one local key.`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
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
|
||||
},
|
||||
RunE: runShowCmd,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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) {
|
||||
switch bechPrefix {
|
||||
case "acc":
|
||||
|
@ -84,15 +87,6 @@ func getBechKeyOut(bechPrefix string) (bechKeyOutFn, error) {
|
|||
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
|
||||
|
||||
|
@ -113,7 +107,7 @@ func GetKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
info, err := getKey(name)
|
||||
info, err := GetKeyInfo(name)
|
||||
// TODO: check for the error if key actually does not exist, instead of
|
||||
// assuming this as the reason
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue