update on util and comments

This commit is contained in:
Jijie Wei
2020-05-29 16:23:10 -07:00
parent 4b5b0dfdce
commit e63b9ef825
3 changed files with 7 additions and 18 deletions

View File

@@ -20,7 +20,7 @@ import (
func NewCreateSetterRunner(parent string) *CreateSetterRunner {
r := &CreateSetterRunner{}
set := &cobra.Command{
Use: "create-setter DIR NAME --value VALUE",
Use: "create-setter DIR NAME VALUE",
Args: cobra.RangeArgs(2, 3),
Short: commands.CreateSetterShort,
Long: commands.CreateSetterLong,
@@ -29,7 +29,7 @@ func NewCreateSetterRunner(parent string) *CreateSetterRunner {
RunE: r.runE,
}
set.Flags().StringVar(&r.Set.SetPartialField.Setter.Value, "value", "",
"optional flag, the value of the setter to be set to")
"optional flag, alternative to specifying the value as an argument. e.g. used to specify values that start with '-'")
set.Flags().StringVar(&r.Set.SetPartialField.SetBy, "set-by", "",
"record who the field was default by.")
set.Flags().StringVar(&r.Set.SetPartialField.Description, "description", "",
@@ -75,7 +75,7 @@ func (r *CreateSetterRunner) runE(c *cobra.Command, args []string) error {
}
func (r *CreateSetterRunner) preRunE(c *cobra.Command, args []string) error {
valueSetFromFlag := isFlagSet("value", c)
valueSetFromFlag := c.Flag("value").Changed
var err error
r.Set.SetPartialField.Setter.Name = args[1]
r.CreateSetter.Name = args[1]
@@ -91,7 +91,7 @@ func (r *CreateSetterRunner) preRunE(c *cobra.Command, args []string) error {
}
if setterVersion == "" {
if len(args) < 2 || !valueSetFromFlag && len(args) < 3 {
if len(args) < 2 || !c.Flag("value").Changed && len(args) < 3 {
setterVersion = "v1"
} else if err := initSetterVersion(c, args); err != nil {
return err

View File

@@ -79,7 +79,7 @@ func initSetterVersion(c *cobra.Command, args []string) error {
}
func (r *SetRunner) preRunE(c *cobra.Command, args []string) error {
valueFlagSet := isFlagSet("values", c)
valueFlagSet := c.Flag("values").Changed
if valueFlagSet && len(args) > 2 {
return errors.Errorf("value should set either from flag or arg")
@@ -90,7 +90,7 @@ func (r *SetRunner) preRunE(c *cobra.Command, args []string) error {
r.Lookup.Name = args[1]
}
if isFlagSet("values", c) && len(r.Values) == 1 {
if valueFlagSet {
r.Perform.Value = r.Values[0]
} else if len(args) > 2 {
r.Perform.Value = args[2]
@@ -136,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) > 2 || isFlagSet("values", c) {
if len(args) > 2 || c.Flag("values").Changed {
return handleError(c, r.perform(c, args))
}
return handleError(c, lookup(r.Lookup, c, args))

View File

@@ -10,7 +10,6 @@ import (
"github.com/go-errors/errors"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
)
// parseFieldPath parse a flag value into a field path
@@ -40,16 +39,6 @@ func parseFieldPath(path string) ([]string, error) {
return newParts, nil
}
func isFlagSet(name string, c *cobra.Command) bool {
set := false
c.Flags().Visit(func(f *flag.Flag) {
if f.Name == name {
set = true
}
})
return set
}
func handleError(c *cobra.Command, err error) error {
if err == nil {
return nil