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

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/ethereum/go-ethereum/internal/cli/flagset"
     9  	"github.com/ethereum/go-ethereum/internal/cli/server/proto"
    10  )
    11  
    12  // PeersStatusCommand is the command to group the peers commands
    13  type PeersStatusCommand struct {
    14  	*Meta2
    15  }
    16  
    17  // Help implements the cli.Command interface
    18  func (p *PeersStatusCommand) Help() string {
    19  	return `Usage: bor peers status <peer id>
    20  
    21    Display the status of a peer by its id.
    22  
    23    ` + p.Flags().Help()
    24  }
    25  
    26  func (p *PeersStatusCommand) Flags() *flagset.Flagset {
    27  	flags := p.NewFlagSet("peers status")
    28  
    29  	return flags
    30  }
    31  
    32  // Synopsis implements the cli.Command interface
    33  func (c *PeersStatusCommand) Synopsis() string {
    34  	return "Display the status of a peer"
    35  }
    36  
    37  // Run implements the cli.Command interface
    38  func (c *PeersStatusCommand) Run(args []string) int {
    39  	flags := c.Flags()
    40  	if err := flags.Parse(args); err != nil {
    41  		c.UI.Error(err.Error())
    42  		return 1
    43  	}
    44  
    45  	args = flags.Args()
    46  	if len(args) != 1 {
    47  		c.UI.Error("No enode address provided")
    48  		return 1
    49  	}
    50  
    51  	borClt, err := c.BorConn()
    52  	if err != nil {
    53  		c.UI.Error(err.Error())
    54  		return 1
    55  	}
    56  
    57  	req := &proto.PeersStatusRequest{
    58  		Enode: args[0],
    59  	}
    60  	resp, err := borClt.PeersStatus(context.Background(), req)
    61  	if err != nil {
    62  		c.UI.Error(err.Error())
    63  		return 1
    64  	}
    65  
    66  	c.UI.Output(formatPeer(resp.Peer))
    67  	return 0
    68  }
    69  
    70  func formatPeer(peer *proto.Peer) string {
    71  	base := formatKV([]string{
    72  		fmt.Sprintf("Name|%s", peer.Name),
    73  		fmt.Sprintf("ID|%s", peer.Id),
    74  		fmt.Sprintf("ENR|%s", peer.Enr),
    75  		fmt.Sprintf("Capabilities|%s", strings.Join(peer.Caps, ",")),
    76  		fmt.Sprintf("Enode|%s", peer.Enode),
    77  		fmt.Sprintf("Static|%v", peer.Static),
    78  		fmt.Sprintf("Trusted|%v", peer.Trusted),
    79  	})
    80  	return base
    81  }