github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/crypto/keys/client/list.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  
     7  	"github.com/gnolang/gno/tm2/pkg/commands"
     8  	"github.com/gnolang/gno/tm2/pkg/crypto/keys"
     9  )
    10  
    11  func NewListCmd(rootCfg *BaseCfg, io commands.IO) *commands.Command {
    12  	return commands.NewCommand(
    13  		commands.Metadata{
    14  			Name:       "list",
    15  			ShortUsage: "list",
    16  			ShortHelp:  "lists all keys in the keybase",
    17  		},
    18  		nil,
    19  		func(_ context.Context, args []string) error {
    20  			return execList(rootCfg, args, io)
    21  		},
    22  	)
    23  }
    24  
    25  func execList(cfg *BaseCfg, args []string, io commands.IO) error {
    26  	if len(args) != 0 {
    27  		return flag.ErrHelp
    28  	}
    29  
    30  	kb, err := keys.NewKeyBaseFromDir(cfg.Home)
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	infos, err := kb.List()
    36  	if err == nil {
    37  		printInfos(infos, io)
    38  	}
    39  
    40  	return err
    41  }
    42  
    43  func printInfos(infos []keys.Info, io commands.IO) {
    44  	for i, info := range infos {
    45  		keyname := info.GetName()
    46  		keytype := info.GetType()
    47  		keypub := info.GetPubKey()
    48  		keyaddr := info.GetAddress()
    49  		keypath, _ := info.GetPath()
    50  		io.Printfln("%d. %s (%s) - addr: %v pub: %v, path: %v",
    51  			i, keyname, keytype, keyaddr, keypub, keypath)
    52  	}
    53  }