2018-02-22 07:17:19 -08:00
|
|
|
package keys
|
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
import (
|
2019-06-08 03:04:52 -07:00
|
|
|
"github.com/spf13/cobra"
|
2019-12-05 07:07:29 -08:00
|
|
|
"github.com/spf13/viper"
|
2019-07-22 08:26:42 -07:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-03-25 08:20:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2020-01-22 09:54:56 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-02-28 23:16:54 -08:00
|
|
|
)
|
|
|
|
|
2019-12-05 07:07:29 -08:00
|
|
|
const flagListNames = "list-names"
|
|
|
|
|
2019-12-12 13:52:24 -08:00
|
|
|
// ListKeysCmd lists all keys in the key store.
|
|
|
|
func ListKeysCmd() *cobra.Command {
|
2019-03-13 10:36:52 -07:00
|
|
|
cmd := &cobra.Command{
|
2019-02-08 12:45:23 -08:00
|
|
|
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.`,
|
2019-02-08 12:45:23 -08:00
|
|
|
RunE: runListCmd,
|
|
|
|
}
|
2019-05-28 01:44:04 -07:00
|
|
|
cmd.Flags().Bool(flags.FlagIndentResponse, false, "Add indent to JSON response")
|
2019-12-05 07:07:29 -08:00
|
|
|
cmd.Flags().BoolP(flagListNames, "n", false, "List names only")
|
2019-03-13 10:36:52 -07:00
|
|
|
return cmd
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
|
|
|
|
2019-12-05 07:07:29 -08:00
|
|
|
func runListCmd(cmd *cobra.Command, _ []string) error {
|
2020-04-08 02:38:28 -07:00
|
|
|
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()
|
2019-12-05 07:07:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-30 07:54:01 -07:00
|
|
|
cmd.SetOut(cmd.OutOrStdout())
|
2019-12-05 07:07:29 -08:00
|
|
|
if !viper.GetBool(flagListNames) {
|
2020-03-30 07:54:01 -07:00
|
|
|
printInfos(cmd.OutOrStdout(), infos)
|
2019-12-05 07:07:29 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range infos {
|
|
|
|
cmd.Println(info.GetName())
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
2019-12-05 07:07:29 -08:00
|
|
|
|
|
|
|
return nil
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|