[#81] cmd/neofs-cli: Implement "nodeinfo" sub-cmd of "netmap" cmd

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-06 13:36:32 +03:00 committed by Alex Vanin
parent 5d94594007
commit 25398262fc

View file

@ -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())
}
}