github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/internal/cli/account_list.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/ethereum/go-ethereum/accounts"
     8  	"github.com/ethereum/go-ethereum/internal/cli/flagset"
     9  )
    10  
    11  type AccountListCommand struct {
    12  	*Meta
    13  }
    14  
    15  // MarkDown implements cli.MarkDown interface
    16  func (a *AccountListCommand) MarkDown() string {
    17  	items := []string{
    18  		"# Account list",
    19  		"The `account list` command lists all the accounts in the Bor data directory.",
    20  		a.Flags().MarkDown(),
    21  	}
    22  
    23  	return strings.Join(items, "\n\n")
    24  }
    25  
    26  // Help implements the cli.Command interface
    27  func (a *AccountListCommand) Help() string {
    28  	return `Usage: bor account list
    29  
    30    List the local accounts.
    31  
    32    ` + a.Flags().Help()
    33  }
    34  
    35  func (a *AccountListCommand) Flags() *flagset.Flagset {
    36  	return a.NewFlagSet("account list")
    37  }
    38  
    39  // Synopsis implements the cli.Command interface
    40  func (a *AccountListCommand) Synopsis() string {
    41  	return "List the local accounts"
    42  }
    43  
    44  // Run implements the cli.Command interface
    45  func (a *AccountListCommand) Run(args []string) int {
    46  	flags := a.Flags()
    47  	if err := flags.Parse(args); err != nil {
    48  		a.UI.Error(err.Error())
    49  		return 1
    50  	}
    51  
    52  	keystore, err := a.GetKeystore()
    53  	if err != nil {
    54  		a.UI.Error(fmt.Sprintf("Failed to get keystore: %v", err))
    55  		return 1
    56  	}
    57  
    58  	a.UI.Output(formatAccounts(keystore.Accounts()))
    59  
    60  	return 0
    61  }
    62  
    63  func formatAccounts(accts []accounts.Account) string {
    64  	if len(accts) == 0 {
    65  		return "No accounts found"
    66  	}
    67  
    68  	rows := make([]string, len(accts)+1)
    69  	rows[0] = "Index|Address"
    70  
    71  	for i, d := range accts {
    72  		rows[i+1] = fmt.Sprintf("%d|%s",
    73  			i,
    74  			d.Address.String())
    75  	}
    76  
    77  	return formatList(rows)
    78  }