From cbc4ca800d5ebe62bc08e64350dad26809745192 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 18 May 2022 12:22:02 +0300 Subject: [PATCH] [#1379] neofs-cli: Move common flags to a separate package Signed-off-by: Evgenii Stratonikov --- cmd/neofs-cli/internal/commonflags/flags.go | 65 ++++++++++++++++++++ cmd/neofs-cli/modules/accounting.go | 15 ++--- cmd/neofs-cli/modules/bearer/create.go | 8 +-- cmd/neofs-cli/modules/container.go | 17 +++--- cmd/neofs-cli/modules/control.go | 17 +++--- cmd/neofs-cli/modules/dump.go | 3 +- cmd/neofs-cli/modules/key_test.go | 13 ++-- cmd/neofs-cli/modules/lock.go | 3 +- cmd/neofs-cli/modules/netmap.go | 9 +-- cmd/neofs-cli/modules/object.go | 17 +++--- cmd/neofs-cli/modules/restore.go | 3 +- cmd/neofs-cli/modules/root.go | 66 +++------------------ cmd/neofs-cli/modules/session/create.go | 20 +++---- cmd/neofs-cli/modules/storagegroup.go | 11 ++-- cmd/neofs-cli/modules/util.go | 19 ++---- 15 files changed, 151 insertions(+), 135 deletions(-) create mode 100644 cmd/neofs-cli/internal/commonflags/flags.go diff --git a/cmd/neofs-cli/internal/commonflags/flags.go b/cmd/neofs-cli/internal/commonflags/flags.go new file mode 100644 index 000000000..5d8196506 --- /dev/null +++ b/cmd/neofs-cli/internal/commonflags/flags.go @@ -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 ':')" + + 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)) +} diff --git a/cmd/neofs-cli/modules/accounting.go b/cmd/neofs-cli/modules/accounting.go index 81a195b5e..aa2e1847e 100644 --- a/cmd/neofs-cli/modules/accounting.go +++ b/cmd/neofs-cli/modules/accounting.go @@ -5,6 +5,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" 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-sdk-go/accounting" "github.com/nspcc-dev/neofs-sdk-go/owner" @@ -24,9 +25,9 @@ var accountingCmd = &cobra.Command{ PersistentPreRun: func(cmd *cobra.Command, args []string) { flags := cmd.Flags() - _ = viper.BindPFlag(walletPath, flags.Lookup(walletPath)) - _ = viper.BindPFlag(address, flags.Lookup(address)) - _ = viper.BindPFlag(rpc, flags.Lookup(rpc)) + _ = viper.BindPFlag(commonflags.WalletPath, flags.Lookup(commonflags.WalletPath)) + _ = viper.BindPFlag(commonflags.Account, flags.Lookup(commonflags.Account)) + _ = viper.BindPFlag(commonflags.RPC, flags.Lookup(commonflags.RPC)) }, } @@ -63,9 +64,9 @@ var accountingBalanceCmd = &cobra.Command{ func initAccountingBalanceCmd() { ff := accountingBalanceCmd.Flags() - ff.StringP(walletPath, walletPathShorthand, walletPathDefault, walletPathUsage) - ff.StringP(address, addressShorthand, addressDefault, addressUsage) - ff.StringP(rpc, rpcShorthand, rpcDefault, rpcUsage) + ff.StringP(commonflags.WalletPath, commonflags.WalletPathShorthand, commonflags.WalletPathDefault, commonflags.WalletPathUsage) + ff.StringP(commonflags.Account, commonflags.AccountShorthand, commonflags.AccountDefault, commonflags.AccountUsage) + 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)") } @@ -92,7 +93,7 @@ func prettyPrintDecimal(cmd *cobra.Command, decimal *accounting.Decimal) { return } - if viper.GetBool(verbose) { + if viper.GetBool(commonflags.Verbose) { cmd.Println("value:", decimal.Value()) cmd.Println("precision:", decimal.Precision()) } else { diff --git a/cmd/neofs-cli/modules/bearer/create.go b/cmd/neofs-cli/modules/bearer/create.go index f9df51d19..2bdec6f10 100644 --- a/cmd/neofs-cli/modules/bearer/create.go +++ b/cmd/neofs-cli/modules/bearer/create.go @@ -12,6 +12,7 @@ import ( "time" 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-sdk-go/bearer" "github.com/nspcc-dev/neofs-sdk-go/client" @@ -28,7 +29,6 @@ const ( ownerFlag = "owner" outFlag = "out" jsonFlag = "json" - rpcFlag = "rpc-endpoint" ) var createCmd = &cobra.Command{ @@ -37,7 +37,7 @@ var createCmd = &cobra.Command{ Long: `Create bearer token. 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. `, RunE: createToken, @@ -51,7 +51,7 @@ func init() { createCmd.Flags().StringP(ownerFlag, "o", "", "token owner") createCmd.Flags().String(outFlag, "", "file to write token to") 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) @@ -76,7 +76,7 @@ func createToken(cmd *cobra.Command, _ []string) error { return err } if iatRelative || expRelative || nvbRelative { - endpoint, _ := cmd.Flags().GetString(rpcFlag) + endpoint, _ := cmd.Flags().GetString(commonflags.RPC) currEpoch, err := getCurrentEpoch(endpoint) if err != nil { return err diff --git a/cmd/neofs-cli/modules/container.go b/cmd/neofs-cli/modules/container.go index 6b5ea3b57..2c95eb766 100644 --- a/cmd/neofs-cli/modules/container.go +++ b/cmd/neofs-cli/modules/container.go @@ -13,6 +13,7 @@ import ( "github.com/google/uuid" "github.com/nspcc-dev/neofs-api-go/v2/refs" 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-sdk-go/acl" "github.com/nspcc-dev/neofs-sdk-go/container" @@ -102,7 +103,7 @@ var containerCmd = &cobra.Command{ PersistentPreRun: func(cmd *cobra.Command, args []string) { // bind exactly that cmd's flags to // the viper before execution - bindCommonFlags(cmd) + commonflags.Bind(cmd) bindAPIFlags(cmd) }, } @@ -467,7 +468,7 @@ Container ID in EACL table will be substituted with ID from the CLI.`, } func initContainerListContainersCmd() { - initCommonFlags(listContainersCmd) + commonflags.Init(listContainersCmd) flags := listContainersCmd.Flags() @@ -475,7 +476,7 @@ func initContainerListContainersCmd() { } func initContainerCreateCmd() { - initCommonFlags(createContainerCmd) + commonflags.Init(createContainerCmd) flags := createContainerCmd.Flags() @@ -490,7 +491,7 @@ func initContainerCreateCmd() { } func initContainerDeleteCmd() { - initCommonFlags(deleteContainerCmd) + commonflags.Init(deleteContainerCmd) flags := deleteContainerCmd.Flags() @@ -499,7 +500,7 @@ func initContainerDeleteCmd() { } func initContainerListObjectsCmd() { - initCommonFlags(listContainerObjectsCmd) + commonflags.Init(listContainerObjectsCmd) flags := listContainerObjectsCmd.Flags() @@ -507,7 +508,7 @@ func initContainerListObjectsCmd() { } func initContainerInfoCmd() { - initCommonFlags(getContainerInfoCmd) + commonflags.Init(getContainerInfoCmd) flags := getContainerInfoCmd.Flags() @@ -518,7 +519,7 @@ func initContainerInfoCmd() { } func initContainerGetEACLCmd() { - initCommonFlags(getExtendedACLCmd) + commonflags.Init(getExtendedACLCmd) flags := getExtendedACLCmd.Flags() @@ -528,7 +529,7 @@ func initContainerGetEACLCmd() { } func initContainerSetEACLCmd() { - initCommonFlags(setExtendedACLCmd) + commonflags.Init(setExtendedACLCmd) flags := setExtendedACLCmd.Flags() flags.StringVar(&containerID, "cid", "", "container ID") diff --git a/cmd/neofs-cli/modules/control.go b/cmd/neofs-cli/modules/control.go index 15234a789..d0ba89808 100644 --- a/cmd/neofs-cli/modules/control.go +++ b/cmd/neofs-cli/modules/control.go @@ -8,6 +8,7 @@ import ( "github.com/mr-tron/base58" "github.com/nspcc-dev/neofs-api-go/v2/refs" 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" ircontrol "github.com/nspcc-dev/neofs-node/pkg/services/control/ir" 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) { ff := cmd.Flags() - _ = viper.BindPFlag(walletPath, ff.Lookup(walletPath)) - _ = viper.BindPFlag(address, ff.Lookup(address)) + _ = viper.BindPFlag(commonflags.WalletPath, ff.Lookup(commonflags.WalletPath)) + _ = viper.BindPFlag(commonflags.Account, ff.Lookup(commonflags.Account)) _ = viper.BindPFlag(controlRPC, ff.Lookup(controlRPC)) }, } @@ -110,7 +111,7 @@ var ( ) func initControlHealthCheckCmd() { - initCommonFlagsWithoutRPC(healthCheckCmd) + commonflags.InitWithoutRPC(healthCheckCmd) flags := healthCheckCmd.Flags() @@ -119,7 +120,7 @@ func initControlHealthCheckCmd() { } func initControlSetShardModeCmd() { - initCommonFlagsWithoutRPC(setShardModeCmd) + commonflags.InitWithoutRPC(setShardModeCmd) flags := setShardModeCmd.Flags() @@ -136,7 +137,7 @@ func initControlSetShardModeCmd() { } func initControlShardsListCmd() { - initCommonFlagsWithoutRPC(listShardsCmd) + commonflags.InitWithoutRPC(listShardsCmd) flags := listShardsCmd.Flags() @@ -144,7 +145,7 @@ func initControlShardsListCmd() { } func initControlSetNetmapStatusCmd() { - initCommonFlagsWithoutRPC(setNetmapStatusCmd) + commonflags.InitWithoutRPC(setNetmapStatusCmd) flags := setNetmapStatusCmd.Flags() @@ -161,7 +162,7 @@ func initControlSetNetmapStatusCmd() { } func initControlDropObjectsCmd() { - initCommonFlagsWithoutRPC(dropObjectsCmd) + commonflags.InitWithoutRPC(dropObjectsCmd) flags := dropObjectsCmd.Flags() @@ -173,7 +174,7 @@ func initControlDropObjectsCmd() { } func initControlSnapshotCmd() { - initCommonFlagsWithoutRPC(snapshotCmd) + commonflags.InitWithoutRPC(snapshotCmd) flags := snapshotCmd.Flags() diff --git a/cmd/neofs-cli/modules/dump.go b/cmd/neofs-cli/modules/dump.go index 94bc170d3..5245147ec 100644 --- a/cmd/neofs-cli/modules/dump.go +++ b/cmd/neofs-cli/modules/dump.go @@ -3,6 +3,7 @@ package cmd import ( "github.com/mr-tron/base58" "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" controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server" "github.com/spf13/cobra" @@ -58,7 +59,7 @@ func dumpShard(cmd *cobra.Command, _ []string) { } func initControlDumpShardCmd() { - initCommonFlagsWithoutRPC(dumpShardCmd) + commonflags.InitWithoutRPC(dumpShardCmd) flags := dumpShardCmd.Flags() flags.String(controlRPC, controlRPCDefault, controlRPCUsage) diff --git a/cmd/neofs-cli/modules/key_test.go b/cmd/neofs-cli/modules/key_test.go index 39608d472..15307c271 100644 --- a/cmd/neofs-cli/modules/key_test.go +++ b/cmd/neofs-cli/modules/key_test.go @@ -10,6 +10,7 @@ import ( "github.com/nspcc-dev/neo-go/cli/input" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "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/spf13/viper" "github.com/stretchr/testify/require" @@ -66,16 +67,16 @@ func Test_getKey(t *testing.T) { in.WriteString("pass\r") checkKey(t, wallPath, acc2.PrivateKey()) // default account - viper.Set(address, acc1.Address) + viper.Set(commonflags.Account, acc1.Address) in.WriteString("pass\r") checkKey(t, wallPath, acc1.PrivateKey()) - viper.Set(address, "not an address") + viper.Set(commonflags.Account, "not an address") checkKeyError(t, wallPath, key.ErrInvalidAddress) acc, err := wallet.NewAccount() require.NoError(t, err) - viper.Set(address, acc.Address) + viper.Set(commonflags.Account, acc.Address) checkKeyError(t, wallPath, key.ErrInvalidAddress) }) @@ -108,7 +109,7 @@ func Test_getKey(t *testing.T) { }) t.Run("generate", func(t *testing.T) { - viper.Set(generateKey, true) + viper.Set(commonflags.GenerateKey, true) actual, err := getKey() require.NoError(t, err) require.NotNil(t, actual) @@ -119,13 +120,13 @@ func Test_getKey(t *testing.T) { } func checkKeyError(t *testing.T, desc string, err error) { - viper.Set(walletPath, desc) + viper.Set(commonflags.WalletPath, desc) _, actualErr := getKey() require.ErrorIs(t, actualErr, err) } func checkKey(t *testing.T, desc string, expected *keys.PrivateKey) { - viper.Set(walletPath, desc) + viper.Set(commonflags.WalletPath, desc) actual, err := getKey() require.NoError(t, err) require.Equal(t, &expected.PrivateKey, actual) diff --git a/cmd/neofs-cli/modules/lock.go b/cmd/neofs-cli/modules/lock.go index 40b5dc1e6..78cb40469 100644 --- a/cmd/neofs-cli/modules/lock.go +++ b/cmd/neofs-cli/modules/lock.go @@ -4,6 +4,7 @@ import ( "fmt" 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" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" "github.com/nspcc-dev/neofs-sdk-go/object" @@ -61,5 +62,5 @@ var cmdObjectLock = &cobra.Command{ } func initCommandObjectLock() { - initCommonFlags(cmdObjectLock) + commonflags.Init(cmdObjectLock) } diff --git a/cmd/neofs-cli/modules/netmap.go b/cmd/neofs-cli/modules/netmap.go index 056394ca4..3b3700555 100644 --- a/cmd/neofs-cli/modules/netmap.go +++ b/cmd/neofs-cli/modules/netmap.go @@ -7,6 +7,7 @@ import ( "github.com/mr-tron/base58" "github.com/nspcc-dev/neo-go/pkg/config/netmode" 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" "github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-sdk-go/netmap" @@ -27,7 +28,7 @@ var netmapCmd = &cobra.Command{ PersistentPreRun: func(cmd *cobra.Command, args []string) { // bind exactly that cmd's flags to // the viper before execution - bindCommonFlags(cmd) + commonflags.Bind(cmd) bindAPIFlags(cmd) }, } @@ -42,10 +43,10 @@ func init() { rootCmd.AddCommand(netmapCmd) netmapCmd.AddCommand(netmapChildCommands...) - initCommonFlags(getEpochCmd) - initCommonFlags(netInfoCmd) + commonflags.Init(getEpochCmd) + commonflags.Init(netInfoCmd) - initCommonFlags(localNodeInfoCmd) + commonflags.Init(localNodeInfoCmd) localNodeInfoCmd.Flags().BoolVar(&nodeInfoJSON, "json", false, "print node info in JSON format") for _, netmapCommand := range netmapChildCommands { diff --git a/cmd/neofs-cli/modules/object.go b/cmd/neofs-cli/modules/object.go index 79c4b6b10..ad53cb0c7 100644 --- a/cmd/neofs-cli/modules/object.go +++ b/cmd/neofs-cli/modules/object.go @@ -18,6 +18,7 @@ import ( objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" "github.com/nspcc-dev/neofs-api-go/v2/refs" 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" "github.com/nspcc-dev/neofs-sdk-go/bearer" "github.com/nspcc-dev/neofs-sdk-go/checksum" @@ -55,7 +56,7 @@ var ( PersistentPreRun: func(cmd *cobra.Command, args []string) { // bind exactly that cmd's flags to // the viper before execution - bindCommonFlags(cmd) + commonflags.Bind(cmd) bindAPIFlags(cmd) }, } @@ -135,7 +136,7 @@ const noProgressFlag = "no-progress" var putExpiredOn uint64 func initObjectPutCmd() { - initCommonFlags(objectPutCmd) + commonflags.Init(objectPutCmd) flags := objectPutCmd.Flags() @@ -156,7 +157,7 @@ func initObjectPutCmd() { } func initObjectDeleteCmd() { - initCommonFlags(objectDelCmd) + commonflags.Init(objectDelCmd) flags := objectDelCmd.Flags() @@ -168,7 +169,7 @@ func initObjectDeleteCmd() { } func initObjectGetCmd() { - initCommonFlags(objectGetCmd) + commonflags.Init(objectGetCmd) flags := objectGetCmd.Flags() @@ -185,7 +186,7 @@ func initObjectGetCmd() { } func initObjectSearchCmd() { - initCommonFlags(objectSearchCmd) + commonflags.Init(objectSearchCmd) flags := objectSearchCmd.Flags() @@ -201,7 +202,7 @@ func initObjectSearchCmd() { } func initObjectHeadCmd() { - initCommonFlags(objectHeadCmd) + commonflags.Init(objectHeadCmd) flags := objectHeadCmd.Flags() @@ -219,7 +220,7 @@ func initObjectHeadCmd() { } func initObjectHashCmd() { - initCommonFlags(objectHashCmd) + commonflags.Init(objectHashCmd) flags := objectHashCmd.Flags() @@ -235,7 +236,7 @@ func initObjectHashCmd() { } func initObjectRangeCmd() { - initCommonFlags(objectRangeCmd) + commonflags.Init(objectRangeCmd) flags := objectRangeCmd.Flags() diff --git a/cmd/neofs-cli/modules/restore.go b/cmd/neofs-cli/modules/restore.go index 247615fff..7ddd265b5 100644 --- a/cmd/neofs-cli/modules/restore.go +++ b/cmd/neofs-cli/modules/restore.go @@ -3,6 +3,7 @@ package cmd import ( "github.com/mr-tron/base58" "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" controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server" "github.com/spf13/cobra" @@ -58,7 +59,7 @@ func restoreShard(cmd *cobra.Command, _ []string) { } func initControlRestoreShardCmd() { - initCommonFlagsWithoutRPC(restoreShardCmd) + commonflags.InitWithoutRPC(restoreShardCmd) flags := restoreShardCmd.Flags() flags.String(controlRPC, controlRPCDefault, controlRPCUsage) diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 940daeeab..016e2c468 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -11,6 +11,7 @@ import ( "github.com/mitchellh/go-homedir" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" 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/modules/acl" bearerCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/bearer" @@ -37,33 +38,6 @@ var ( ) 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 ':')" - - verbose = "verbose" - verboseShorthand = "v" - verboseDefault = false - verboseUsage = "verbose output" - ttl = "ttl" ttlShorthand = "" ttlDefault = 2 @@ -111,9 +85,10 @@ func init() { // Cobra supports persistent flags, which, if defined here, // will be global for your application. 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 // when this action is called directly. @@ -167,7 +142,7 @@ func initConfig() { // getKey returns private key that was provided in global arguments. func getKey() (*ecdsa.PrivateKey, error) { - if viper.GetBool(generateKey) { + if viper.GetBool(commonflags.GenerateKey) { priv, err := keys.NewPrivateKey() if err != nil { return nil, errCantGenerateKey @@ -178,7 +153,7 @@ func getKey() (*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 @@ -229,7 +204,7 @@ func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) { // getSDKClient calls getSDKGClientFlag with "rpc-endpoint" flag. 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 @@ -263,7 +238,7 @@ func ownerFromString(s string) (*owner.ID, error) { } func printVerbose(format string, a ...interface{}) { - if viper.GetBool(verbose) { + if viper.GetBool(commonflags.Verbose) { fmt.Printf(format+"\n", a...) } } @@ -287,31 +262,6 @@ func parseXHeaders() []*session.XHeader { 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) { ff := cmd.Flags() diff --git a/cmd/neofs-cli/modules/session/create.go b/cmd/neofs-cli/modules/session/create.go index 2e4d42028..6db71a013 100644 --- a/cmd/neofs-cli/modules/session/create.go +++ b/cmd/neofs-cli/modules/session/create.go @@ -5,6 +5,7 @@ import ( "io/ioutil" 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/pkg/network" "github.com/nspcc-dev/neofs-sdk-go/client" @@ -15,11 +16,8 @@ import ( const ( lifetimeFlag = "lifetime" - walletFlag = "wallet" - accountFlag = "address" outFlag = "out" jsonFlag = "json" - rpcFlag = "rpc-endpoint" ) const defaultLifetime = 10 @@ -32,28 +30,28 @@ var createCmd = &cobra.Command{ func init() { 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(accountFlag, "a", "", "account address") + createCmd.Flags().StringP(commonflags.WalletPath, commonflags.WalletPathShorthand, commonflags.WalletPathDefault, commonflags.WalletPathUsage) + createCmd.Flags().StringP(commonflags.Account, commonflags.AccountShorthand, commonflags.AccountDefault, commonflags.AccountUsage) createCmd.Flags().String(outFlag, "", "file to write session token to") 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(), walletFlag) + _ = cobra.MarkFlagRequired(createCmd.Flags(), commonflags.WalletPath) _ = cobra.MarkFlagRequired(createCmd.Flags(), outFlag) - _ = cobra.MarkFlagRequired(createCmd.Flags(), rpcFlag) + _ = cobra.MarkFlagRequired(createCmd.Flags(), commonflags.RPC) } func createSession(cmd *cobra.Command, _ []string) error { - walletPath, _ := cmd.Flags().GetString(walletFlag) - accPath, _ := cmd.Flags().GetString(accountFlag) + walletPath, _ := cmd.Flags().GetString(commonflags.WalletPath) + accPath, _ := cmd.Flags().GetString(commonflags.Account) privKey, err := key.Get(walletPath, accPath) if err != nil { return err } var netAddr network.Address - addrStr, _ := cmd.Flags().GetString(rpcFlag) + addrStr, _ := cmd.Flags().GetString(commonflags.RPC) if err := netAddr.FromString(addrStr); err != nil { return err } diff --git a/cmd/neofs-cli/modules/storagegroup.go b/cmd/neofs-cli/modules/storagegroup.go index dc9f54fd3..88454b156 100644 --- a/cmd/neofs-cli/modules/storagegroup.go +++ b/cmd/neofs-cli/modules/storagegroup.go @@ -7,6 +7,7 @@ import ( "fmt" 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-sdk-go/object" 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) { // bind exactly that cmd's flags to // the viper before execution - bindCommonFlags(cmd) + commonflags.Bind(cmd) bindAPIFlags(cmd) }, } @@ -68,7 +69,7 @@ var ( ) func initSGPutCmd() { - initCommonFlags(sgPutCmd) + commonflags.Init(sgPutCmd) flags := sgPutCmd.Flags() @@ -80,7 +81,7 @@ func initSGPutCmd() { } func initSGGetCmd() { - initCommonFlags(sgGetCmd) + commonflags.Init(sgGetCmd) flags := sgGetCmd.Flags() @@ -92,14 +93,14 @@ func initSGGetCmd() { } func initSGListCmd() { - initCommonFlags(sgListCmd) + commonflags.Init(sgListCmd) sgListCmd.Flags().String("cid", "", "Container ID") _ = sgListCmd.MarkFlagRequired("cid") } func initSGDeleteCmd() { - initCommonFlags(sgDelCmd) + commonflags.Init(sgDelCmd) flags := sgDelCmd.Flags() diff --git a/cmd/neofs-cli/modules/util.go b/cmd/neofs-cli/modules/util.go index a42f76caf..854498e5c 100644 --- a/cmd/neofs-cli/modules/util.go +++ b/cmd/neofs-cli/modules/util.go @@ -10,6 +10,7 @@ import ( "strconv" "time" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/pkg/util/keyer" locodedb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db" airportsdb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db/airports" @@ -30,9 +31,9 @@ var ( PersistentPreRun: func(cmd *cobra.Command, args []string) { flags := cmd.Flags() - _ = viper.BindPFlag(generateKey, flags.Lookup(generateKey)) - _ = viper.BindPFlag(walletPath, flags.Lookup(walletPath)) - _ = viper.BindPFlag(address, flags.Lookup(address)) + _ = viper.BindPFlag(commonflags.GenerateKey, flags.Lookup(commonflags.GenerateKey)) + _ = viper.BindPFlag(commonflags.WalletPath, flags.Lookup(commonflags.WalletPath)) + _ = 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") } -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() { - initCommonFlagsWithoutRPC(signBearerCmd) + commonflags.InitWithoutRPC(signBearerCmd) flags := signBearerCmd.Flags() @@ -207,7 +200,7 @@ func initUtilSignBearerCmd() { } func initUtilSignSessionCmd() { - initCommonFlagsWithoutRPC(signSessionCmd) + commonflags.InitWithoutRPC(signSessionCmd) flags := signSessionCmd.Flags()