github.com/shrimpyuk/bor@v0.2.15-0.20220224151350-fb4ec6020bae/internal/cli/account_list.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 6 "github.com/ethereum/go-ethereum/accounts" 7 "github.com/ethereum/go-ethereum/internal/cli/flagset" 8 ) 9 10 type AccountListCommand struct { 11 *Meta 12 } 13 14 // Help implements the cli.Command interface 15 func (a *AccountListCommand) Help() string { 16 return `Usage: bor account list 17 18 List the local accounts. 19 20 ` + a.Flags().Help() 21 } 22 23 func (a *AccountListCommand) Flags() *flagset.Flagset { 24 return a.NewFlagSet("account list") 25 } 26 27 // Synopsis implements the cli.Command interface 28 func (a *AccountListCommand) Synopsis() string { 29 return "List the local accounts" 30 } 31 32 // Run implements the cli.Command interface 33 func (a *AccountListCommand) Run(args []string) int { 34 flags := a.Flags() 35 if err := flags.Parse(args); err != nil { 36 a.UI.Error(err.Error()) 37 return 1 38 } 39 40 keystore, err := a.GetKeystore() 41 if err != nil { 42 a.UI.Error(fmt.Sprintf("Failed to get keystore: %v", err)) 43 return 1 44 } 45 a.UI.Output(formatAccounts(keystore.Accounts())) 46 return 0 47 } 48 49 func formatAccounts(accts []accounts.Account) string { 50 if len(accts) == 0 { 51 return "No accounts found" 52 } 53 54 rows := make([]string, len(accts)+1) 55 rows[0] = "Index|Address" 56 for i, d := range accts { 57 rows[i+1] = fmt.Sprintf("%d|%s", 58 i, 59 d.Address.String()) 60 } 61 return formatList(rows) 62 }