forked from maticnetwork/bor
/
peers_list.go
96 lines (74 loc) · 1.81 KB
/
peers_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cli
import (
"context"
"fmt"
"strings"
"github.com/ethereum/go-ethereum/internal/cli/flagset"
"github.com/ethereum/go-ethereum/internal/cli/server/proto"
)
// PeersListCommand is the command to group the peers commands
type PeersListCommand struct {
*Meta2
}
// MarkDown implements cli.MarkDown interface
func (p *PeersListCommand) MarkDown() string {
items := []string{
"# Peers add",
"The ```peers list``` command lists the connected peers.",
p.Flags().MarkDown(),
}
return strings.Join(items, "\n\n")
}
// Help implements the cli.Command interface
func (p *PeersListCommand) Help() string {
return `Usage: bor peers list
Lists the connected peers
` + p.Flags().Help()
}
func (p *PeersListCommand) Flags() *flagset.Flagset {
flags := p.NewFlagSet("peers list")
return flags
}
// Synopsis implements the cli.Command interface
func (c *PeersListCommand) Synopsis() string {
return ""
}
// Run implements the cli.Command interface
func (c *PeersListCommand) Run(args []string) int {
flags := c.Flags()
if err := flags.Parse(args); err != nil {
c.UI.Error(err.Error())
return 1
}
borClt, err := c.BorConn()
if err != nil {
c.UI.Error(err.Error())
return 1
}
req := &proto.PeersListRequest{}
resp, err := borClt.PeersList(context.Background(), req)
if err != nil {
c.UI.Error(err.Error())
return 1
}
c.UI.Output(formatPeers(resp.Peers))
return 0
}
func formatPeers(peers []*proto.Peer) string {
if len(peers) == 0 {
return "No peers found"
}
rows := make([]string, len(peers)+1)
rows[0] = "ID|Enode|Name|Caps|Static|Trusted"
for i, d := range peers {
enode := strings.TrimPrefix(d.Enode, "enode://")
rows[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%v",
d.Id,
enode[:10],
d.Name,
strings.Join(d.Caps, ","),
d.Static,
d.Trusted)
}
return formatList(rows)
}