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/get_connections.go
Ciro S. Costa 9cf4665b88 cmd: improve pretty-printing, add get-version
- update get-height's description
- update some fields to prefer uint64 rather than `int`

	all those fields would never take negative values and should be
	able to grow quite a lot

- switch commands from display to humanize

	`humanize` gives us some pretty handy human readable
	conversions, so let's just go with it rather than rolling our
	own

- add get-version command

- prettify get-transaction pool

	example out (hash reduced in size):

	AGE             HASH                FEE (µɱ)        FEE (µɱ per-kB) SIZE    in/out
	1 minute ago    1a053e4058839b21c4  9                6.4            1.5 kB  1/2
	1 minute ago    fb61680a1584ca1ec3  9                6.4            1.5 kB  1/2
	1 minute ago    a58de0d2747cdd6a5d  12               6.4            2.0 kB  2/2
	1 minute ago    c54f4b33ed81335f78  308             160.6           2.0 kB  2/2
	57 seconds ago  ab210c55bd9c3efe09  12               6.4            2.0 kB  2/2
	55 seconds ago  88df3311e19b1280b5  9                6.4            1.5 kB  1/2
	51 seconds ago  a3aa674ed3d4eb56c6  127             32.5            4.0 kB  6/2
	51 seconds ago  e896abe686d2f816fc  9                6.4            1.5 kB  1/2
	31 seconds ago  eb6ea6662ce754d025  9                6.4            1.5 kB  1/2
	31 seconds ago  592204223e607cc5d9  9                6.4            1.5 kB  1/2
	24 seconds ago  ba8d52f90fd45dd32d  12               6.4            2.0 kB  2/2
	24 seconds ago  792e68ded7805037c9  12               6.4            2.0 kB  2/2

- move response types to a single file
- rework pretty-printing of txn-based cmds

Signed-off-by: Ciro S. Costa <utxobr@protonmail.com>
2021-07-16 07:48:30 -04:00

81 lines
1.7 KiB
Go

package daemon
import (
"fmt"
"sort"
"time"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/cirocosta/go-monero/cmd/monero/display"
"github.com/cirocosta/go-monero/cmd/monero/options"
"github.com/cirocosta/go-monero/pkg/rpc/daemon"
)
type getConnectionsCommand struct {
JSON bool
}
func (c *getConnectionsCommand) Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get-connections",
Short: "information about incoming and outgoing connections.",
RunE: c.RunE,
}
cmd.Flags().BoolVar(&c.JSON, "json",
false, "whether or not to output the result as json")
return cmd
}
func (c *getConnectionsCommand) RunE(_ *cobra.Command, _ []string) error {
ctx, cancel := options.RootOptions.Context()
defer cancel()
client, err := options.RootOptions.Client()
if err != nil {
return fmt.Errorf("client: %w", err)
}
resp, err := client.GetConnections(ctx)
if err != nil {
return fmt.Errorf("get block count: %w", err)
}
if c.JSON {
return display.JSON(resp)
}
c.pretty(resp)
return nil
}
func (c *getConnectionsCommand) pretty(v *daemon.GetConnectionsResult) {
table := display.NewTable()
table.AddRow("ADDR", "IN", "STATE", "HEIGHT", "SINCE", "RECV", "SEND")
sort.Slice(v.Connections, func(i, j int) bool {
return v.Connections[i].LiveTime > v.Connections[j].LiveTime
})
for _, connection := range v.Connections {
table.AddRow(
connection.Address,
connection.Incoming,
connection.State,
connection.Height,
humanize.Time(time.Now().Add(-1*time.Duration(connection.LiveTime)*time.Second)),
humanize.IBytes(connection.RecvCount),
humanize.IBytes(connection.SendCount),
)
}
fmt.Println(table)
}
func init() {
RootCommand.AddCommand((&getConnectionsCommand{}).Cmd())
}