forked from TrueCloudLab/frostfs-node
[#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 <evgeniy@nspcc.ru>
This commit is contained in:
parent
736e09a70d
commit
a2bcb3e0ce
6 changed files with 45 additions and 41 deletions
33
cmd/neofs-cli/internal/commonflags/api.go
Normal file
33
cmd/neofs-cli/internal/commonflags/api.go
Normal file
|
@ -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))
|
||||
}
|
|
@ -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{
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue