github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/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/flagset"
     9  	"github.com/ethereum/go-ethereum/internal/cli/server/proto"
    10  )
    11  
    12  // StatusCommand is the command to output the status of the client
    13  type StatusCommand struct {
    14  	*Meta2
    15  
    16  	wait bool
    17  }
    18  
    19  func (c *StatusCommand) Flags() *flagset.Flagset {
    20  	flags := c.NewFlagSet("status")
    21  
    22  	flags.BoolFlag(&flagset.BoolFlag{
    23  		Name:    "w",
    24  		Value:   &c.wait,
    25  		Usage:   "wait for Bor node to be available",
    26  		Default: false,
    27  	})
    28  
    29  	return flags
    30  }
    31  
    32  // MarkDown implements cli.MarkDown interface
    33  func (p *StatusCommand) MarkDown() string {
    34  	items := []string{
    35  		"# Status",
    36  		"The ```status``` command outputs the status of the client.",
    37  	}
    38  
    39  	return strings.Join(items, "\n\n")
    40  }
    41  
    42  // Help implements the cli.Command interface
    43  func (p *StatusCommand) Help() string {
    44  	return `Usage: bor status
    45  
    46    Output the status of the client`
    47  }
    48  
    49  // Synopsis implements the cli.Command interface
    50  func (c *StatusCommand) Synopsis() string {
    51  	return "Output the status of the client"
    52  }
    53  
    54  // Run implements the cli.Command interface
    55  func (c *StatusCommand) Run(args []string) int {
    56  	flags := c.Flags()
    57  	if err := flags.Parse(args); err != nil {
    58  		c.UI.Error(err.Error())
    59  		return 1
    60  	}
    61  
    62  	borClt, err := c.BorConn()
    63  	if err != nil {
    64  		c.UI.Error(err.Error())
    65  		return 1
    66  	}
    67  
    68  	status, err := borClt.Status(context.Background(), &proto.StatusRequest{Wait: c.wait})
    69  	if err != nil {
    70  		c.UI.Error(err.Error())
    71  		return 1
    72  	}
    73  
    74  	c.UI.Output(printStatus(status))
    75  
    76  	return 0
    77  }
    78  
    79  func printStatus(status *proto.StatusResponse) string {
    80  	printHeader := func(h *proto.Header) string {
    81  		return formatKV([]string{
    82  			fmt.Sprintf("Hash|%s", h.Hash),
    83  			fmt.Sprintf("Number|%d", h.Number),
    84  		})
    85  	}
    86  
    87  	forks := make([]string, len(status.Forks)+1)
    88  	forks[0] = "Name|Block|Enabled"
    89  
    90  	for i, d := range status.Forks {
    91  		forks[i+1] = fmt.Sprintf("%s|%d|%v", d.Name, d.Block, !d.Disabled)
    92  	}
    93  
    94  	full := []string{
    95  		"General",
    96  		formatKV([]string{
    97  			fmt.Sprintf("Num peers|%d", status.NumPeers),
    98  			fmt.Sprintf("Sync mode|%s", status.SyncMode),
    99  		}),
   100  		"\nCurrent Header",
   101  		printHeader(status.CurrentHeader),
   102  		"\nCurrent Block",
   103  		printHeader(status.CurrentBlock),
   104  		"\nSyncing",
   105  		formatKV([]string{
   106  			fmt.Sprintf("Current block|%d", status.Syncing.CurrentBlock),
   107  			fmt.Sprintf("Highest block|%d", status.Syncing.HighestBlock),
   108  			fmt.Sprintf("Starting block|%d", status.Syncing.StartingBlock),
   109  		}),
   110  		"\nForks",
   111  		formatList(forks),
   112  	}
   113  
   114  	return strings.Join(full, "\n")
   115  }