github.com/cosmos/cosmos-sdk@v0.50.10/client/keys/list.go (about)

     1  package keys
     2  
     3  import (
     4  	"github.com/spf13/cobra"
     5  
     6  	"github.com/cosmos/cosmos-sdk/client"
     7  	"github.com/cosmos/cosmos-sdk/client/flags"
     8  )
     9  
    10  const flagListNames = "list-names"
    11  
    12  // ListKeysCmd lists all keys in the key store.
    13  func ListKeysCmd() *cobra.Command {
    14  	cmd := &cobra.Command{
    15  		Use:   "list",
    16  		Short: "List all keys",
    17  		Long: `Return a list of all public keys stored by this key manager
    18  along with their associated name and address.`,
    19  		RunE: runListCmd,
    20  	}
    21  
    22  	cmd.Flags().BoolP(flagListNames, "n", false, "List names only")
    23  	return cmd
    24  }
    25  
    26  func runListCmd(cmd *cobra.Command, _ []string) error {
    27  	clientCtx, err := client.GetClientQueryContext(cmd)
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	records, err := clientCtx.Keyring.List()
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	if len(records) == 0 && clientCtx.OutputFormat == flags.OutputFormatText {
    38  		cmd.Println("No records were found in keyring")
    39  		return nil
    40  	}
    41  
    42  	if ok, _ := cmd.Flags().GetBool(flagListNames); !ok {
    43  		return printKeyringRecords(cmd.OutOrStdout(), records, clientCtx.OutputFormat)
    44  	}
    45  
    46  	for _, k := range records {
    47  		cmd.Println(k.Name)
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  // ListKeyTypesCmd lists all key types.
    54  func ListKeyTypesCmd() *cobra.Command {
    55  	return &cobra.Command{
    56  		Use:   "list-key-types",
    57  		Short: "List all key types",
    58  		Long:  `Return a list of all supported key types (also known as algos)`,
    59  		RunE: func(cmd *cobra.Command, args []string) error {
    60  			clientCtx, err := client.GetClientQueryContext(cmd)
    61  			if err != nil {
    62  				return err
    63  			}
    64  
    65  			cmd.Println("Supported key types/algos:")
    66  			keyring, _ := clientCtx.Keyring.SupportedAlgorithms()
    67  			cmd.Printf("%+q\n", keyring)
    68  			return nil
    69  		},
    70  	}
    71  }