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"
|
"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,21 +27,33 @@ 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,
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Flags().String(FlagBechPrefix, "acc", "The Bech32 prefix encoding for a key (acc|val|cons)")
|
||||||
|
cmd.Flags().Bool(FlagAddress, false, "output the address only (overrides --output)")
|
||||||
|
cmd.Flags().Bool(FlagPublicKey, false, "output the public key only (overrides --output)")
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func runShowCmd(cmd *cobra.Command, args []string) error {
|
||||||
name := args[0]
|
name := args[0]
|
||||||
info, err := getKey(name)
|
|
||||||
|
info, err := GetKeyInfo(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
showAddress := viper.GetBool(FlagAddress)
|
isShowAddr := viper.GetBool(FlagAddress)
|
||||||
showPublicKey := viper.GetBool(FlagPublicKey)
|
isShowPubKey := viper.GetBool(FlagPublicKey)
|
||||||
outputSet := cmd.Flag(cli.OutputFlag).Changed
|
isOutputSet := cmd.Flag(cli.OutputFlag).Changed
|
||||||
|
|
||||||
if showAddress && showPublicKey {
|
if isShowAddr && isShowPubKey {
|
||||||
return errors.New("cannot use both --address and --pubkey at once")
|
return errors.New("cannot use both --address and --pubkey at once")
|
||||||
}
|
}
|
||||||
if outputSet && (showAddress || showPublicKey) {
|
|
||||||
|
if isOutputSet && (isShowAddr || isShowPubKey) {
|
||||||
return errors.New("cannot use --output with --address or --pubkey")
|
return errors.New("cannot use --output with --address or --pubkey")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,22 +63,15 @@ func showKeysCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case showAddress:
|
case isShowAddr:
|
||||||
printKeyAddress(info, bechKeyOut)
|
printKeyAddress(info, bechKeyOut)
|
||||||
case showPublicKey:
|
case isShowPubKey:
|
||||||
printPubKey(info, bechKeyOut)
|
printPubKey(info, bechKeyOut)
|
||||||
default:
|
default:
|
||||||
printKeyInfo(info, bechKeyOut)
|
printKeyInfo(info, bechKeyOut)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd.Flags().String(FlagBechPrefix, "acc", "The Bech32 prefix encoding for a key (acc|val|cons)")
|
|
||||||
cmd.Flags().Bool(FlagAddress, false, "output the address only (overrides --output)")
|
|
||||||
cmd.Flags().Bool(FlagPublicKey, false, "output the public key only (overrides --output)")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBechKeyOut(bechPrefix string) (bechKeyOutFn, error) {
|
func getBechKeyOut(bechPrefix string) (bechKeyOutFn, error) {
|
||||||
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue