diff --git a/cmd/neofs-cli/modules/netmap/get_epoch.go b/cmd/neofs-cli/modules/netmap/get_epoch.go new file mode 100644 index 000000000..9ac6e64d3 --- /dev/null +++ b/cmd/neofs-cli/modules/netmap/get_epoch.go @@ -0,0 +1,34 @@ +package netmap + +import ( + internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key" + "github.com/spf13/cobra" +) + +var getEpochCmd = &cobra.Command{ + Use: "epoch", + Short: "Get current epoch number", + Long: "Get current epoch number", + Run: func(cmd *cobra.Command, args []string) { + p := key.GetOrGenerate(cmd) + cli := internalclient.GetSDKClientByFlag(cmd, p, commonflags.RPC) + + var prm internalclient.NetworkInfoPrm + prm.SetClient(cli) + + res, err := internalclient.NetworkInfo(prm) + common.ExitOnErr(cmd, "rpc error: %w", err) + + netInfo := res.NetworkInfo() + + cmd.Println(netInfo.CurrentEpoch()) + }, +} + +func initGetEpochCmd() { + commonflags.Init(getEpochCmd) + commonflags.InitAPI(getEpochCmd) +} diff --git a/cmd/neofs-cli/modules/netmap.go b/cmd/neofs-cli/modules/netmap/netinfo.go similarity index 56% rename from cmd/neofs-cli/modules/netmap.go rename to cmd/neofs-cli/modules/netmap/netinfo.go index e6bec1eae..f02c6800c 100644 --- a/cmd/neofs-cli/modules/netmap.go +++ b/cmd/neofs-cli/modules/netmap/netinfo.go @@ -1,4 +1,4 @@ -package cmd +package netmap import ( "encoding/hex" @@ -8,87 +8,60 @@ import ( internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key" nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" "github.com/nspcc-dev/neofs-sdk-go/netmap" "github.com/spf13/cobra" ) -var ( - nodeInfoJSON bool +type netCfgWriter cobra.Command - netmapSnapshotJSON bool -) - -// netmapCmd represents the netmap command -var netmapCmd = &cobra.Command{ - Use: "netmap", - Short: "Operations with Network Map", - Long: `Operations with Network Map`, - PersistentPreRun: func(cmd *cobra.Command, args []string) { - // bind exactly that cmd's flags to - // the viper before execution - commonflags.Bind(cmd) - commonflags.BindAPI(cmd) - }, -} - -func init() { - netmapChildCommands := []*cobra.Command{ - getEpochCmd, - localNodeInfoCmd, - netInfoCmd, - } - - rootCmd.AddCommand(netmapCmd) - netmapCmd.AddCommand(netmapChildCommands...) - - commonflags.Init(getEpochCmd) - commonflags.Init(netInfoCmd) - - commonflags.Init(localNodeInfoCmd) - localNodeInfoCmd.Flags().BoolVar(&nodeInfoJSON, "json", false, "print node info in JSON format") - - for _, netmapCommand := range netmapChildCommands { - commonflags.InitAPI(netmapCommand) - } -} - -var getEpochCmd = &cobra.Command{ - Use: "epoch", - Short: "Get current epoch number", - Long: "Get current epoch number", +var netInfoCmd = &cobra.Command{ + Use: "netinfo", + Short: "Get information about NeoFS network", + Long: "Get information about NeoFS network", Run: func(cmd *cobra.Command, args []string) { - var prm internalclient.NetworkInfoPrm + p := key.GetOrGenerate(cmd) + cli := internalclient.GetSDKClientByFlag(cmd, p, commonflags.RPC) - prepareAPIClient(cmd, &prm) + var prm internalclient.NetworkInfoPrm + prm.SetClient(cli) res, err := internalclient.NetworkInfo(prm) common.ExitOnErr(cmd, "rpc error: %w", err) netInfo := res.NetworkInfo() - cmd.Println(netInfo.CurrentEpoch()) + cmd.Printf("Epoch: %d\n", netInfo.CurrentEpoch()) + + magic := netInfo.MagicNumber() + cmd.Printf("Network magic: [%s] %d\n", netmode.Magic(magic), magic) + + cmd.Printf("Time per block: %s\n", time.Duration(netInfo.MsPerBlock())*time.Millisecond) + + netCfg := netInfo.NetworkConfig() + + cmd.Println("NeoFS network configuration") + + err = nmClient.WriteConfig((*netCfgWriter)(cmd), func(f func(key []byte, val []byte) error) error { + var err error + + netCfg.IterateParameters(func(prm *netmap.NetworkParameter) bool { + err = f(prm.Key(), prm.Value()) + return err != nil + }) + + return err + }) + common.ExitOnErr(cmd, "read config: %w", err) }, } -var localNodeInfoCmd = &cobra.Command{ - Use: "nodeinfo", - Short: "Get local node info", - Long: `Get local node info`, - Run: func(cmd *cobra.Command, args []string) { - var prm internalclient.NodeInfoPrm - - prepareAPIClient(cmd, &prm) - - res, err := internalclient.NodeInfo(prm) - common.ExitOnErr(cmd, "rpc error: %w", err) - - prettyPrintNodeInfo(cmd, res.NodeInfo(), nodeInfoJSON) - }, +func initNetInfoCmd() { + commonflags.Init(netInfoCmd) + commonflags.InitAPI(netInfoCmd) } -type netCfgWriter cobra.Command - func (x *netCfgWriter) print(name string, v interface{}, unknown bool) { var sUnknown string @@ -142,59 +115,3 @@ func (x *netCfgWriter) InnerRingCandidateFee(v uint64) { func (x *netCfgWriter) WithdrawFee(v uint64) { x.print("Withdraw fee", v, false) } - -var netInfoCmd = &cobra.Command{ - Use: "netinfo", - Short: "Get information about NeoFS network", - Long: "Get information about NeoFS network", - Run: func(cmd *cobra.Command, args []string) { - var prm internalclient.NetworkInfoPrm - - prepareAPIClient(cmd, &prm) - - res, err := internalclient.NetworkInfo(prm) - common.ExitOnErr(cmd, "rpc error: %w", err) - - netInfo := res.NetworkInfo() - - cmd.Printf("Epoch: %d\n", netInfo.CurrentEpoch()) - - magic := netInfo.MagicNumber() - cmd.Printf("Network magic: [%s] %d\n", netmode.Magic(magic), magic) - - cmd.Printf("Time per block: %s\n", time.Duration(netInfo.MsPerBlock())*time.Millisecond) - - netCfg := netInfo.NetworkConfig() - - cmd.Println("NeoFS network configuration") - - err = nmClient.WriteConfig((*netCfgWriter)(cmd), func(f func(key []byte, val []byte) error) error { - var err error - - netCfg.IterateParameters(func(prm *netmap.NetworkParameter) bool { - err = f(prm.Key(), prm.Value()) - return err != nil - }) - - return err - }) - common.ExitOnErr(cmd, "read config: %w", err) - }, -} - -func prettyPrintNodeInfo(cmd *cobra.Command, i *netmap.NodeInfo, jsonEncoding bool) { - if jsonEncoding { - common.PrettyPrintJSON(cmd, i, "node info") - return - } - - cmd.Println("key:", hex.EncodeToString(i.PublicKey())) - cmd.Println("state:", i.State()) - netmap.IterateAllAddresses(i, func(s string) { - cmd.Println("address:", s) - }) - - for _, attribute := range i.Attributes() { - cmd.Printf("attribute: %s=%s\n", attribute.Key(), attribute.Value()) - } -} diff --git a/cmd/neofs-cli/modules/netmap/nodeinfo.go b/cmd/neofs-cli/modules/netmap/nodeinfo.go new file mode 100644 index 000000000..ee397086c --- /dev/null +++ b/cmd/neofs-cli/modules/netmap/nodeinfo.go @@ -0,0 +1,56 @@ +package netmap + +import ( + "encoding/hex" + + internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key" + "github.com/nspcc-dev/neofs-sdk-go/netmap" + "github.com/spf13/cobra" +) + +const nodeInfoJSONFlag = "json" + +var nodeInfoCmd = &cobra.Command{ + Use: "nodeinfo", + Short: "Get local node info", + Long: `Get local node info`, + Run: func(cmd *cobra.Command, args []string) { + p := key.GetOrGenerate(cmd) + cli := internalclient.GetSDKClientByFlag(cmd, p, commonflags.RPC) + + var prm internalclient.NodeInfoPrm + prm.SetClient(cli) + + res, err := internalclient.NodeInfo(prm) + common.ExitOnErr(cmd, "rpc error: %w", err) + + prettyPrintNodeInfo(cmd, res.NodeInfo()) + }, +} + +func initNodeInfoCmd() { + commonflags.Init(nodeInfoCmd) + commonflags.InitAPI(nodeInfoCmd) + nodeInfoCmd.Flags().Bool(nodeInfoJSONFlag, false, "print node info in JSON format") +} + +func prettyPrintNodeInfo(cmd *cobra.Command, i *netmap.NodeInfo) { + isJSON, _ := cmd.Flags().GetBool(nodeInfoJSONFlag) + if isJSON { + common.PrettyPrintJSON(cmd, i, "node info") + return + } + + cmd.Println("key:", hex.EncodeToString(i.PublicKey())) + cmd.Println("state:", i.State()) + netmap.IterateAllAddresses(i, func(s string) { + cmd.Println("address:", s) + }) + + for _, attribute := range i.Attributes() { + cmd.Printf("attribute: %s=%s\n", attribute.Key(), attribute.Value()) + } +} diff --git a/cmd/neofs-cli/modules/netmap/root.go b/cmd/neofs-cli/modules/netmap/root.go new file mode 100644 index 000000000..89e2e34d4 --- /dev/null +++ b/cmd/neofs-cli/modules/netmap/root.go @@ -0,0 +1,30 @@ +package netmap + +import ( + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" + "github.com/spf13/cobra" +) + +var Cmd = &cobra.Command{ + Use: "netmap", + Short: "Operations with Network Map", + Long: `Operations with Network Map`, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + // bind exactly that cmd's flags to + // the viper before execution + commonflags.Bind(cmd) + commonflags.BindAPI(cmd) + }, +} + +func init() { + Cmd.AddCommand( + getEpochCmd, + nodeInfoCmd, + netInfoCmd, + ) + + initGetEpochCmd() + initNetInfoCmd() + initNodeInfoCmd() +} diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 39a83214c..ac047e3f4 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -15,6 +15,7 @@ import ( "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/acl" bearerCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/bearer" controlCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/control" + netmapCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/netmap" sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session" utilCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/util" "github.com/nspcc-dev/neofs-node/misc" @@ -78,6 +79,7 @@ func init() { rootCmd.AddCommand(accountingCli.Cmd) rootCmd.AddCommand(controlCli.Cmd) rootCmd.AddCommand(utilCli.Cmd) + rootCmd.AddCommand(netmapCli.Cmd) rootCmd.AddCommand(gendoc.Command(rootCmd)) }