go-ethereum/internal/cli/peers_status.go
Jerry a131e427f6 Automatically generate markdown pages from bor CLI
Adding a script that can automatically generate markdown pages from bor
CLI, so we can avoid copy-pasting helper strings whenever a flag is created,
deleted, or modified.

CLI docs could be generated with command `make docs`.
2022-05-09 17:10:36 +02:00

91 lines
2 KiB
Go

package cli
import (
"context"
"fmt"
"strings"
"github.com/ethereum/go-ethereum/internal/cli/flagset"
"github.com/ethereum/go-ethereum/internal/cli/server/proto"
)
// PeersStatusCommand is the command to group the peers commands
type PeersStatusCommand struct {
*Meta2
}
// MarkDown implements cli.MarkDown interface
func (p *PeersStatusCommand) MarkDown() string {
items := []string{
"# Peers status",
"The ```peers status <peer id>``` command displays the status of a peer by its id.",
p.Flags().MarkDown(),
}
return strings.Join(items, "\n\n")
}
// Help implements the cli.Command interface
func (p *PeersStatusCommand) Help() string {
return `Usage: bor peers status <peer id>
Display the status of a peer by its id.
` + p.Flags().Help()
}
func (p *PeersStatusCommand) Flags() *flagset.Flagset {
flags := p.NewFlagSet("peers status")
return flags
}
// Synopsis implements the cli.Command interface
func (c *PeersStatusCommand) Synopsis() string {
return "Display the status of a peer"
}
// Run implements the cli.Command interface
func (c *PeersStatusCommand) Run(args []string) int {
flags := c.Flags()
if err := flags.Parse(args); err != nil {
c.UI.Error(err.Error())
return 1
}
args = flags.Args()
if len(args) != 1 {
c.UI.Error("No enode address provided")
return 1
}
borClt, err := c.BorConn()
if err != nil {
c.UI.Error(err.Error())
return 1
}
req := &proto.PeersStatusRequest{
Enode: args[0],
}
resp, err := borClt.PeersStatus(context.Background(), req)
if err != nil {
c.UI.Error(err.Error())
return 1
}
c.UI.Output(formatPeer(resp.Peer))
return 0
}
func formatPeer(peer *proto.Peer) string {
base := formatKV([]string{
fmt.Sprintf("Name|%s", peer.Name),
fmt.Sprintf("ID|%s", peer.Id),
fmt.Sprintf("ENR|%s", peer.Enr),
fmt.Sprintf("Capabilities|%s", strings.Join(peer.Caps, ",")),
fmt.Sprintf("Enode|%s", peer.Enode),
fmt.Sprintf("Static|%v", peer.Static),
fmt.Sprintf("Trusted|%v", peer.Trusted),
})
return base
}