diff --git a/cmd/mindthegap/create/helmbundle/helm_bundle.go b/cmd/mindthegap/create/helmbundle/helm_bundle.go index acde0d26..1378ffd6 100644 --- a/cmd/mindthegap/create/helmbundle/helm_bundle.go +++ b/cmd/mindthegap/create/helmbundle/helm_bundle.go @@ -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" @@ -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") diff --git a/cmd/mindthegap/create/imagebundle/image_bundle.go b/cmd/mindthegap/create/imagebundle/image_bundle.go index 934390b3..73a0d684 100644 --- a/cmd/mindthegap/create/imagebundle/image_bundle.go +++ b/cmd/mindthegap/create/imagebundle/image_bundle.go @@ -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" @@ -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") diff --git a/cmd/mindthegap/flags/validate.go b/cmd/mindthegap/flags/validate.go new file mode 100644 index 00000000..acf8ac40 --- /dev/null +++ b/cmd/mindthegap/flags/validate.go @@ -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 { + 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 +} diff --git a/cmd/mindthegap/importcmd/imagebundle/image_bundle.go b/cmd/mindthegap/importcmd/imagebundle/image_bundle.go index 94321746..63eadaae 100644 --- a/cmd/mindthegap/importcmd/imagebundle/image_bundle.go +++ b/cmd/mindthegap/importcmd/imagebundle/image_bundle.go @@ -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" @@ -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() diff --git a/cmd/mindthegap/push/bundle/bundle.go b/cmd/mindthegap/push/bundle/bundle.go index 4fde30b2..033c835a 100644 --- a/cmd/mindthegap/push/bundle/bundle.go +++ b/cmd/mindthegap/push/bundle/bundle.go @@ -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() diff --git a/cmd/mindthegap/serve/bundle/bundle.go b/cmd/mindthegap/serve/bundle/bundle.go index 85c5cb1f..933555de 100644 --- a/cmd/mindthegap/serve/bundle/bundle.go +++ b/cmd/mindthegap/serve/bundle/bundle.go @@ -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" @@ -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()