From a2bcb3e0ceee2f02af42a5755d655acef67d5001 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 30 May 2022 16:04:40 +0300 Subject: [PATCH] [#1074] neofs-cli: Move common API flags to a separate package TTL and XHeader flags are reused between multiple commands. Signed-off-by: Evgenii Stratonikov --- cmd/neofs-cli/internal/commonflags/api.go | 33 +++++++++++++++++++++++ cmd/neofs-cli/modules/container.go | 7 ++--- cmd/neofs-cli/modules/netmap.go | 7 ++--- cmd/neofs-cli/modules/object.go | 7 +++-- cmd/neofs-cli/modules/root.go | 27 +++---------------- cmd/neofs-cli/modules/storagegroup.go | 5 ++-- 6 files changed, 45 insertions(+), 41 deletions(-) create mode 100644 cmd/neofs-cli/internal/commonflags/api.go diff --git a/cmd/neofs-cli/internal/commonflags/api.go b/cmd/neofs-cli/internal/commonflags/api.go new file mode 100644 index 000000000..88321176f --- /dev/null +++ b/cmd/neofs-cli/internal/commonflags/api.go @@ -0,0 +1,33 @@ +package commonflags + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +const ( + TTL = "ttl" + TTLShorthand = "" + TTLDefault = 2 + TTLUsage = "TTL value in request meta header" + + XHeadersKey = "xhdr" + XHeadersShorthand = "x" + XHeadersUsage = "Request X-Headers in form of Key=Value" +) + +// InitAPI inits common flags for storage node services. +func InitAPI(cmd *cobra.Command) { + ff := cmd.Flags() + + ff.StringSliceP(XHeadersKey, XHeadersShorthand, []string{}, XHeadersUsage) + ff.Uint32P(TTL, TTLShorthand, TTLDefault, TTLUsage) +} + +// BindAPI binds API flags of storage node services to the viper. +func BindAPI(cmd *cobra.Command) { + ff := cmd.Flags() + + _ = viper.BindPFlag(TTL, ff.Lookup(TTL)) + _ = viper.BindPFlag(XHeadersKey, ff.Lookup(XHeadersKey)) +} diff --git a/cmd/neofs-cli/modules/container.go b/cmd/neofs-cli/modules/container.go index 6817dee53..a8a566693 100644 --- a/cmd/neofs-cli/modules/container.go +++ b/cmd/neofs-cli/modules/container.go @@ -103,7 +103,7 @@ var containerCmd = &cobra.Command{ // bind exactly that cmd's flags to // the viper before execution commonflags.Bind(cmd) - bindAPIFlags(cmd) + commonflags.BindAPI(cmd) }, } @@ -578,10 +578,7 @@ func init() { initContainerSetEACLCmd() for _, containerCommand := range containerChildCommand { - flags := containerCommand.Flags() - - flags.StringSliceVarP(&xHeaders, xHeadersKey, xHeadersShorthand, xHeadersDefault, xHeadersUsage) - flags.Uint32P(ttl, ttlShorthand, ttlDefault, ttlUsage) + commonflags.InitAPI(containerCommand) } for _, cmd := range []*cobra.Command{ diff --git a/cmd/neofs-cli/modules/netmap.go b/cmd/neofs-cli/modules/netmap.go index 736a09f09..e6bec1eae 100644 --- a/cmd/neofs-cli/modules/netmap.go +++ b/cmd/neofs-cli/modules/netmap.go @@ -28,7 +28,7 @@ var netmapCmd = &cobra.Command{ // bind exactly that cmd's flags to // the viper before execution commonflags.Bind(cmd) - bindAPIFlags(cmd) + commonflags.BindAPI(cmd) }, } @@ -49,10 +49,7 @@ func init() { localNodeInfoCmd.Flags().BoolVar(&nodeInfoJSON, "json", false, "print node info in JSON format") for _, netmapCommand := range netmapChildCommands { - flags := netmapCommand.Flags() - - flags.StringSliceVarP(&xHeaders, xHeadersKey, xHeadersShorthand, xHeadersDefault, xHeadersUsage) - flags.Uint32P(ttl, ttlShorthand, ttlDefault, ttlUsage) + commonflags.InitAPI(netmapCommand) } } diff --git a/cmd/neofs-cli/modules/object.go b/cmd/neofs-cli/modules/object.go index ed9a54524..515c11736 100644 --- a/cmd/neofs-cli/modules/object.go +++ b/cmd/neofs-cli/modules/object.go @@ -57,7 +57,7 @@ var ( // bind exactly that cmd's flags to // the viper before execution commonflags.Bind(cmd) - bindAPIFlags(cmd) + commonflags.BindAPI(cmd) }, } @@ -270,8 +270,7 @@ func init() { flags := objCommand.Flags() flags.String(bearerTokenFlag, "", "File with signed JSON or binary encoded bearer token") - flags.StringSliceVarP(&xHeaders, xHeadersKey, xHeadersShorthand, xHeadersDefault, xHeadersUsage) - flags.Uint32P(ttl, ttlShorthand, ttlDefault, ttlUsage) + commonflags.InitAPI(objCommand) } // Here you will define your flags and configuration settings. @@ -398,7 +397,7 @@ func prepareObjectPrm(cmd *cobra.Command, prms ...objectPrm) { prepareBearerPrm(cmd, prms[i]) prms[i].SetTTL(getTTL()) - prms[i].SetXHeaders(parseXHeaders()) + prms[i].SetXHeaders(parseXHeaders(cmd)) } } diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index c0279c9b8..4ec7ae0cc 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -31,26 +31,11 @@ const ( envPrefix = "NEOFS_CLI" ) -var xHeaders []string - // Global scope flags. var ( cfgFile string ) -const ( - ttl = "ttl" - ttlShorthand = "" - ttlDefault = 2 - ttlUsage = "TTL value in request meta header" - - xHeadersKey = "xhdr" - xHeadersShorthand = "x" - xHeadersUsage = "Request X-Headers in form of Key=Value" -) - -var xHeadersDefault []string - // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "neofs-cli", @@ -171,7 +156,7 @@ func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) { } func getTTL() uint32 { - ttl := viper.GetUint32(ttl) + ttl := viper.GetUint32(commonflags.TTL) common.PrintVerbose("TTL: %d", ttl) return ttl @@ -187,7 +172,8 @@ func userFromString(id *user.ID, s string) error { return nil } -func parseXHeaders() []string { +func parseXHeaders(cmd *cobra.Command) []string { + xHeaders, _ := cmd.Flags().GetStringSlice(commonflags.XHeadersKey) xs := make([]string, 0, 2*len(xHeaders)) for i := range xHeaders { @@ -201,10 +187,3 @@ func parseXHeaders() []string { return xs } - -func bindAPIFlags(cmd *cobra.Command) { - ff := cmd.Flags() - - _ = viper.BindPFlag(ttl, ff.Lookup(ttl)) - _ = viper.BindPFlag(xHeadersKey, ff.Lookup(xHeadersKey)) -} diff --git a/cmd/neofs-cli/modules/storagegroup.go b/cmd/neofs-cli/modules/storagegroup.go index 36e3fe002..2b5cb28a7 100644 --- a/cmd/neofs-cli/modules/storagegroup.go +++ b/cmd/neofs-cli/modules/storagegroup.go @@ -28,7 +28,7 @@ var storagegroupCmd = &cobra.Command{ // bind exactly that cmd's flags to // the viper before execution commonflags.Bind(cmd) - bindAPIFlags(cmd) + commonflags.BindAPI(cmd) }, } @@ -128,8 +128,7 @@ func init() { flags := sgCommand.Flags() flags.String(bearerTokenFlag, "", "File with signed JSON or binary encoded bearer token") - flags.StringSliceVarP(&xHeaders, xHeadersKey, xHeadersShorthand, xHeadersDefault, xHeadersUsage) - flags.Uint32P(ttl, ttlShorthand, ttlDefault, ttlUsage) + commonflags.InitAPI(sgCommand) } initSGPutCmd()