forked from TrueCloudLab/frostfs-node
[#624] cli: Do not print help on any failure
Change usage function to `Run`(does not return err). Log errors with `cmd.PrintErrln`. Change all `fmt.Print*` to `cmd.Print*`. Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
parent
af72412b77
commit
02ca1c4cc1
7 changed files with 479 additions and 360 deletions
|
@ -4,7 +4,6 @@ import (
|
|||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"github.com/mr-tron/base58"
|
||||
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
||||
|
@ -47,25 +46,26 @@ var getEpochCmd = &cobra.Command{
|
|||
Use: "epoch",
|
||||
Short: "Get current epoch number",
|
||||
Long: "Get current epoch number",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
key, err := getKey()
|
||||
if err != nil {
|
||||
return err
|
||||
cmd.PrintErrln(err)
|
||||
return
|
||||
}
|
||||
|
||||
cli, err := getSDKClient(key)
|
||||
if err != nil {
|
||||
return err
|
||||
cmd.PrintErrln(err)
|
||||
return
|
||||
}
|
||||
|
||||
netInfo, err := cli.NetworkInfo(context.Background(), globalCallOptions()...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rpc error: %w", err)
|
||||
cmd.PrintErrln(fmt.Errorf("rpc error: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(netInfo.CurrentEpoch())
|
||||
|
||||
return nil
|
||||
cmd.Println(netInfo.CurrentEpoch())
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -73,25 +73,26 @@ var localNodeInfoCmd = &cobra.Command{
|
|||
Use: "nodeinfo",
|
||||
Short: "Get local node info",
|
||||
Long: `Get local node info`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
key, err := getKey()
|
||||
if err != nil {
|
||||
return err
|
||||
cmd.PrintErrln(err)
|
||||
return
|
||||
}
|
||||
|
||||
cli, err := getSDKClient(key)
|
||||
if err != nil {
|
||||
return err
|
||||
cmd.PrintErrln(err)
|
||||
return
|
||||
}
|
||||
|
||||
nodeInfo, err := cli.EndpointInfo(context.Background(), globalCallOptions()...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rpc error: %w", err)
|
||||
cmd.PrintErrln(fmt.Errorf("rpc error: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
prettyPrintNodeInfo(nodeInfo.NodeInfo(), nodeInfoJSON)
|
||||
|
||||
return nil
|
||||
prettyPrintNodeInfo(cmd, nodeInfo.NodeInfo(), nodeInfoJSON)
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -99,27 +100,31 @@ var snapshotCmd = &cobra.Command{
|
|||
Use: "snapshot",
|
||||
Short: "Get network map snapshot",
|
||||
Long: "Get network map snapshot",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
key, err := getKey()
|
||||
if err != nil {
|
||||
return err
|
||||
cmd.PrintErrln(err)
|
||||
return
|
||||
}
|
||||
|
||||
req := new(control.NetmapSnapshotRequest)
|
||||
req.SetBody(new(control.NetmapSnapshotRequest_Body))
|
||||
|
||||
if err := controlSvc.SignMessage(key, req); err != nil {
|
||||
return err
|
||||
cmd.PrintErrln(err)
|
||||
return
|
||||
}
|
||||
|
||||
cli, err := getSDKClient(key)
|
||||
if err != nil {
|
||||
return err
|
||||
cmd.PrintErrln(err)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := control.NetmapSnapshot(cli.Raw(), req)
|
||||
if err != nil {
|
||||
return err
|
||||
cmd.PrintErrln(err)
|
||||
return
|
||||
}
|
||||
|
||||
sign := resp.GetSignature()
|
||||
|
@ -127,12 +132,11 @@ var snapshotCmd = &cobra.Command{
|
|||
if err := signature.VerifyDataWithSource(resp, func() ([]byte, []byte) {
|
||||
return sign.GetKey(), sign.GetSign()
|
||||
}); err != nil {
|
||||
return err
|
||||
cmd.PrintErrln(err)
|
||||
return
|
||||
}
|
||||
|
||||
prettyPrintNetmap(resp.GetBody().GetNetmap(), netmapSnapshotJSON)
|
||||
|
||||
return nil
|
||||
prettyPrintNetmap(cmd, resp.GetBody().GetNetmap(), netmapSnapshotJSON)
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -140,63 +144,64 @@ var netInfoCmd = &cobra.Command{
|
|||
Use: "netinfo",
|
||||
Short: "Get information about NeoFS network",
|
||||
Long: "Get information about NeoFS network",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
key, err := getKey()
|
||||
if err != nil {
|
||||
return err
|
||||
cmd.PrintErrln(err)
|
||||
return
|
||||
}
|
||||
|
||||
cli, err := getSDKClient(key)
|
||||
if err != nil {
|
||||
return err
|
||||
cmd.PrintErrln(err)
|
||||
return
|
||||
}
|
||||
|
||||
netInfo, err := cli.NetworkInfo(context.Background(), globalCallOptions()...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rpc error: %w", err)
|
||||
cmd.PrintErrln(fmt.Errorf("rpc error: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
cmd.Printf("Epoch: %d\n", netInfo.CurrentEpoch())
|
||||
|
||||
magic := netInfo.MagicNumber()
|
||||
cmd.Printf("Network magic: [%s] %d\n", netmode.Magic(magic), magic)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func prettyPrintNodeInfo(i *netmap.NodeInfo, jsonEncoding bool) {
|
||||
func prettyPrintNodeInfo(cmd *cobra.Command, i *netmap.NodeInfo, jsonEncoding bool) {
|
||||
if jsonEncoding {
|
||||
printJSONMarshaler(i, "node info")
|
||||
printJSONMarshaler(cmd, i, "node info")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("key:", hex.EncodeToString(i.PublicKey()))
|
||||
fmt.Println("address:", i.Address())
|
||||
fmt.Println("state:", i.State())
|
||||
cmd.Println("key:", hex.EncodeToString(i.PublicKey()))
|
||||
cmd.Println("address:", i.Address())
|
||||
cmd.Println("state:", i.State())
|
||||
|
||||
for _, attribute := range i.Attributes() {
|
||||
fmt.Printf("attribute: %s=%s\n", attribute.Key(), attribute.Value())
|
||||
cmd.Printf("attribute: %s=%s\n", attribute.Key(), attribute.Value())
|
||||
}
|
||||
}
|
||||
|
||||
func prettyPrintNetmap(nm *control.Netmap, jsonEncoding bool) {
|
||||
func prettyPrintNetmap(cmd *cobra.Command, nm *control.Netmap, jsonEncoding bool) {
|
||||
if jsonEncoding {
|
||||
printJSONMarshaler(nm, "netmap")
|
||||
printJSONMarshaler(cmd, nm, "netmap")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Epoch:", nm.GetEpoch())
|
||||
cmd.Println("Epoch:", nm.GetEpoch())
|
||||
|
||||
for i, node := range nm.GetNodes() {
|
||||
fmt.Printf("Node %d: %s %s %s\n", i+1,
|
||||
cmd.Printf("Node %d: %s %s %s\n", i+1,
|
||||
base58.Encode(node.GetPublicKey()),
|
||||
node.GetAddress(),
|
||||
node.GetState(),
|
||||
)
|
||||
|
||||
for _, attr := range node.GetAttributes() {
|
||||
fmt.Printf("\t%s: %s\n", attr.GetKey(), attr.GetValue())
|
||||
cmd.Printf("\t%s: %s\n", attr.GetKey(), attr.GetValue())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue