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

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/ethereum/go-ethereum/internal/cli/flagset"
     8  	"github.com/ethereum/go-ethereum/internal/cli/server/proto"
     9  )
    10  
    11  // PeersAddCommand is the command to group the peers commands
    12  type PeersAddCommand struct {
    13  	*Meta2
    14  
    15  	trusted bool
    16  }
    17  
    18  // MarkDown implements cli.MarkDown interface
    19  func (p *PeersAddCommand) MarkDown() string {
    20  	items := []string{
    21  		"# Peers add",
    22  		"The ```peers add <enode>``` command joins the local client to another remote peer.",
    23  		p.Flags().MarkDown(),
    24  	}
    25  
    26  	return strings.Join(items, "\n\n")
    27  }
    28  
    29  // Help implements the cli.Command interface
    30  func (p *PeersAddCommand) Help() string {
    31  	return `Usage: bor peers add <enode>
    32  
    33    Joins the local client to another remote peer.
    34  
    35    ` + p.Flags().Help()
    36  }
    37  
    38  func (p *PeersAddCommand) Flags() *flagset.Flagset {
    39  	flags := p.NewFlagSet("peers add")
    40  
    41  	flags.BoolFlag(&flagset.BoolFlag{
    42  		Name:  "trusted",
    43  		Usage: "Add the peer as a trusted",
    44  		Value: &p.trusted,
    45  	})
    46  
    47  	return flags
    48  }
    49  
    50  // Synopsis implements the cli.Command interface
    51  func (c *PeersAddCommand) Synopsis() string {
    52  	return "Join the client to a remote peer"
    53  }
    54  
    55  // Run implements the cli.Command interface
    56  func (c *PeersAddCommand) Run(args []string) int {
    57  	flags := c.Flags()
    58  	if err := flags.Parse(args); err != nil {
    59  		c.UI.Error(err.Error())
    60  		return 1
    61  	}
    62  
    63  	args = flags.Args()
    64  	if len(args) != 1 {
    65  		c.UI.Error("No enode address provided")
    66  		return 1
    67  	}
    68  
    69  	borClt, err := c.BorConn()
    70  	if err != nil {
    71  		c.UI.Error(err.Error())
    72  		return 1
    73  	}
    74  
    75  	req := &proto.PeersAddRequest{
    76  		Enode:   args[0],
    77  		Trusted: c.trusted,
    78  	}
    79  	if _, err := borClt.PeersAdd(context.Background(), req); err != nil {
    80  		c.UI.Error(err.Error())
    81  		return 1
    82  	}
    83  
    84  	return 0
    85  }