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

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