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

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/ethereum/go-ethereum/internal/cli/server/proto"
     9  	"github.com/golang/protobuf/ptypes/empty"
    10  )
    11  
    12  // StatusCommand is the command to output the status of the client
    13  type StatusCommand struct {
    14  	*Meta2
    15  }
    16  
    17  // Help implements the cli.Command interface
    18  func (p *StatusCommand) Help() string {
    19  	return `Usage: bor status
    20  
    21    Output the status of the client`
    22  }
    23  
    24  // Synopsis implements the cli.Command interface
    25  func (c *StatusCommand) Synopsis() string {
    26  	return "Output the status of the client"
    27  }
    28  
    29  // Run implements the cli.Command interface
    30  func (c *StatusCommand) Run(args []string) int {
    31  	flags := c.NewFlagSet("status")
    32  	if err := flags.Parse(args); err != nil {
    33  		c.UI.Error(err.Error())
    34  		return 1
    35  	}
    36  
    37  	borClt, err := c.BorConn()
    38  	if err != nil {
    39  		c.UI.Error(err.Error())
    40  		return 1
    41  	}
    42  
    43  	status, err := borClt.Status(context.Background(), &empty.Empty{})
    44  	if err != nil {
    45  		c.UI.Error(err.Error())
    46  		return 1
    47  	}
    48  
    49  	c.UI.Output(printStatus(status))
    50  	return 0
    51  }
    52  
    53  func printStatus(status *proto.StatusResponse) string {
    54  	printHeader := func(h *proto.Header) string {
    55  		return formatKV([]string{
    56  			fmt.Sprintf("Hash|%s", h.Hash),
    57  			fmt.Sprintf("Number|%d", h.Number),
    58  		})
    59  	}
    60  
    61  	forks := make([]string, len(status.Forks)+1)
    62  	forks[0] = "Name|Block|Enabled"
    63  	for i, d := range status.Forks {
    64  		forks[i+1] = fmt.Sprintf("%s|%d|%v", d.Name, d.Block, !d.Disabled)
    65  	}
    66  
    67  	full := []string{
    68  		"General",
    69  		formatKV([]string{
    70  			fmt.Sprintf("Num peers|%d", status.NumPeers),
    71  			fmt.Sprintf("Sync mode|%s", status.SyncMode),
    72  		}),
    73  		"\nCurrent Header",
    74  		printHeader(status.CurrentHeader),
    75  		"\nCurrent Block",
    76  		printHeader(status.CurrentBlock),
    77  		"\nSyncing",
    78  		formatKV([]string{
    79  			fmt.Sprintf("Current block|%d", status.Syncing.CurrentBlock),
    80  			fmt.Sprintf("Highest block|%d", status.Syncing.HighestBlock),
    81  			fmt.Sprintf("Starting block|%d", status.Syncing.StartingBlock),
    82  		}),
    83  		"\nForks",
    84  		formatList(forks),
    85  	}
    86  	return strings.Join(full, "\n")
    87  }