github.com/shrimpyuk/bor@v0.2.15-0.20220224151350-fb4ec6020bae/internal/cli/peers_list.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  // PeersListCommand is the command to group the peers commands
    13  type PeersListCommand struct {
    14  	*Meta2
    15  }
    16  
    17  // Help implements the cli.Command interface
    18  func (p *PeersListCommand) Help() string {
    19  	return `Usage: bor peers list
    20  
    21    Lists the connected peers
    22  
    23    ` + p.Flags().Help()
    24  }
    25  
    26  func (p *PeersListCommand) Flags() *flagset.Flagset {
    27  	flags := p.NewFlagSet("peers list")
    28  
    29  	return flags
    30  }
    31  
    32  // Synopsis implements the cli.Command interface
    33  func (c *PeersListCommand) Synopsis() string {
    34  	return ""
    35  }
    36  
    37  // Run implements the cli.Command interface
    38  func (c *PeersListCommand) 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  	borClt, err := c.BorConn()
    46  	if err != nil {
    47  		c.UI.Error(err.Error())
    48  		return 1
    49  	}
    50  
    51  	req := &proto.PeersListRequest{}
    52  	resp, err := borClt.PeersList(context.Background(), req)
    53  	if err != nil {
    54  		c.UI.Error(err.Error())
    55  		return 1
    56  	}
    57  
    58  	c.UI.Output(formatPeers(resp.Peers))
    59  	return 0
    60  }
    61  
    62  func formatPeers(peers []*proto.Peer) string {
    63  	if len(peers) == 0 {
    64  		return "No peers found"
    65  	}
    66  
    67  	rows := make([]string, len(peers)+1)
    68  	rows[0] = "ID|Enode|Name|Caps|Static|Trusted"
    69  	for i, d := range peers {
    70  		enode := strings.TrimPrefix(d.Enode, "enode://")
    71  
    72  		rows[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%v",
    73  			d.Id,
    74  			enode[:10],
    75  			d.Name,
    76  			strings.Join(d.Caps, ","),
    77  			d.Static,
    78  			d.Trusted)
    79  	}
    80  	return formatList(rows)
    81  }