mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 00:52:55 +00:00
change args to use flags instead, so that values starting with hyphen can be correctly parsed.
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/setters"
|
||||
"sigs.k8s.io/kustomize/kyaml/setters2/settersutil"
|
||||
@@ -20,8 +21,8 @@ import (
|
||||
func NewSetRunner(parent string) *SetRunner {
|
||||
r := &SetRunner{}
|
||||
c := &cobra.Command{
|
||||
Use: "set DIR NAME [VALUE]",
|
||||
Args: cobra.MinimumNArgs(3),
|
||||
Use: "set DIR NAME --values [VALUE]",
|
||||
Args: cobra.MinimumNArgs(2),
|
||||
Short: commands.SetShort,
|
||||
Long: commands.SetLong,
|
||||
Example: commands.SetExamples,
|
||||
@@ -30,6 +31,8 @@ func NewSetRunner(parent string) *SetRunner {
|
||||
}
|
||||
fixDocs(parent, c)
|
||||
r.Command = c
|
||||
c.Flags().StringArrayVar(&r.Values, "values", []string{},
|
||||
"optional flag, the values of the setter to be set to")
|
||||
c.Flags().StringVar(&r.Perform.SetBy, "set-by", "",
|
||||
"annotate the field with who set it")
|
||||
c.Flags().StringVar(&r.Perform.Description, "description", "",
|
||||
@@ -53,6 +56,7 @@ type SetRunner struct {
|
||||
Perform setters.PerformSetters
|
||||
Set settersutil.FieldSetter
|
||||
OpenAPIFile string
|
||||
Values []string
|
||||
}
|
||||
|
||||
func initSetterVersion(c *cobra.Command, args []string) error {
|
||||
@@ -75,16 +79,25 @@ func initSetterVersion(c *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
func (r *SetRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
valueFlagSet := isFlagSet("values", c)
|
||||
|
||||
if valueFlagSet && len(args) > 2 {
|
||||
return errors.Errorf("value should set either from flag or arg")
|
||||
}
|
||||
|
||||
if len(args) > 1 {
|
||||
r.Perform.Name = args[1]
|
||||
r.Lookup.Name = args[1]
|
||||
}
|
||||
if len(args) > 2 {
|
||||
|
||||
if isFlagSet("values", c) && len(r.Values) == 1 {
|
||||
r.Perform.Value = r.Values[0]
|
||||
} else if len(args) > 2 {
|
||||
r.Perform.Value = args[2]
|
||||
}
|
||||
|
||||
if setterVersion == "" {
|
||||
if len(args) < 3 {
|
||||
if len(args) < 2 || len(args) < 3 && !valueFlagSet {
|
||||
setterVersion = "v1"
|
||||
} else if err := initSetterVersion(c, args); err != nil {
|
||||
return err
|
||||
@@ -93,12 +106,19 @@ func (r *SetRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
if setterVersion == "v2" {
|
||||
var err error
|
||||
r.Set.Name = args[1]
|
||||
r.Set.Value = args[2]
|
||||
if valueFlagSet {
|
||||
r.Set.Value = r.Values[0]
|
||||
} else {
|
||||
r.Set.Value = args[2]
|
||||
}
|
||||
|
||||
// set remaining values as list values
|
||||
if len(args) > 3 {
|
||||
if valueFlagSet && len(r.Values) > 1 {
|
||||
r.Set.ListValues = r.Values[1:]
|
||||
} else if !valueFlagSet && len(args) > 3 {
|
||||
r.Set.ListValues = args[3:]
|
||||
}
|
||||
|
||||
r.Set.Description = r.Perform.Description
|
||||
r.Set.SetBy = r.Perform.SetBy
|
||||
r.OpenAPIFile, err = ext.GetOpenAPIFile(args)
|
||||
@@ -116,7 +136,7 @@ func (r *SetRunner) runE(c *cobra.Command, args []string) error {
|
||||
fmt.Fprintf(c.OutOrStdout(), "set %d fields\n", count)
|
||||
return handleError(c, err)
|
||||
}
|
||||
if len(args) == 3 {
|
||||
if len(args) > 2 || isFlagSet("values", c) {
|
||||
return handleError(c, r.perform(c, args))
|
||||
}
|
||||
return handleError(c, lookup(r.Lookup, c, args))
|
||||
|
||||
Reference in New Issue
Block a user