diff --git a/cmd/neofs-cli/internal/commonflags/api.go b/cmd/neofs-cli/internal/commonflags/api.go new file mode 100644 index 0000000000..88321176f8 --- /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 6817dee531..a8a5666935 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 736a09f096..e6bec1eae9 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 ed9a54524b..515c117361 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 c0279c9b86..4ec7ae0cce 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 36e3fe0024..2b5cb28a7e 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()