Skip to content

Commit

Permalink
feat: Validate empty values for required flags
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmidyson committed Oct 24, 2023
1 parent 578497a commit 201f88d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
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.ValidateRequiredFlagValues(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.ValidateRequiredFlagValues(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
32 changes: 32 additions & 0 deletions cmd/mindthegap/flags/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package flags

import (
"fmt"
"strings"

"github.com/spf13/cobra"
)

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

missingFlagValues := []string{}
for _, flagName := range requiredFlagsWithValues {
foundFlag := fs.Lookup(flagName)
if foundFlag == nil {
continue
}

if foundFlag.Value.String() == "" || 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 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.ValidateRequiredFlagValues(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.ValidateRequiredFlagValues(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.ValidateRequiredFlagValues(cmd, bundleCmdName); err != nil {
return err
}

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

0 comments on commit 201f88d

Please sign in to comment.