Do not use SetNormalizeFunc
In order to avoid inconsistent depreciation message I choose not to use SetNormalizeFunc
I modified the handling of resources/verbs in consequence
@@ -9,7 +9,6 @@ import (
"github.com/sensu/sensu-go/cli/commands/helpers"
"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
- "github.com/spf13/pflag"
)
// CreateCommand defines new command to create a cluster role
@@ -37,18 +36,33 @@ func CreateCommand(cli *cli.SensuCli) *cobra.Command {
if err != nil {
return err
}
+ if len(verbs) == 0 {
+ // Check the old "verb"
+ verbs, err = cmd.Flags().GetStringSlice("verb")
+ if err != nil {
+ return err
+ }
+ // If it's still zero raise an error
if len(verbs) == 0 {
return errors.New("at least one verb must be provided")
}
+ }
rule.Verbs = verbs
resources, err := cmd.Flags().GetStringSlice("resources")
if err != nil {
return err
}
+ if len(resources) == 0 {
+ // Check old resource
+ resources, err = cmd.Flags().GetStringSlice("resource")
+ if err != nil {
+ return err
+ }
if len(resources) == 0 {
return errors.New("at least one resource must be provided")
}
+ }
rule.Resources = resources
resourceNames, err := cmd.Flags().GetStringSlice("resource-name")
@@ -71,17 +85,19 @@ func CreateCommand(cli *cli.SensuCli) *cobra.Command {
},
}
+ // Non plural
// To be removed in a later version?
_ = cmd.Flags().StringSliceP("verb", "", []string{},
"verbs that apply to the resources contained in the rule",
)
- _ = cmd.Flags().StringSliceP("verbs", "v", []string{},
- "verbs that apply to the resources contained in the rule",
- )
- // To be removed in a later version?
_ = cmd.Flags().StringSliceP("resource", "", []string{},
"resources that the rule applies to",
)
+
+ // Plural
+ _ = cmd.Flags().StringSliceP("verbs", "v", []string{},
+ "verbs that apply to the resources contained in the rule",
+ )
_ = cmd.Flags().StringSliceP("resources", "r", []string{},
"resources that the rule applies to",
)
@@ -93,18 +109,6 @@ func CreateCommand(cli *cli.SensuCli) *cobra.Command {
cmd.Flags().MarkDeprecated("verb", "please use verbs instead.")
cmd.Flags().MarkHidden("resource")
cmd.Flags().MarkHidden("verb")
- cmd.Flags().SetNormalizeFunc(normalizeParamsRun)
return cmd
}
-
-func normalizeParamsRun(f *pflag.FlagSet, name string) pflag.NormalizedName {
- switch name {
- case "resource":
- name = "resources"
- case "verb":
- name = "verbs"
- }
-
- return pflag.NormalizedName(name)
-}
