Merge duplicated attr/secret/kv flag handling into one function (#2720)
This commit is contained in:
1 parent
1596b02e53
commit
5353725278
6 files changed
+154
-133
No files matched your search
@@ -94,7 +94,15 @@ func (c *JsonCommand) Flags() *base.FlagSets {
|
||||
common.PopulateCommonFlags(c.Command, f, "json-type credential", flagsJsonMap, c.Func)
|
||||
|
||||
f = set.NewFlagSet("Object Options")
|
||||
common.PopulateObjectFlags(c.Command, f, flagsJsonMap, c.Func)
|
||||
objsInput := common.CombinedSliceFlagValuePopulationInput{
|
||||
FlagSet: f,
|
||||
FlagNames: flagsJsonMap[c.Func],
|
||||
FullPopulationFlag: &c.FlagObject,
|
||||
FullPopulationInputName: "object",
|
||||
PiecewisePopulationFlag: &c.FlagKv,
|
||||
PiecewisePopulationInputBaseName: "kv",
|
||||
}
|
||||
common.PopulateCombinedSliceFlagValue(objsInput)
|
||||
|
||||
extraJsonFlagsFunc(c, set, f)
|
||||
|
||||
|
||||
@@ -94,10 +94,26 @@ func (c *PluginCommand) Flags() *base.FlagSets {
|
||||
common.PopulateCommonFlags(c.Command, f, "plugin-type host catalog", flagsPluginMap, c.Func)
|
||||
|
||||
f = set.NewFlagSet("Attribute Options")
|
||||
common.PopulateAttributeFlags(c.Command, f, flagsPluginMap, c.Func)
|
||||
attrsInput := common.CombinedSliceFlagValuePopulationInput{
|
||||
FlagSet: f,
|
||||
FlagNames: flagsPluginMap[c.Func],
|
||||
FullPopulationFlag: &c.FlagAttributes,
|
||||
FullPopulationInputName: "attributes",
|
||||
PiecewisePopulationFlag: &c.FlagAttrs,
|
||||
PiecewisePopulationInputBaseName: "attr",
|
||||
}
|
||||
common.PopulateCombinedSliceFlagValue(attrsInput)
|
||||
|
||||
f = set.NewFlagSet("Secrets Options")
|
||||
common.PopulateSecretFlags(c.Command, f, flagsPluginMap, c.Func)
|
||||
scrtsInput := common.CombinedSliceFlagValuePopulationInput{
|
||||
FlagSet: f,
|
||||
FlagNames: flagsPluginMap[c.Func],
|
||||
FullPopulationFlag: &c.FlagSecrets,
|
||||
FullPopulationInputName: "secrets",
|
||||
PiecewisePopulationFlag: &c.FlagScrts,
|
||||
PiecewisePopulationInputBaseName: "secret",
|
||||
}
|
||||
common.PopulateCombinedSliceFlagValue(scrtsInput)
|
||||
|
||||
extraPluginFlagsFunc(c, set, f)
|
||||
|
||||
|
||||
@@ -96,7 +96,15 @@ func (c *PluginCommand) Flags() *base.FlagSets {
|
||||
common.PopulateCommonFlags(c.Command, f, "plugin-type host set", flagsPluginMap, c.Func)
|
||||
|
||||
f = set.NewFlagSet("Attribute Options")
|
||||
common.PopulateAttributeFlags(c.Command, f, flagsPluginMap, c.Func)
|
||||
attrsInput := common.CombinedSliceFlagValuePopulationInput{
|
||||
FlagSet: f,
|
||||
FlagNames: flagsPluginMap[c.Func],
|
||||
FullPopulationFlag: &c.FlagAttributes,
|
||||
FullPopulationInputName: "attributes",
|
||||
PiecewisePopulationFlag: &c.FlagAttrs,
|
||||
PiecewisePopulationInputBaseName: "attr",
|
||||
}
|
||||
common.PopulateCombinedSliceFlagValue(attrsInput)
|
||||
|
||||
extraPluginFlagsFunc(c, set, f)
|
||||
|
||||
|
||||
+80
-125
@@ -116,151 +116,106 @@ func PopulateCommonFlags(c *base.Command, f *base.FlagSet, resourceType string,
|
||||
}
|
||||
}
|
||||
|
||||
func PopulateAttributeFlags(c *base.Command, f *base.FlagSet, flagNames map[string][]string, command string) {
|
||||
keyDelimiter := "."
|
||||
for _, name := range flagNames[command] {
|
||||
switch name {
|
||||
case "attributes":
|
||||
f.StringVar(&base.StringVar{
|
||||
Name: "attributes",
|
||||
Target: &c.FlagAttributes,
|
||||
Usage: `A JSON map value to use as the entirety of the request's attributes map. Usually this will be sourced from a file via "file://" syntax. Is exclusive with the other attr flags.`,
|
||||
})
|
||||
case "attr":
|
||||
f.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "attr",
|
||||
Target: &c.FlagAttrs,
|
||||
KvSplit: true,
|
||||
KeyDelimiter: &keyDelimiter,
|
||||
ProtoCompatKey: true,
|
||||
Usage: `A key=value attribute to add to the request's attributes map. The type is automatically inferred. Use -string-attr, -bool-attr, or -num-attr if the type needs to be overridden. Can be specified multiple times. Supports sourcing values from files via "file://" and env vars via "env://"`,
|
||||
})
|
||||
case "string-attr":
|
||||
f.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "string-attr",
|
||||
Target: &c.FlagAttrs,
|
||||
KvSplit: true,
|
||||
KeyDelimiter: &keyDelimiter,
|
||||
ProtoCompatKey: true,
|
||||
Usage: `A key=value string attribute to add to the request's attributes map. Can be specified multiple times. Supports sourcing values from files via "file://" and env vars via "env://"`,
|
||||
})
|
||||
case "bool-attr":
|
||||
f.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "bool-attr",
|
||||
Target: &c.FlagAttrs,
|
||||
KvSplit: true,
|
||||
KeyDelimiter: &keyDelimiter,
|
||||
ProtoCompatKey: true,
|
||||
Usage: `A key=value bool attribute to add to the request's attributes map. Can be specified multiple times. Supports sourcing values from files via "file://" and env vars via "env://"`,
|
||||
})
|
||||
case "num-attr":
|
||||
f.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "num-attr",
|
||||
Target: &c.FlagAttrs,
|
||||
KvSplit: true,
|
||||
KeyDelimiter: &keyDelimiter,
|
||||
ProtoCompatKey: true,
|
||||
Usage: `A key=value numeric attribute to add to the request's attributes map. Can be specified multiple times. Supports sourcing values from files via "file://" and env vars via "env://"`,
|
||||
})
|
||||
}
|
||||
}
|
||||
type CombinedSliceFlagValuePopulationInput struct {
|
||||
// FlagSet is the flag set to add vars to
|
||||
FlagSet *base.FlagSet
|
||||
|
||||
// FlagNames is the set of flag names
|
||||
FlagNames []string
|
||||
|
||||
// FullPopulationFlag is the string var to set if a fully-specified map is
|
||||
// supplied, e.g. "attributes"
|
||||
FullPopulationFlag *string
|
||||
|
||||
// FullPopulationInputName is the name of the flag when setting a
|
||||
// fully-specified map; also used for generating help texts
|
||||
FullPopulationInputName string
|
||||
|
||||
// PiecewisePopulationFlag is the var that is built up via the combination
|
||||
// method, e.g. "attr", "string-attr", etc.
|
||||
PiecewisePopulationFlag *[]base.CombinedSliceFlagValue
|
||||
|
||||
// PiecewisePopulationInputName is the base name of the flag when using the
|
||||
// combination method, e.g. "attr" will be used to build "string-attr"; also
|
||||
// used for generating help texts
|
||||
PiecewisePopulationInputBaseName string
|
||||
}
|
||||
|
||||
func PopulateSecretFlags(c *base.Command, f *base.FlagSet, flagNames map[string][]string, command string) {
|
||||
func PopulateCombinedSliceFlagValue(input CombinedSliceFlagValuePopulationInput) {
|
||||
keyDelimiter := "."
|
||||
for _, name := range flagNames[command] {
|
||||
for _, name := range input.FlagNames {
|
||||
switch name {
|
||||
case "secrets":
|
||||
f.StringVar(&base.StringVar{
|
||||
Name: "secrets",
|
||||
Target: &c.FlagSecrets,
|
||||
Usage: `A JSON map value to use as the entirety of the request's secrets map. Usually this will be sourced from a file via "file://" syntax. Is exclusive with the other secret flags.`,
|
||||
case input.FullPopulationInputName:
|
||||
input.FlagSet.StringVar(&base.StringVar{
|
||||
Name: input.FullPopulationInputName,
|
||||
Target: input.FullPopulationFlag,
|
||||
Usage: fmt.Sprintf(
|
||||
"A JSON map value to use as the entirety of the request's %s map. "+
|
||||
"Usually this will be sourced from a file via \"file://\" syntax. "+
|
||||
"Is exclusive with the other %s flags.",
|
||||
input.FullPopulationInputName,
|
||||
input.PiecewisePopulationInputBaseName,
|
||||
),
|
||||
})
|
||||
case "secret":
|
||||
f.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "secret",
|
||||
Target: &c.FlagScrts,
|
||||
case input.PiecewisePopulationInputBaseName:
|
||||
input.FlagSet.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: input.PiecewisePopulationInputBaseName,
|
||||
Target: input.PiecewisePopulationFlag,
|
||||
KvSplit: true,
|
||||
KeyDelimiter: &keyDelimiter,
|
||||
ProtoCompatKey: true,
|
||||
Usage: `A key=value secret to add to the request's secrets map. The type is automatically inferred. Use -string-secret, -bool-secret, or -num-secret if the type needs to be overridden. Can be specified multiple times. Supports sourcing values from files via "file://" and env vars via "env://"`,
|
||||
Usage: fmt.Sprintf(
|
||||
"A key=value pair to add to the request's %s map. "+
|
||||
"The type is automatically inferred. Use -string-%s, -bool-%s, or -num-%s if the type needs to be overridden. "+
|
||||
"Can be specified multiple times. "+
|
||||
"Supports sourcing values from files via \"file://\" and env vars via \"env://\".",
|
||||
input.FullPopulationInputName,
|
||||
input.PiecewisePopulationInputBaseName,
|
||||
input.PiecewisePopulationInputBaseName,
|
||||
input.PiecewisePopulationInputBaseName,
|
||||
),
|
||||
})
|
||||
case "string-secret":
|
||||
f.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "string-secret",
|
||||
Target: &c.FlagScrts,
|
||||
case "string-" + input.PiecewisePopulationInputBaseName:
|
||||
input.FlagSet.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "string-" + input.PiecewisePopulationInputBaseName,
|
||||
Target: input.PiecewisePopulationFlag,
|
||||
KvSplit: true,
|
||||
KeyDelimiter: &keyDelimiter,
|
||||
ProtoCompatKey: true,
|
||||
Usage: `A key=value string secret to add to the request's secrets map. Can be specified multiple times. Supports sourcing values from files via "file://" and env vars via "env://"`,
|
||||
Usage: fmt.Sprintf(
|
||||
"A key=value string value to add to the request's %s map. "+
|
||||
"Can be specified multiple times. "+
|
||||
"Supports sourcing values from files via \"file://\" and env vars via \"env://\"`.",
|
||||
input.FullPopulationInputName,
|
||||
),
|
||||
})
|
||||
case "bool-secret":
|
||||
f.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "bool-secret",
|
||||
Target: &c.FlagScrts,
|
||||
case "bool-" + input.PiecewisePopulationInputBaseName:
|
||||
input.FlagSet.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "bool-" + input.PiecewisePopulationInputBaseName,
|
||||
Target: input.PiecewisePopulationFlag,
|
||||
KvSplit: true,
|
||||
KeyDelimiter: &keyDelimiter,
|
||||
ProtoCompatKey: true,
|
||||
Usage: `A key=value bool secret to add to the request's secrets map. Can be specified multiple times. Supports sourcing values from files via "file://" and env vars via "env://"`,
|
||||
Usage: fmt.Sprintf(
|
||||
"A key=value bool value to add to the request's %s map. "+
|
||||
"Can be specified multiple times. "+
|
||||
"Supports sourcing values from files via \"file://\" and env vars via \"env://\"`.",
|
||||
input.FullPopulationInputName,
|
||||
),
|
||||
})
|
||||
case "num-secret":
|
||||
f.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "num-secret",
|
||||
Target: &c.FlagScrts,
|
||||
case "num-" + input.PiecewisePopulationInputBaseName:
|
||||
input.FlagSet.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "num-" + input.PiecewisePopulationInputBaseName,
|
||||
Target: input.PiecewisePopulationFlag,
|
||||
KvSplit: true,
|
||||
KeyDelimiter: &keyDelimiter,
|
||||
ProtoCompatKey: true,
|
||||
Usage: `A key=value numeric secret to add to the request's secrets map. Can be specified multiple times. Supports sourcing values from files via "file://" and env vars via "env://"`,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func PopulateObjectFlags(c *base.Command, f *base.FlagSet, flagNames map[string][]string, command string) {
|
||||
keyDelimiter := "."
|
||||
for _, name := range flagNames[command] {
|
||||
switch name {
|
||||
case "object":
|
||||
f.StringVar(&base.StringVar{
|
||||
Name: "object",
|
||||
Target: &c.FlagObject,
|
||||
Usage: `A JSON map value to use as the entirety of the request's object map. Usually this will be sourced from a file via "file://" syntax. Is exclusive with the other kv flags.`,
|
||||
})
|
||||
case "kv":
|
||||
f.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "kv",
|
||||
Target: &c.FlagKv,
|
||||
KvSplit: true,
|
||||
KeyDelimiter: &keyDelimiter,
|
||||
ProtoCompatKey: true,
|
||||
Usage: `A key=value pair to add to the request's object map. The type is automatically inferred. Use -string-kv, -bool-kv, or -num-kv if the type needs to be overridden. Can be specified multiple times. Supports sourcing values from files via "file://" and env vars via "env://"`,
|
||||
})
|
||||
case "string-kv":
|
||||
f.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "string-kv",
|
||||
Target: &c.FlagKv,
|
||||
KvSplit: true,
|
||||
KeyDelimiter: &keyDelimiter,
|
||||
ProtoCompatKey: true,
|
||||
Usage: `A key=value string value to add to the request's object map. Can be specified multiple times. Supports sourcing values from files via "file://" and env vars via "env://"`,
|
||||
})
|
||||
case "bool-kv":
|
||||
f.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "bool-kv",
|
||||
Target: &c.FlagKv,
|
||||
KvSplit: true,
|
||||
KeyDelimiter: &keyDelimiter,
|
||||
ProtoCompatKey: true,
|
||||
Usage: `A key=value bool value to add to the request's object map. Can be specified multiple times. Supports sourcing values from files via "file://" and env vars via "env://"`,
|
||||
})
|
||||
case "num-kv":
|
||||
f.CombinationSliceVar(&base.CombinationSliceVar{
|
||||
Name: "num-kv",
|
||||
Target: &c.FlagKv,
|
||||
KvSplit: true,
|
||||
KeyDelimiter: &keyDelimiter,
|
||||
ProtoCompatKey: true,
|
||||
Usage: `A key=value numeric value to add to the request's object map. Can be specified multiple times. Supports sourcing values from files via "file://" and env vars via "env://"`,
|
||||
Usage: fmt.Sprintf(
|
||||
"A key=value numeric value to add to the request's %s map. "+
|
||||
"Can be specified multiple times. "+
|
||||
"Supports sourcing values from files via \"file://\" and env vars via \"env://\"`.",
|
||||
input.FullPopulationInputName,
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,6 +145,7 @@ func TestPopulateAttrFlags(t *testing.T) {
|
||||
flagSet := c.FlagSet(base.FlagSetNone)
|
||||
f := flagSet.NewFlagSet("Attribute Options")
|
||||
cmd := "create"
|
||||
|
||||
flagNames := map[string][]string{
|
||||
cmd: {
|
||||
"attributes",
|
||||
@@ -155,7 +156,16 @@ func TestPopulateAttrFlags(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
PopulateAttributeFlags(c, f, flagNames, cmd)
|
||||
attrsInput := CombinedSliceFlagValuePopulationInput{
|
||||
FlagSet: f,
|
||||
FlagNames: flagNames[cmd],
|
||||
FullPopulationFlag: &c.FlagAttributes,
|
||||
FullPopulationInputName: "attributes",
|
||||
PiecewisePopulationFlag: &c.FlagAttrs,
|
||||
PiecewisePopulationInputBaseName: "attr",
|
||||
}
|
||||
PopulateCombinedSliceFlagValue(attrsInput)
|
||||
|
||||
err := flagSet.Parse(tt.args)
|
||||
if tt.expectedErr != "" {
|
||||
require.Error(err)
|
||||
|
||||
@@ -205,17 +205,41 @@ func (c *{{ camelCase .SubActionPrefix }}Command) Flags() *base.FlagSets {
|
||||
|
||||
{{ if .HasGenericAttributes }}
|
||||
f = set.NewFlagSet("Attribute Options")
|
||||
common.PopulateAttributeFlags(c.Command, f, flags{{ camelCase .SubActionPrefix }}Map, c.Func)
|
||||
attrsInput := common.CombinedSliceFlagValuePopulationInput{
|
||||
FlagSet: f,
|
||||
FlagNames: flags{{ camelCase .SubActionPrefix }}Map[c.Func],
|
||||
FullPopulationFlag: &c.FlagAttributes,
|
||||
FullPopulationInputName: "attributes",
|
||||
PiecewisePopulationFlag: &c.FlagAttrs,
|
||||
PiecewisePopulationInputBaseName: "attr",
|
||||
}
|
||||
common.PopulateCombinedSliceFlagValue(attrsInput)
|
||||
{{ end }}
|
||||
|
||||
{{ if .HasGenericSecrets }}
|
||||
f = set.NewFlagSet("Secrets Options")
|
||||
common.PopulateSecretFlags(c.Command, f, flags{{ camelCase .SubActionPrefix }}Map, c.Func)
|
||||
scrtsInput := common.CombinedSliceFlagValuePopulationInput{
|
||||
FlagSet: f,
|
||||
FlagNames: flags{{ camelCase .SubActionPrefix }}Map[c.Func],
|
||||
FullPopulationFlag: &c.FlagSecrets,
|
||||
FullPopulationInputName: "secrets",
|
||||
PiecewisePopulationFlag: &c.FlagScrts,
|
||||
PiecewisePopulationInputBaseName: "secret",
|
||||
}
|
||||
common.PopulateCombinedSliceFlagValue(scrtsInput)
|
||||
{{ end }}
|
||||
|
||||
{{ if .HasJsonObject }}
|
||||
f = set.NewFlagSet("Object Options")
|
||||
common.PopulateObjectFlags(c.Command, f, flags{{ camelCase .SubActionPrefix }}Map, c.Func)
|
||||
objsInput := common.CombinedSliceFlagValuePopulationInput{
|
||||
FlagSet: f,
|
||||
FlagNames: flags{{ camelCase .SubActionPrefix }}Map[c.Func],
|
||||
FullPopulationFlag: &c.FlagObject,
|
||||
FullPopulationInputName: "object",
|
||||
PiecewisePopulationFlag: &c.FlagKv,
|
||||
PiecewisePopulationInputBaseName: "kv",
|
||||
}
|
||||
common.PopulateCombinedSliceFlagValue(objsInput)
|
||||
{{ end }}
|
||||
|
||||
extra{{ camelCase .SubActionPrefix }}FlagsFunc(c, set, f)
|
||||
|
||||
Reference in new issue
Block a user