This repository has been archived on 2024-04-07. You can view files and clone it, but cannot push or open issues or pull requests.
go-monero/cmd/monero/commands/daemon/sync_info.go
2022-10-05 09:40:23 +02:00

84 lines
1.7 KiB
Go

package daemon
import (
"fmt"
"sort"
"time"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"git.gammaspectra.live/P2Pool/go-monero/cmd/monero/display"
"git.gammaspectra.live/P2Pool/go-monero/cmd/monero/options"
"git.gammaspectra.live/P2Pool/go-monero/pkg/rpc/daemon"
)
type syncInfoCommand struct {
JSON bool
}
func (c *syncInfoCommand) Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sync-info",
Short: "daemon's chain synchronization info",
RunE: c.RunE,
}
cmd.Flags().BoolVar(&c.JSON, "json",
false, "whether or not to output the result as json")
return cmd
}
func (c *syncInfoCommand) RunE(_ *cobra.Command, _ []string) error {
ctx, cancel := options.RootOpts.Context()
defer cancel()
client, err := options.RootOpts.Client()
if err != nil {
return fmt.Errorf("client: %w", err)
}
resp, err := client.SyncInfo(ctx)
if err != nil {
return fmt.Errorf("get block count: %w", err)
}
if c.JSON {
return display.JSON(resp)
}
c.pretty(resp)
return nil
}
// nolint:forbidigo
func (c *syncInfoCommand) pretty(v *daemon.SyncInfoResult) {
table := display.NewTable()
table.AddRow("HEIGHT", v.Height)
table.AddRow("")
sort.Slice(v.Peers, func(i, j int) bool {
return v.Peers[i].Info.LiveTime > v.Peers[j].Info.LiveTime
})
table.AddRow("ADDR", "IN", "STATE", "TIME", "RECV", "SEND")
for _, peer := range v.Peers {
table.AddRow(
peer.Info.Address,
peer.Info.Incoming,
peer.Info.State,
time.Duration(peer.Info.LiveTime)*time.Second,
humanize.Bytes(peer.Info.RecvCount),
humanize.Bytes(peer.Info.SendCount),
)
}
fmt.Println(table)
}
func init() {
RootCommand.AddCommand((&syncInfoCommand{}).Cmd())
}