[#1379] neofs-cli: Move common flags to a separate package

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-05-18 12:22:02 +03:00 committed by fyrchik
parent 4be5dce848
commit cbc4ca800d
15 changed files with 151 additions and 135 deletions

View file

@ -0,0 +1,65 @@
package commonflags
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Common CLI flag keys, shorthands, default
// values and their usage descriptions.
const (
GenerateKey = "generate-key"
generateKeyShorthand = "g"
generateKeyDefault = false
generateKeyUsage = "generate new private key"
WalletPath = "wallet"
WalletPathShorthand = "w"
WalletPathDefault = ""
WalletPathUsage = "WIF (NEP-2) string or path to the wallet or binary key"
Account = "address"
AccountShorthand = ""
AccountDefault = ""
AccountUsage = "address of wallet account"
RPC = "rpc-endpoint"
RPCShorthand = "r"
RPCDefault = ""
RPCUsage = "remote node address (as 'multiaddr' or '<host>:<port>')"
Verbose = "verbose"
VerboseShorthand = "v"
VerboseUsage = "verbose output"
)
// Init adds common flags to the command:
// - GenerateKey
// - WalletPath
// - Account
// - RPC
func Init(cmd *cobra.Command) {
InitWithoutRPC(cmd)
ff := cmd.Flags()
ff.StringP(RPC, RPCShorthand, RPCDefault, RPCUsage)
}
// InitWithoutRPC is similar to Init but doesn't create the RPC flag.
func InitWithoutRPC(cmd *cobra.Command) {
ff := cmd.Flags()
ff.BoolP(GenerateKey, generateKeyShorthand, generateKeyDefault, generateKeyUsage)
ff.StringP(WalletPath, WalletPathShorthand, WalletPathDefault, WalletPathUsage)
ff.StringP(Account, AccountShorthand, AccountDefault, AccountUsage)
}
// Bind binds common command flags to the viper.
func Bind(cmd *cobra.Command) {
ff := cmd.Flags()
_ = viper.BindPFlag(GenerateKey, ff.Lookup(GenerateKey))
_ = viper.BindPFlag(WalletPath, ff.Lookup(WalletPath))
_ = viper.BindPFlag(Account, ff.Lookup(Account))
_ = viper.BindPFlag(RPC, ff.Lookup(RPC))
}

View file

@ -5,6 +5,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/util/precision" "github.com/nspcc-dev/neofs-node/pkg/util/precision"
"github.com/nspcc-dev/neofs-sdk-go/accounting" "github.com/nspcc-dev/neofs-sdk-go/accounting"
"github.com/nspcc-dev/neofs-sdk-go/owner" "github.com/nspcc-dev/neofs-sdk-go/owner"
@ -24,9 +25,9 @@ var accountingCmd = &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
flags := cmd.Flags() flags := cmd.Flags()
_ = viper.BindPFlag(walletPath, flags.Lookup(walletPath)) _ = viper.BindPFlag(commonflags.WalletPath, flags.Lookup(commonflags.WalletPath))
_ = viper.BindPFlag(address, flags.Lookup(address)) _ = viper.BindPFlag(commonflags.Account, flags.Lookup(commonflags.Account))
_ = viper.BindPFlag(rpc, flags.Lookup(rpc)) _ = viper.BindPFlag(commonflags.RPC, flags.Lookup(commonflags.RPC))
}, },
} }
@ -63,9 +64,9 @@ var accountingBalanceCmd = &cobra.Command{
func initAccountingBalanceCmd() { func initAccountingBalanceCmd() {
ff := accountingBalanceCmd.Flags() ff := accountingBalanceCmd.Flags()
ff.StringP(walletPath, walletPathShorthand, walletPathDefault, walletPathUsage) ff.StringP(commonflags.WalletPath, commonflags.WalletPathShorthand, commonflags.WalletPathDefault, commonflags.WalletPathUsage)
ff.StringP(address, addressShorthand, addressDefault, addressUsage) ff.StringP(commonflags.Account, commonflags.AccountShorthand, commonflags.AccountDefault, commonflags.AccountUsage)
ff.StringP(rpc, rpcShorthand, rpcDefault, rpcUsage) ff.StringP(commonflags.RPC, commonflags.RPCShorthand, commonflags.RPCDefault, commonflags.RPCUsage)
accountingBalanceCmd.Flags().StringVar(&balanceOwner, "owner", "", "owner of balance account (omit to use owner from private key)") accountingBalanceCmd.Flags().StringVar(&balanceOwner, "owner", "", "owner of balance account (omit to use owner from private key)")
} }
@ -92,7 +93,7 @@ func prettyPrintDecimal(cmd *cobra.Command, decimal *accounting.Decimal) {
return return
} }
if viper.GetBool(verbose) { if viper.GetBool(commonflags.Verbose) {
cmd.Println("value:", decimal.Value()) cmd.Println("value:", decimal.Value())
cmd.Println("precision:", decimal.Precision()) cmd.Println("precision:", decimal.Precision())
} else { } else {

View file

@ -12,6 +12,7 @@ import (
"time" "time"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-node/pkg/network"
"github.com/nspcc-dev/neofs-sdk-go/bearer" "github.com/nspcc-dev/neofs-sdk-go/bearer"
"github.com/nspcc-dev/neofs-sdk-go/client" "github.com/nspcc-dev/neofs-sdk-go/client"
@ -28,7 +29,6 @@ const (
ownerFlag = "owner" ownerFlag = "owner"
outFlag = "out" outFlag = "out"
jsonFlag = "json" jsonFlag = "json"
rpcFlag = "rpc-endpoint"
) )
var createCmd = &cobra.Command{ var createCmd = &cobra.Command{
@ -37,7 +37,7 @@ var createCmd = &cobra.Command{
Long: `Create bearer token. Long: `Create bearer token.
All epoch flags can be specified relative to the current epoch with the +n syntax. All epoch flags can be specified relative to the current epoch with the +n syntax.
In this case --` + rpcFlag + ` flag should be specified and the epoch in bearer token In this case --` + commonflags.RPC + ` flag should be specified and the epoch in bearer token
is set to current epoch + n. is set to current epoch + n.
`, `,
RunE: createToken, RunE: createToken,
@ -51,7 +51,7 @@ func init() {
createCmd.Flags().StringP(ownerFlag, "o", "", "token owner") createCmd.Flags().StringP(ownerFlag, "o", "", "token owner")
createCmd.Flags().String(outFlag, "", "file to write token to") createCmd.Flags().String(outFlag, "", "file to write token to")
createCmd.Flags().Bool(jsonFlag, false, "output token in JSON") createCmd.Flags().Bool(jsonFlag, false, "output token in JSON")
createCmd.Flags().StringP(rpcFlag, "r", "", "rpc-endpoint") createCmd.Flags().StringP(commonflags.RPC, commonflags.RPCShorthand, commonflags.RPCDefault, commonflags.RPCUsage)
_ = cobra.MarkFlagFilename(createCmd.Flags(), eaclFlag) _ = cobra.MarkFlagFilename(createCmd.Flags(), eaclFlag)
@ -76,7 +76,7 @@ func createToken(cmd *cobra.Command, _ []string) error {
return err return err
} }
if iatRelative || expRelative || nvbRelative { if iatRelative || expRelative || nvbRelative {
endpoint, _ := cmd.Flags().GetString(rpcFlag) endpoint, _ := cmd.Flags().GetString(commonflags.RPC)
currEpoch, err := getCurrentEpoch(endpoint) currEpoch, err := getCurrentEpoch(endpoint)
if err != nil { if err != nil {
return err return err

View file

@ -13,6 +13,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/core/version" "github.com/nspcc-dev/neofs-node/pkg/core/version"
"github.com/nspcc-dev/neofs-sdk-go/acl" "github.com/nspcc-dev/neofs-sdk-go/acl"
"github.com/nspcc-dev/neofs-sdk-go/container" "github.com/nspcc-dev/neofs-sdk-go/container"
@ -102,7 +103,7 @@ var containerCmd = &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
// bind exactly that cmd's flags to // bind exactly that cmd's flags to
// the viper before execution // the viper before execution
bindCommonFlags(cmd) commonflags.Bind(cmd)
bindAPIFlags(cmd) bindAPIFlags(cmd)
}, },
} }
@ -467,7 +468,7 @@ Container ID in EACL table will be substituted with ID from the CLI.`,
} }
func initContainerListContainersCmd() { func initContainerListContainersCmd() {
initCommonFlags(listContainersCmd) commonflags.Init(listContainersCmd)
flags := listContainersCmd.Flags() flags := listContainersCmd.Flags()
@ -475,7 +476,7 @@ func initContainerListContainersCmd() {
} }
func initContainerCreateCmd() { func initContainerCreateCmd() {
initCommonFlags(createContainerCmd) commonflags.Init(createContainerCmd)
flags := createContainerCmd.Flags() flags := createContainerCmd.Flags()
@ -490,7 +491,7 @@ func initContainerCreateCmd() {
} }
func initContainerDeleteCmd() { func initContainerDeleteCmd() {
initCommonFlags(deleteContainerCmd) commonflags.Init(deleteContainerCmd)
flags := deleteContainerCmd.Flags() flags := deleteContainerCmd.Flags()
@ -499,7 +500,7 @@ func initContainerDeleteCmd() {
} }
func initContainerListObjectsCmd() { func initContainerListObjectsCmd() {
initCommonFlags(listContainerObjectsCmd) commonflags.Init(listContainerObjectsCmd)
flags := listContainerObjectsCmd.Flags() flags := listContainerObjectsCmd.Flags()
@ -507,7 +508,7 @@ func initContainerListObjectsCmd() {
} }
func initContainerInfoCmd() { func initContainerInfoCmd() {
initCommonFlags(getContainerInfoCmd) commonflags.Init(getContainerInfoCmd)
flags := getContainerInfoCmd.Flags() flags := getContainerInfoCmd.Flags()
@ -518,7 +519,7 @@ func initContainerInfoCmd() {
} }
func initContainerGetEACLCmd() { func initContainerGetEACLCmd() {
initCommonFlags(getExtendedACLCmd) commonflags.Init(getExtendedACLCmd)
flags := getExtendedACLCmd.Flags() flags := getExtendedACLCmd.Flags()
@ -528,7 +529,7 @@ func initContainerGetEACLCmd() {
} }
func initContainerSetEACLCmd() { func initContainerSetEACLCmd() {
initCommonFlags(setExtendedACLCmd) commonflags.Init(setExtendedACLCmd)
flags := setExtendedACLCmd.Flags() flags := setExtendedACLCmd.Flags()
flags.StringVar(&containerID, "cid", "", "container ID") flags.StringVar(&containerID, "cid", "", "container ID")

View file

@ -8,6 +8,7 @@ import (
"github.com/mr-tron/base58" "github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
ircontrol "github.com/nspcc-dev/neofs-node/pkg/services/control/ir" ircontrol "github.com/nspcc-dev/neofs-node/pkg/services/control/ir"
ircontrolsrv "github.com/nspcc-dev/neofs-node/pkg/services/control/ir/server" ircontrolsrv "github.com/nspcc-dev/neofs-node/pkg/services/control/ir/server"
@ -26,8 +27,8 @@ var controlCmd = &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
ff := cmd.Flags() ff := cmd.Flags()
_ = viper.BindPFlag(walletPath, ff.Lookup(walletPath)) _ = viper.BindPFlag(commonflags.WalletPath, ff.Lookup(commonflags.WalletPath))
_ = viper.BindPFlag(address, ff.Lookup(address)) _ = viper.BindPFlag(commonflags.Account, ff.Lookup(commonflags.Account))
_ = viper.BindPFlag(controlRPC, ff.Lookup(controlRPC)) _ = viper.BindPFlag(controlRPC, ff.Lookup(controlRPC))
}, },
} }
@ -110,7 +111,7 @@ var (
) )
func initControlHealthCheckCmd() { func initControlHealthCheckCmd() {
initCommonFlagsWithoutRPC(healthCheckCmd) commonflags.InitWithoutRPC(healthCheckCmd)
flags := healthCheckCmd.Flags() flags := healthCheckCmd.Flags()
@ -119,7 +120,7 @@ func initControlHealthCheckCmd() {
} }
func initControlSetShardModeCmd() { func initControlSetShardModeCmd() {
initCommonFlagsWithoutRPC(setShardModeCmd) commonflags.InitWithoutRPC(setShardModeCmd)
flags := setShardModeCmd.Flags() flags := setShardModeCmd.Flags()
@ -136,7 +137,7 @@ func initControlSetShardModeCmd() {
} }
func initControlShardsListCmd() { func initControlShardsListCmd() {
initCommonFlagsWithoutRPC(listShardsCmd) commonflags.InitWithoutRPC(listShardsCmd)
flags := listShardsCmd.Flags() flags := listShardsCmd.Flags()
@ -144,7 +145,7 @@ func initControlShardsListCmd() {
} }
func initControlSetNetmapStatusCmd() { func initControlSetNetmapStatusCmd() {
initCommonFlagsWithoutRPC(setNetmapStatusCmd) commonflags.InitWithoutRPC(setNetmapStatusCmd)
flags := setNetmapStatusCmd.Flags() flags := setNetmapStatusCmd.Flags()
@ -161,7 +162,7 @@ func initControlSetNetmapStatusCmd() {
} }
func initControlDropObjectsCmd() { func initControlDropObjectsCmd() {
initCommonFlagsWithoutRPC(dropObjectsCmd) commonflags.InitWithoutRPC(dropObjectsCmd)
flags := dropObjectsCmd.Flags() flags := dropObjectsCmd.Flags()
@ -173,7 +174,7 @@ func initControlDropObjectsCmd() {
} }
func initControlSnapshotCmd() { func initControlSnapshotCmd() {
initCommonFlagsWithoutRPC(snapshotCmd) commonflags.InitWithoutRPC(snapshotCmd)
flags := snapshotCmd.Flags() flags := snapshotCmd.Flags()

View file

@ -3,6 +3,7 @@ package cmd
import ( import (
"github.com/mr-tron/base58" "github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client" "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server" controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -58,7 +59,7 @@ func dumpShard(cmd *cobra.Command, _ []string) {
} }
func initControlDumpShardCmd() { func initControlDumpShardCmd() {
initCommonFlagsWithoutRPC(dumpShardCmd) commonflags.InitWithoutRPC(dumpShardCmd)
flags := dumpShardCmd.Flags() flags := dumpShardCmd.Flags()
flags.String(controlRPC, controlRPCDefault, controlRPCUsage) flags.String(controlRPC, controlRPCDefault, controlRPCUsage)

View file

@ -10,6 +10,7 @@ import (
"github.com/nspcc-dev/neo-go/cli/input" "github.com/nspcc-dev/neo-go/cli/input"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"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-node/cmd/neofs-cli/internal/key"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -66,16 +67,16 @@ func Test_getKey(t *testing.T) {
in.WriteString("pass\r") in.WriteString("pass\r")
checkKey(t, wallPath, acc2.PrivateKey()) // default account checkKey(t, wallPath, acc2.PrivateKey()) // default account
viper.Set(address, acc1.Address) viper.Set(commonflags.Account, acc1.Address)
in.WriteString("pass\r") in.WriteString("pass\r")
checkKey(t, wallPath, acc1.PrivateKey()) checkKey(t, wallPath, acc1.PrivateKey())
viper.Set(address, "not an address") viper.Set(commonflags.Account, "not an address")
checkKeyError(t, wallPath, key.ErrInvalidAddress) checkKeyError(t, wallPath, key.ErrInvalidAddress)
acc, err := wallet.NewAccount() acc, err := wallet.NewAccount()
require.NoError(t, err) require.NoError(t, err)
viper.Set(address, acc.Address) viper.Set(commonflags.Account, acc.Address)
checkKeyError(t, wallPath, key.ErrInvalidAddress) checkKeyError(t, wallPath, key.ErrInvalidAddress)
}) })
@ -108,7 +109,7 @@ func Test_getKey(t *testing.T) {
}) })
t.Run("generate", func(t *testing.T) { t.Run("generate", func(t *testing.T) {
viper.Set(generateKey, true) viper.Set(commonflags.GenerateKey, true)
actual, err := getKey() actual, err := getKey()
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, actual) require.NotNil(t, actual)
@ -119,13 +120,13 @@ func Test_getKey(t *testing.T) {
} }
func checkKeyError(t *testing.T, desc string, err error) { func checkKeyError(t *testing.T, desc string, err error) {
viper.Set(walletPath, desc) viper.Set(commonflags.WalletPath, desc)
_, actualErr := getKey() _, actualErr := getKey()
require.ErrorIs(t, actualErr, err) require.ErrorIs(t, actualErr, err)
} }
func checkKey(t *testing.T, desc string, expected *keys.PrivateKey) { func checkKey(t *testing.T, desc string, expected *keys.PrivateKey) {
viper.Set(walletPath, desc) viper.Set(commonflags.WalletPath, desc)
actual, err := getKey() actual, err := getKey()
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, &expected.PrivateKey, actual) require.Equal(t, &expected.PrivateKey, actual)

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object" objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id" cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
"github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/object"
@ -61,5 +62,5 @@ var cmdObjectLock = &cobra.Command{
} }
func initCommandObjectLock() { func initCommandObjectLock() {
initCommonFlags(cmdObjectLock) commonflags.Init(cmdObjectLock)
} }

View file

@ -7,6 +7,7 @@ import (
"github.com/mr-tron/base58" "github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/config/netmode"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
"github.com/nspcc-dev/neofs-sdk-go/netmap" "github.com/nspcc-dev/neofs-sdk-go/netmap"
@ -27,7 +28,7 @@ var netmapCmd = &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
// bind exactly that cmd's flags to // bind exactly that cmd's flags to
// the viper before execution // the viper before execution
bindCommonFlags(cmd) commonflags.Bind(cmd)
bindAPIFlags(cmd) bindAPIFlags(cmd)
}, },
} }
@ -42,10 +43,10 @@ func init() {
rootCmd.AddCommand(netmapCmd) rootCmd.AddCommand(netmapCmd)
netmapCmd.AddCommand(netmapChildCommands...) netmapCmd.AddCommand(netmapChildCommands...)
initCommonFlags(getEpochCmd) commonflags.Init(getEpochCmd)
initCommonFlags(netInfoCmd) commonflags.Init(netInfoCmd)
initCommonFlags(localNodeInfoCmd) commonflags.Init(localNodeInfoCmd)
localNodeInfoCmd.Flags().BoolVar(&nodeInfoJSON, "json", false, "print node info in JSON format") localNodeInfoCmd.Flags().BoolVar(&nodeInfoJSON, "json", false, "print node info in JSON format")
for _, netmapCommand := range netmapChildCommands { for _, netmapCommand := range netmapChildCommands {

View file

@ -18,6 +18,7 @@ import (
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session" sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session"
"github.com/nspcc-dev/neofs-sdk-go/bearer" "github.com/nspcc-dev/neofs-sdk-go/bearer"
"github.com/nspcc-dev/neofs-sdk-go/checksum" "github.com/nspcc-dev/neofs-sdk-go/checksum"
@ -55,7 +56,7 @@ var (
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
// bind exactly that cmd's flags to // bind exactly that cmd's flags to
// the viper before execution // the viper before execution
bindCommonFlags(cmd) commonflags.Bind(cmd)
bindAPIFlags(cmd) bindAPIFlags(cmd)
}, },
} }
@ -135,7 +136,7 @@ const noProgressFlag = "no-progress"
var putExpiredOn uint64 var putExpiredOn uint64
func initObjectPutCmd() { func initObjectPutCmd() {
initCommonFlags(objectPutCmd) commonflags.Init(objectPutCmd)
flags := objectPutCmd.Flags() flags := objectPutCmd.Flags()
@ -156,7 +157,7 @@ func initObjectPutCmd() {
} }
func initObjectDeleteCmd() { func initObjectDeleteCmd() {
initCommonFlags(objectDelCmd) commonflags.Init(objectDelCmd)
flags := objectDelCmd.Flags() flags := objectDelCmd.Flags()
@ -168,7 +169,7 @@ func initObjectDeleteCmd() {
} }
func initObjectGetCmd() { func initObjectGetCmd() {
initCommonFlags(objectGetCmd) commonflags.Init(objectGetCmd)
flags := objectGetCmd.Flags() flags := objectGetCmd.Flags()
@ -185,7 +186,7 @@ func initObjectGetCmd() {
} }
func initObjectSearchCmd() { func initObjectSearchCmd() {
initCommonFlags(objectSearchCmd) commonflags.Init(objectSearchCmd)
flags := objectSearchCmd.Flags() flags := objectSearchCmd.Flags()
@ -201,7 +202,7 @@ func initObjectSearchCmd() {
} }
func initObjectHeadCmd() { func initObjectHeadCmd() {
initCommonFlags(objectHeadCmd) commonflags.Init(objectHeadCmd)
flags := objectHeadCmd.Flags() flags := objectHeadCmd.Flags()
@ -219,7 +220,7 @@ func initObjectHeadCmd() {
} }
func initObjectHashCmd() { func initObjectHashCmd() {
initCommonFlags(objectHashCmd) commonflags.Init(objectHashCmd)
flags := objectHashCmd.Flags() flags := objectHashCmd.Flags()
@ -235,7 +236,7 @@ func initObjectHashCmd() {
} }
func initObjectRangeCmd() { func initObjectRangeCmd() {
initCommonFlags(objectRangeCmd) commonflags.Init(objectRangeCmd)
flags := objectRangeCmd.Flags() flags := objectRangeCmd.Flags()

View file

@ -3,6 +3,7 @@ package cmd
import ( import (
"github.com/mr-tron/base58" "github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client" "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server" controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -58,7 +59,7 @@ func restoreShard(cmd *cobra.Command, _ []string) {
} }
func initControlRestoreShardCmd() { func initControlRestoreShardCmd() {
initCommonFlagsWithoutRPC(restoreShardCmd) commonflags.InitWithoutRPC(restoreShardCmd)
flags := restoreShardCmd.Flags() flags := restoreShardCmd.Flags()
flags.String(controlRPC, controlRPCDefault, controlRPCUsage) flags.String(controlRPC, controlRPCDefault, controlRPCUsage)

View file

@ -11,6 +11,7 @@ import (
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"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-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/acl" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/acl"
bearerCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/bearer" bearerCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/bearer"
@ -37,33 +38,6 @@ var (
) )
const ( const (
// Common CLI flag keys, shorthands, default
// values and their usage descriptions.
generateKey = "generate-key"
generateKeyShorthand = "g"
generateKeyDefault = false
generateKeyUsage = "generate new private key"
walletPath = "wallet"
walletPathShorthand = "w"
walletPathDefault = ""
walletPathUsage = "WIF (NEP-2) string or path to the wallet or binary key"
address = "address"
addressShorthand = ""
addressDefault = ""
addressUsage = "address of wallet account"
rpc = "rpc-endpoint"
rpcShorthand = "r"
rpcDefault = ""
rpcUsage = "remote node address (as 'multiaddr' or '<host>:<port>')"
verbose = "verbose"
verboseShorthand = "v"
verboseDefault = false
verboseUsage = "verbose output"
ttl = "ttl" ttl = "ttl"
ttlShorthand = "" ttlShorthand = ""
ttlDefault = 2 ttlDefault = 2
@ -111,9 +85,10 @@ func init() {
// Cobra supports persistent flags, which, if defined here, // Cobra supports persistent flags, which, if defined here,
// will be global for your application. // will be global for your application.
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/neofs-cli/config.yaml)") rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/neofs-cli/config.yaml)")
rootCmd.PersistentFlags().BoolP(verbose, verboseShorthand, verboseDefault, verboseUsage) rootCmd.PersistentFlags().BoolP(commonflags.Verbose, commonflags.VerboseShorthand,
false, commonflags.VerboseUsage)
_ = viper.BindPFlag(verbose, rootCmd.PersistentFlags().Lookup(verbose)) _ = viper.BindPFlag(commonflags.Verbose, rootCmd.PersistentFlags().Lookup(commonflags.Verbose))
// Cobra also supports local flags, which will only run // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
@ -167,7 +142,7 @@ func initConfig() {
// getKey returns private key that was provided in global arguments. // getKey returns private key that was provided in global arguments.
func getKey() (*ecdsa.PrivateKey, error) { func getKey() (*ecdsa.PrivateKey, error) {
if viper.GetBool(generateKey) { if viper.GetBool(commonflags.GenerateKey) {
priv, err := keys.NewPrivateKey() priv, err := keys.NewPrivateKey()
if err != nil { if err != nil {
return nil, errCantGenerateKey return nil, errCantGenerateKey
@ -178,7 +153,7 @@ func getKey() (*ecdsa.PrivateKey, error) {
} }
func getKeyNoGenerate() (*ecdsa.PrivateKey, error) { func getKeyNoGenerate() (*ecdsa.PrivateKey, error) {
return key.Get(viper.GetString(walletPath), viper.GetString(address)) return key.Get(viper.GetString(commonflags.WalletPath), viper.GetString(commonflags.Account))
} }
// getEndpointAddress returns network address structure that stores multiaddr // getEndpointAddress returns network address structure that stores multiaddr
@ -229,7 +204,7 @@ func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) {
// getSDKClient calls getSDKGClientFlag with "rpc-endpoint" flag. // getSDKClient calls getSDKGClientFlag with "rpc-endpoint" flag.
func getSDKClient(key *ecdsa.PrivateKey) (*client.Client, error) { func getSDKClient(key *ecdsa.PrivateKey) (*client.Client, error) {
return getSDKClientFlag(key, rpc) return getSDKClientFlag(key, commonflags.RPC)
} }
// getSDKClientFlag returns NeoFS API client connection to the network address // getSDKClientFlag returns NeoFS API client connection to the network address
@ -263,7 +238,7 @@ func ownerFromString(s string) (*owner.ID, error) {
} }
func printVerbose(format string, a ...interface{}) { func printVerbose(format string, a ...interface{}) {
if viper.GetBool(verbose) { if viper.GetBool(commonflags.Verbose) {
fmt.Printf(format+"\n", a...) fmt.Printf(format+"\n", a...)
} }
} }
@ -287,31 +262,6 @@ func parseXHeaders() []*session.XHeader {
return xs return xs
} }
// add common flags to the command:
// - key;
// - wallet;
// - WIF;
// - address;
// - RPC;
func initCommonFlags(cmd *cobra.Command) {
ff := cmd.Flags()
ff.BoolP(generateKey, generateKeyShorthand, generateKeyDefault, generateKeyUsage)
ff.StringP(walletPath, walletPathShorthand, walletPathDefault, walletPathUsage)
ff.StringP(address, addressShorthand, addressDefault, addressUsage)
ff.StringP(rpc, rpcShorthand, rpcDefault, rpcUsage)
}
// bind common command flags to the viper
func bindCommonFlags(cmd *cobra.Command) {
ff := cmd.Flags()
_ = viper.BindPFlag(generateKey, ff.Lookup(generateKey))
_ = viper.BindPFlag(walletPath, ff.Lookup(walletPath))
_ = viper.BindPFlag(address, ff.Lookup(address))
_ = viper.BindPFlag(rpc, ff.Lookup(rpc))
}
func bindAPIFlags(cmd *cobra.Command) { func bindAPIFlags(cmd *cobra.Command) {
ff := cmd.Flags() ff := cmd.Flags()

View file

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"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-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-node/pkg/network"
"github.com/nspcc-dev/neofs-sdk-go/client" "github.com/nspcc-dev/neofs-sdk-go/client"
@ -15,11 +16,8 @@ import (
const ( const (
lifetimeFlag = "lifetime" lifetimeFlag = "lifetime"
walletFlag = "wallet"
accountFlag = "address"
outFlag = "out" outFlag = "out"
jsonFlag = "json" jsonFlag = "json"
rpcFlag = "rpc-endpoint"
) )
const defaultLifetime = 10 const defaultLifetime = 10
@ -32,28 +30,28 @@ var createCmd = &cobra.Command{
func init() { func init() {
createCmd.Flags().Uint64P(lifetimeFlag, "l", defaultLifetime, "number of epochs for token to stay valid") createCmd.Flags().Uint64P(lifetimeFlag, "l", defaultLifetime, "number of epochs for token to stay valid")
createCmd.Flags().StringP(walletFlag, "w", "", "path to the wallet") createCmd.Flags().StringP(commonflags.WalletPath, commonflags.WalletPathShorthand, commonflags.WalletPathDefault, commonflags.WalletPathUsage)
createCmd.Flags().StringP(accountFlag, "a", "", "account address") createCmd.Flags().StringP(commonflags.Account, commonflags.AccountShorthand, commonflags.AccountDefault, commonflags.AccountUsage)
createCmd.Flags().String(outFlag, "", "file to write session token to") createCmd.Flags().String(outFlag, "", "file to write session token to")
createCmd.Flags().Bool(jsonFlag, false, "output token in JSON") createCmd.Flags().Bool(jsonFlag, false, "output token in JSON")
createCmd.Flags().StringP(rpcFlag, "r", "", "rpc-endpoint") createCmd.Flags().StringP(commonflags.RPC, commonflags.RPCShorthand, commonflags.RPCDefault, commonflags.RPCUsage)
_ = cobra.MarkFlagRequired(createCmd.Flags(), lifetimeFlag) _ = cobra.MarkFlagRequired(createCmd.Flags(), lifetimeFlag)
_ = cobra.MarkFlagRequired(createCmd.Flags(), walletFlag) _ = cobra.MarkFlagRequired(createCmd.Flags(), commonflags.WalletPath)
_ = cobra.MarkFlagRequired(createCmd.Flags(), outFlag) _ = cobra.MarkFlagRequired(createCmd.Flags(), outFlag)
_ = cobra.MarkFlagRequired(createCmd.Flags(), rpcFlag) _ = cobra.MarkFlagRequired(createCmd.Flags(), commonflags.RPC)
} }
func createSession(cmd *cobra.Command, _ []string) error { func createSession(cmd *cobra.Command, _ []string) error {
walletPath, _ := cmd.Flags().GetString(walletFlag) walletPath, _ := cmd.Flags().GetString(commonflags.WalletPath)
accPath, _ := cmd.Flags().GetString(accountFlag) accPath, _ := cmd.Flags().GetString(commonflags.Account)
privKey, err := key.Get(walletPath, accPath) privKey, err := key.Get(walletPath, accPath)
if err != nil { if err != nil {
return err return err
} }
var netAddr network.Address var netAddr network.Address
addrStr, _ := cmd.Flags().GetString(rpcFlag) addrStr, _ := cmd.Flags().GetString(commonflags.RPC)
if err := netAddr.FromString(addrStr); err != nil { if err := netAddr.FromString(addrStr); err != nil {
return err return err
} }

View file

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/storagegroup" "github.com/nspcc-dev/neofs-node/pkg/services/object_manager/storagegroup"
"github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/object"
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address" addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
@ -24,7 +25,7 @@ var storagegroupCmd = &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
// bind exactly that cmd's flags to // bind exactly that cmd's flags to
// the viper before execution // the viper before execution
bindCommonFlags(cmd) commonflags.Bind(cmd)
bindAPIFlags(cmd) bindAPIFlags(cmd)
}, },
} }
@ -68,7 +69,7 @@ var (
) )
func initSGPutCmd() { func initSGPutCmd() {
initCommonFlags(sgPutCmd) commonflags.Init(sgPutCmd)
flags := sgPutCmd.Flags() flags := sgPutCmd.Flags()
@ -80,7 +81,7 @@ func initSGPutCmd() {
} }
func initSGGetCmd() { func initSGGetCmd() {
initCommonFlags(sgGetCmd) commonflags.Init(sgGetCmd)
flags := sgGetCmd.Flags() flags := sgGetCmd.Flags()
@ -92,14 +93,14 @@ func initSGGetCmd() {
} }
func initSGListCmd() { func initSGListCmd() {
initCommonFlags(sgListCmd) commonflags.Init(sgListCmd)
sgListCmd.Flags().String("cid", "", "Container ID") sgListCmd.Flags().String("cid", "", "Container ID")
_ = sgListCmd.MarkFlagRequired("cid") _ = sgListCmd.MarkFlagRequired("cid")
} }
func initSGDeleteCmd() { func initSGDeleteCmd() {
initCommonFlags(sgDelCmd) commonflags.Init(sgDelCmd)
flags := sgDelCmd.Flags() flags := sgDelCmd.Flags()

View file

@ -10,6 +10,7 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/pkg/util/keyer" "github.com/nspcc-dev/neofs-node/pkg/util/keyer"
locodedb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db" locodedb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db"
airportsdb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db/airports" airportsdb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db/airports"
@ -30,9 +31,9 @@ var (
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
flags := cmd.Flags() flags := cmd.Flags()
_ = viper.BindPFlag(generateKey, flags.Lookup(generateKey)) _ = viper.BindPFlag(commonflags.GenerateKey, flags.Lookup(commonflags.GenerateKey))
_ = viper.BindPFlag(walletPath, flags.Lookup(walletPath)) _ = viper.BindPFlag(commonflags.WalletPath, flags.Lookup(commonflags.WalletPath))
_ = viper.BindPFlag(address, flags.Lookup(address)) _ = viper.BindPFlag(commonflags.Account, flags.Lookup(commonflags.Account))
}, },
} }
@ -185,16 +186,8 @@ func initUtilKeyerCmd() {
keyerCmd.Flags().BoolP("multisig", "m", false, "calculate multisig address from public keys") keyerCmd.Flags().BoolP("multisig", "m", false, "calculate multisig address from public keys")
} }
func initCommonFlagsWithoutRPC(cmd *cobra.Command) {
flags := cmd.Flags()
flags.BoolP(generateKey, generateKeyShorthand, generateKeyDefault, generateKeyUsage)
flags.StringP(walletPath, walletPathShorthand, walletPathDefault, walletPathUsage)
flags.StringP(address, addressShorthand, addressDefault, addressUsage)
}
func initUtilSignBearerCmd() { func initUtilSignBearerCmd() {
initCommonFlagsWithoutRPC(signBearerCmd) commonflags.InitWithoutRPC(signBearerCmd)
flags := signBearerCmd.Flags() flags := signBearerCmd.Flags()
@ -207,7 +200,7 @@ func initUtilSignBearerCmd() {
} }
func initUtilSignSessionCmd() { func initUtilSignSessionCmd() {
initCommonFlagsWithoutRPC(signSessionCmd) commonflags.InitWithoutRPC(signSessionCmd)
flags := signSessionCmd.Flags() flags := signSessionCmd.Flags()