Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Validate empty values for required flags #541

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmd/mindthegap/create/helmbundle/helm_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/mesosphere/mindthegap/archive"
"github.com/mesosphere/mindthegap/cleanup"
"github.com/mesosphere/mindthegap/cmd/mindthegap/flags"
"github.com/mesosphere/mindthegap/cmd/mindthegap/utils"
"github.com/mesosphere/mindthegap/config"
"github.com/mesosphere/mindthegap/docker/registry"
Expand All @@ -34,6 +35,17 @@ func NewCommand(out output.Output) *cobra.Command {
cmd := &cobra.Command{
Use: "helm-bundle",
Short: "Create a Helm chart bundle",
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.ValidateRequiredFlags(); err != nil {
return err
}

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

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if !overwrite {
out.StartOperation("Checking if output file already exists")
Expand Down
12 changes: 12 additions & 0 deletions cmd/mindthegap/create/imagebundle/image_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/mesosphere/mindthegap/archive"
"github.com/mesosphere/mindthegap/cleanup"
"github.com/mesosphere/mindthegap/cmd/mindthegap/flags"
"github.com/mesosphere/mindthegap/cmd/mindthegap/utils"
"github.com/mesosphere/mindthegap/config"
"github.com/mesosphere/mindthegap/docker/registry"
Expand All @@ -43,6 +44,17 @@ func NewCommand(out output.Output) *cobra.Command {
cmd := &cobra.Command{
Use: "image-bundle",
Short: "Create an image bundle",
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.ValidateRequiredFlags(); err != nil {
return err
}

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

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if !overwrite {
out.StartOperation("Checking if output file already exists")
Expand Down
43 changes: 43 additions & 0 deletions cmd/mindthegap/flags/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package flags

import (
"fmt"
"strings"

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

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

missingFlagValues := []string{}
for _, flagName := range requiredFlagsWithValues {
foundFlag := fs.Lookup(flagName)
if foundFlag == nil {
jimmidyson marked this conversation as resolved.
Show resolved Hide resolved
continue
}

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(
`the following flags require value(s) to be specified: "%s"`,
strings.Join(missingFlagValues, `", "`),
)
}
return nil
}
12 changes: 12 additions & 0 deletions cmd/mindthegap/importcmd/imagebundle/image_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/cleanup"
"github.com/mesosphere/mindthegap/cmd/mindthegap/flags"
"github.com/mesosphere/mindthegap/cmd/mindthegap/utils"
"github.com/mesosphere/mindthegap/containerd"
"github.com/mesosphere/mindthegap/docker/registry"
Expand All @@ -34,6 +35,17 @@ func NewCommand(out output.Output) *cobra.Command {
cmd := &cobra.Command{
Use: "image-bundle",
Short: "Import images from image bundles into Containerd",
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.ValidateRequiredFlags(); err != nil {
return err
}

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

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
cleaner := cleanup.NewCleaner()
defer cleaner.Cleanup()
Expand Down
11 changes: 11 additions & 0 deletions cmd/mindthegap/push/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ func NewCommand(out output.Output, bundleCmdName string) *cobra.Command {
cmd := &cobra.Command{
Use: bundleCmdName,
Short: "Push from bundles into an existing OCI registry",
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.ValidateRequiredFlags(); err != nil {
return err
}

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

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
cleaner := cleanup.NewCleaner()
defer cleaner.Cleanup()
Expand Down
12 changes: 12 additions & 0 deletions cmd/mindthegap/serve/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/cleanup"
"github.com/mesosphere/mindthegap/cmd/mindthegap/flags"
"github.com/mesosphere/mindthegap/cmd/mindthegap/utils"
"github.com/mesosphere/mindthegap/config"
"github.com/mesosphere/mindthegap/docker/registry"
Expand All @@ -36,6 +37,17 @@ func NewCommand(
cmd = &cobra.Command{
Use: bundleCmdName,
Short: "Serve an OCI registry from previously created bundles",
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.ValidateRequiredFlags(); err != nil {
return err
}

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

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
cleaner := cleanup.NewCleaner()
defer cleaner.Cleanup()
Expand Down