51 lines
1019 B
Go
51 lines
1019 B
Go
package keys
|
|
|
|
import (
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
)
|
|
|
|
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
|
|
along with their associated name and address.`,
|
|
RunE: runListCmd,
|
|
}
|
|
|
|
cmd.Flags().BoolP(flagListNames, "n", false, "List names only")
|
|
return cmd
|
|
}
|
|
|
|
func runListCmd(cmd *cobra.Command, _ []string) error {
|
|
clientCtx, err := client.GetClientQueryContext(cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
records, err := clientCtx.Keyring.List()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(records) == 0 {
|
|
cmd.Println("No records were found in keyring")
|
|
return nil
|
|
}
|
|
|
|
if ok, _ := cmd.Flags().GetBool(flagListNames); !ok {
|
|
return printKeyringRecords(cmd.OutOrStdout(), records, clientCtx.OutputFormat)
|
|
}
|
|
|
|
for _, k := range records {
|
|
cmd.Println(k.Name)
|
|
}
|
|
|
|
return nil
|
|
}
|