github.com/shrimpyuk/bor@v0.2.15-0.20220224151350-fb4ec6020bae/internal/cli/command.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/ethereum/go-ethereum/accounts/keystore"
     8  	"github.com/ethereum/go-ethereum/internal/cli/flagset"
     9  	"github.com/ethereum/go-ethereum/internal/cli/server"
    10  	"github.com/ethereum/go-ethereum/internal/cli/server/proto"
    11  	"github.com/ethereum/go-ethereum/node"
    12  	"github.com/mitchellh/cli"
    13  	"github.com/ryanuber/columnize"
    14  	"google.golang.org/grpc"
    15  )
    16  
    17  func Run(args []string) int {
    18  	commands := commands()
    19  
    20  	cli := &cli.CLI{
    21  		Name:     "bor",
    22  		Args:     args,
    23  		Commands: commands,
    24  	}
    25  
    26  	exitCode, err := cli.Run()
    27  	if err != nil {
    28  		fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())
    29  		return 1
    30  	}
    31  	return exitCode
    32  }
    33  
    34  func commands() map[string]cli.CommandFactory {
    35  	ui := &cli.BasicUi{
    36  		Reader:      os.Stdin,
    37  		Writer:      os.Stdout,
    38  		ErrorWriter: os.Stderr,
    39  	}
    40  
    41  	meta2 := &Meta2{
    42  		UI: ui,
    43  	}
    44  	meta := &Meta{
    45  		UI: ui,
    46  	}
    47  	return map[string]cli.CommandFactory{
    48  		"server": func() (cli.Command, error) {
    49  			return &server.Command{
    50  				UI: ui,
    51  			}, nil
    52  		},
    53  		"version": func() (cli.Command, error) {
    54  			return &VersionCommand{
    55  				UI: ui,
    56  			}, nil
    57  		},
    58  		"debug": func() (cli.Command, error) {
    59  			return &DebugCommand{
    60  				Meta2: meta2,
    61  			}, nil
    62  		},
    63  		"chain": func() (cli.Command, error) {
    64  			return &ChainCommand{
    65  				UI: ui,
    66  			}, nil
    67  		},
    68  		"chain watch": func() (cli.Command, error) {
    69  			return &ChainWatchCommand{
    70  				Meta2: meta2,
    71  			}, nil
    72  		},
    73  		"chain sethead": func() (cli.Command, error) {
    74  			return &ChainSetHeadCommand{
    75  				Meta2: meta2,
    76  			}, nil
    77  		},
    78  		"account": func() (cli.Command, error) {
    79  			return &Account{
    80  				UI: ui,
    81  			}, nil
    82  		},
    83  		"account new": func() (cli.Command, error) {
    84  			return &AccountNewCommand{
    85  				Meta: meta,
    86  			}, nil
    87  		},
    88  		"account import": func() (cli.Command, error) {
    89  			return &AccountImportCommand{
    90  				Meta: meta,
    91  			}, nil
    92  		},
    93  		"account list": func() (cli.Command, error) {
    94  			return &AccountListCommand{
    95  				Meta: meta,
    96  			}, nil
    97  		},
    98  		"peers": func() (cli.Command, error) {
    99  			return &PeersCommand{
   100  				UI: ui,
   101  			}, nil
   102  		},
   103  		"peers add": func() (cli.Command, error) {
   104  			return &PeersAddCommand{
   105  				Meta2: meta2,
   106  			}, nil
   107  		},
   108  		"peers remove": func() (cli.Command, error) {
   109  			return &PeersRemoveCommand{
   110  				Meta2: meta2,
   111  			}, nil
   112  		},
   113  		"peers list": func() (cli.Command, error) {
   114  			return &PeersListCommand{
   115  				Meta2: meta2,
   116  			}, nil
   117  		},
   118  		"peers status": func() (cli.Command, error) {
   119  			return &PeersStatusCommand{
   120  				Meta2: meta2,
   121  			}, nil
   122  		},
   123  		"status": func() (cli.Command, error) {
   124  			return &StatusCommand{
   125  				Meta2: meta2,
   126  			}, nil
   127  		},
   128  	}
   129  }
   130  
   131  type Meta2 struct {
   132  	UI cli.Ui
   133  
   134  	addr string
   135  }
   136  
   137  func (m *Meta2) NewFlagSet(n string) *flagset.Flagset {
   138  	f := flagset.NewFlagSet(n)
   139  
   140  	f.StringFlag(&flagset.StringFlag{
   141  		Name:    "address",
   142  		Value:   &m.addr,
   143  		Usage:   "Address of the grpc endpoint",
   144  		Default: "127.0.0.1:3131",
   145  	})
   146  	return f
   147  }
   148  
   149  func (m *Meta2) Conn() (*grpc.ClientConn, error) {
   150  	conn, err := grpc.Dial(m.addr, grpc.WithInsecure())
   151  	if err != nil {
   152  		return nil, fmt.Errorf("failed to connect to server: %v", err)
   153  	}
   154  	return conn, nil
   155  }
   156  
   157  func (m *Meta2) BorConn() (proto.BorClient, error) {
   158  	conn, err := m.Conn()
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  	return proto.NewBorClient(conn), nil
   163  }
   164  
   165  // Meta is a helper utility for the commands
   166  type Meta struct {
   167  	UI cli.Ui
   168  
   169  	dataDir     string
   170  	keyStoreDir string
   171  }
   172  
   173  func (m *Meta) NewFlagSet(n string) *flagset.Flagset {
   174  	f := flagset.NewFlagSet(n)
   175  
   176  	f.StringFlag(&flagset.StringFlag{
   177  		Name:  "datadir",
   178  		Value: &m.dataDir,
   179  		Usage: "Path of the data directory to store information",
   180  	})
   181  	f.StringFlag(&flagset.StringFlag{
   182  		Name:  "keystore",
   183  		Value: &m.keyStoreDir,
   184  		Usage: "Path of the data directory to store information",
   185  	})
   186  
   187  	return f
   188  }
   189  
   190  func (m *Meta) AskPassword() (string, error) {
   191  	return m.UI.AskSecret("Your new account is locked with a password. Please give a password. Do not forget this password")
   192  }
   193  
   194  func (m *Meta) GetKeystore() (*keystore.KeyStore, error) {
   195  	cfg := node.DefaultConfig
   196  	cfg.DataDir = m.dataDir
   197  	cfg.KeyStoreDir = m.keyStoreDir
   198  
   199  	stack, err := node.New(&cfg)
   200  	if err != nil {
   201  		return nil, err
   202  	}
   203  
   204  	keydir := stack.KeyStoreDir()
   205  	scryptN := keystore.StandardScryptN
   206  	scryptP := keystore.StandardScryptP
   207  
   208  	keys := keystore.NewKeyStore(keydir, scryptN, scryptP)
   209  	return keys, nil
   210  }
   211  
   212  func formatList(in []string) string {
   213  	columnConf := columnize.DefaultConfig()
   214  	columnConf.Empty = "<none>"
   215  	return columnize.Format(in, columnConf)
   216  }
   217  
   218  func formatKV(in []string) string {
   219  	columnConf := columnize.DefaultConfig()
   220  	columnConf.Empty = "<none>"
   221  	columnConf.Glue = " = "
   222  	return columnize.Format(in, columnConf)
   223  }