[#1382] neofs-cli: move netmap command to a separate package

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-05-31 09:53:54 +03:00 committed by fyrchik
parent 81f925d5a0
commit 12bc5607f7
5 changed files with 158 additions and 119 deletions

View file

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

View file

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

View file

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

View file

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

View file

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