2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-11-06 10:36:32 +00:00
|
|
|
"bytes"
|
2020-11-05 07:15:28 +00:00
|
|
|
"context"
|
2020-11-06 13:23:31 +00:00
|
|
|
"encoding/hex"
|
2020-11-06 10:36:32 +00:00
|
|
|
"encoding/json"
|
2020-08-04 14:46:12 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-11-16 08:25:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2020-11-06 10:36:32 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2020-08-04 14:46:12 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2020-11-06 10:36:32 +00:00
|
|
|
var (
|
|
|
|
nodeInfoJSON bool
|
|
|
|
)
|
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
// netmapCmd represents the netmap command
|
|
|
|
var netmapCmd = &cobra.Command{
|
|
|
|
Use: "netmap",
|
|
|
|
Short: "Operations with Network Map",
|
|
|
|
Long: `Operations with Network Map`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
fmt.Println("netmap called")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(netmapCmd)
|
|
|
|
|
2020-11-05 07:15:28 +00:00
|
|
|
netmapCmd.AddCommand(
|
|
|
|
getEpochCmd,
|
2020-11-06 10:36:32 +00:00
|
|
|
localNodeInfoCmd,
|
2020-11-05 07:15:28 +00:00
|
|
|
)
|
2020-11-06 10:36:32 +00:00
|
|
|
|
|
|
|
localNodeInfoCmd.Flags().BoolVar(&nodeInfoJSON, "json", false, "print node info in JSON format")
|
2020-11-05 07:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var getEpochCmd = &cobra.Command{
|
|
|
|
Use: "epoch",
|
|
|
|
Short: "Get current epoch number",
|
|
|
|
Long: "Get current epoch number",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
cli, err := getSDKClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2020-11-16 08:25:42 +00:00
|
|
|
e, err := cli.Epoch(context.Background(), client.WithTTL(getTTL()))
|
2020-11-05 07:15:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("rpc error: %w", err)
|
|
|
|
}
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2020-11-05 07:15:28 +00:00
|
|
|
fmt.Println(e)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
2020-11-06 10:36:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:25:42 +00:00
|
|
|
nodeInfo, err := cli.EndpointInfo(context.Background(), client.WithTTL(getTTL()))
|
2020-11-06 10:36:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("rpc error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
prettyPrintNodeInfo(nodeInfo, nodeInfoJSON)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func prettyPrintNodeInfo(i *netmap.NodeInfo, jsonEncoding bool) {
|
|
|
|
if jsonEncoding {
|
2020-11-16 09:43:52 +00:00
|
|
|
data, err := i.MarshalJSON()
|
2020-11-06 10:36:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-06 13:23:31 +00:00
|
|
|
fmt.Println("key:", hex.EncodeToString(i.PublicKey()))
|
2020-11-06 10:36:32 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|