Skip to content

Commit

Permalink
fixup! refactor: Rename func and better handle slice values
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmidyson committed Oct 25, 2023
1 parent 201f88d commit a59e49a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/mindthegap/create/helmbundle/helm_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewCommand(out output.Output) *cobra.Command {
return err
}

if err := flags.ValidateRequiredFlagValues(cmd, "helm-charts-file"); err != nil {
if err := flags.ValidateFlagsThatRequireValues(cmd, "helm-charts-file"); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/mindthegap/create/imagebundle/image_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewCommand(out output.Output) *cobra.Command {
return err
}

if err := flags.ValidateRequiredFlagValues(cmd, "images-file"); err != nil {
if err := flags.ValidateFlagsThatRequireValues(cmd, "images-file"); err != nil {
return err
}

Expand Down
19 changes: 15 additions & 4 deletions cmd/mindthegap/flags/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/utils/strings/slices"
)

func ValidateRequiredFlagValues(cmd *cobra.Command, requiredFlagsWithValues ...string) error {
func ValidateFlagsThatRequireValues(cmd *cobra.Command, requiredFlagsWithValues ...string) error {
fs := cmd.Flags()

missingFlagValues := []string{}
Expand All @@ -20,13 +22,22 @@ func ValidateRequiredFlagValues(cmd *cobra.Command, requiredFlagsWithValues ...s
continue
}

if foundFlag.Value.String() == "" || foundFlag.Value.String() == "[]" {
missingFlagValues = append(missingFlagValues, flagName)
if sv, ok := foundFlag.Value.(pflag.SliceValue); ok {
if len(slices.Filter(nil, sv.GetSlice(), func(s string) bool { return s != "" })) == 0 {
missingFlagValues = append(missingFlagValues, flagName)
}
} else {
if foundFlag.Value.String() == "" {
missingFlagValues = append(missingFlagValues, flagName)
}
}
}

if len(missingFlagValues) > 0 {
return fmt.Errorf(`required flag value(s) "%s" not set`, strings.Join(missingFlagValues, `", "`))
return fmt.Errorf(
`the following flags require value(s) to be specified: "%s"`,
strings.Join(missingFlagValues, `", "`),
)
}
return nil
}
2 changes: 1 addition & 1 deletion cmd/mindthegap/importcmd/imagebundle/image_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewCommand(out output.Output) *cobra.Command {
return err
}

if err := flags.ValidateRequiredFlagValues(cmd, "image-bundle"); err != nil {
if err := flags.ValidateFlagsThatRequireValues(cmd, "image-bundle"); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/mindthegap/push/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewCommand(out output.Output, bundleCmdName string) *cobra.Command {
return err
}

if err := flags.ValidateRequiredFlagValues(cmd, bundleCmdName, "to-registry"); err != nil {
if err := flags.ValidateFlagsThatRequireValues(cmd, bundleCmdName, "to-registry"); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/mindthegap/serve/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewCommand(
return err
}

if err := flags.ValidateRequiredFlagValues(cmd, bundleCmdName); err != nil {
if err := flags.ValidateFlagsThatRequireValues(cmd, bundleCmdName); err != nil {
return err
}

Expand Down

0 comments on commit a59e49a

Please sign in to comment.