From 25398262fcaa1290de02fc21ff5a006a70ee952a Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 6 Nov 2020 13:36:32 +0300 Subject: [PATCH] [#81] cmd/neofs-cli: Implement "nodeinfo" sub-cmd of "netmap" cmd Signed-off-by: Leonard Lyubich --- cmd/neofs-cli/modules/netmap.go | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/cmd/neofs-cli/modules/netmap.go b/cmd/neofs-cli/modules/netmap.go index 38ca44fff..74fa73e61 100644 --- a/cmd/neofs-cli/modules/netmap.go +++ b/cmd/neofs-cli/modules/netmap.go @@ -1,12 +1,20 @@ package cmd import ( + "bytes" "context" + "encoding/json" "fmt" + "github.com/mr-tron/base58" + "github.com/nspcc-dev/neofs-api-go/pkg/netmap" "github.com/spf13/cobra" ) +var ( + nodeInfoJSON bool +) + // netmapCmd represents the netmap command var netmapCmd = &cobra.Command{ Use: "netmap", @@ -22,7 +30,10 @@ func init() { netmapCmd.AddCommand( getEpochCmd, + localNodeInfoCmd, ) + + localNodeInfoCmd.Flags().BoolVar(&nodeInfoJSON, "json", false, "print node info in JSON format") } var getEpochCmd = &cobra.Command{ @@ -45,3 +56,51 @@ var getEpochCmd = &cobra.Command{ return nil }, } + +var localNodeInfoCmd = &cobra.Command{ + Use: "nodeinfo", + Short: "Get local node info", + Long: `Get local node info`, + RunE: func(cmd *cobra.Command, args []string) error { + cli, err := getSDKClient() + if err != nil { + return err + } + + nodeInfo, err := cli.EndpointInfo(context.Background()) + if err != nil { + return fmt.Errorf("rpc error: %w", err) + } + + prettyPrintNodeInfo(nodeInfo, nodeInfoJSON) + + return nil + }, +} + +func prettyPrintNodeInfo(i *netmap.NodeInfo, jsonEncoding bool) { + if jsonEncoding { + data, err := netmap.NodeInfoToJSON(i) + if err != nil { + printVerbose("Can't convert container to json: %w", err) + return + } + + buf := new(bytes.Buffer) + if err := json.Indent(buf, data, "", " "); err != nil { + printVerbose("Can't pretty print json: %w", err) + } + + fmt.Println(buf) + + return + } + + fmt.Println("key:", base58.Encode(i.PublicKey())) + fmt.Println("address:", i.Address()) + fmt.Println("state:", i.State()) + + for _, attribute := range i.Attributes() { + fmt.Printf("attribute: %s=%s\n", attribute.Key(), attribute.Value()) + } +}