cosmos-sdk/client/keys/list.go

51 lines
1.2 KiB
Go
Raw Normal View History

2018-02-22 07:17:19 -08:00
package keys
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const flagListNames = "list-names"
// ListKeysCmd lists all keys in the key store.
func ListKeysCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List all keys",
Long: `Return a list of all public keys stored by this key manager
2018-02-22 07:17:19 -08:00
along with their associated name and address.`,
RunE: runListCmd,
}
cmd.Flags().Bool(flags.FlagIndentResponse, false, "Add indent to JSON response")
cmd.Flags().BoolP(flagListNames, "n", false, "List names only")
return cmd
2018-02-22 07:17:19 -08:00
}
func runListCmd(cmd *cobra.Command, _ []string) error {
kb, err := keyring.New(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), cmd.InOrStdin())
2018-02-22 07:17:19 -08:00
if err != nil {
return err
}
infos, err := kb.List()
if err != nil {
return err
}
cmd.SetOut(cmd.OutOrStdout())
if !viper.GetBool(flagListNames) {
printInfos(cmd.OutOrStdout(), infos)
return nil
}
for _, info := range infos {
cmd.Println(info.GetName())
2018-02-22 07:17:19 -08:00
}
return nil
2018-02-22 07:17:19 -08:00
}