Skip to content

Commit 63acbeb

Browse files
arcanezarttor
authored andcommitted
make crd-dir and optional-crds mutually exclusive
1 parent 4a3e55e commit 63acbeb

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

cmd/helmify/flags.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"flag"
56
"fmt"
67
"os"
@@ -38,6 +39,9 @@ Flags:
3839

3940
type arrayFlags []string
4041

42+
var osExit = os.Exit
43+
var errMutuallyExclusiveCRDs = errors.New("-crd and -optional-crds cannot be used together")
44+
4145
func (i *arrayFlags) String() string {
4246
if i == nil || len(*i) == 0 {
4347
return ""
@@ -51,16 +55,16 @@ func (i *arrayFlags) Set(value string) error {
5155
}
5256

5357
// ReadFlags command-line flags into app config.
54-
func ReadFlags() config.Config {
58+
func ReadFlags() (config.Config, error) {
5559
files := arrayFlags{}
5660
result := config.Config{}
57-
var h, help, version, crd, preservens bool
61+
var h, help, version bool
5862
flag.BoolVar(&h, "h", false, "Print help. Example: helmify -h")
5963
flag.BoolVar(&help, "help", false, "Print help. Example: helmify -help")
6064
flag.BoolVar(&version, "version", false, "Print helmify version. Example: helmify -version")
6165
flag.BoolVar(&result.Verbose, "v", false, "Enable verbose output (print WARN & INFO). Example: helmify -v")
6266
flag.BoolVar(&result.VeryVerbose, "vv", false, "Enable very verbose output. Same as verbose but with DEBUG. Example: helmify -vv")
63-
flag.BoolVar(&crd, "crd-dir", false, "Enable crd install into 'crds' directory.\nWarning: CRDs placed in 'crds' directory will not be templated by Helm.\nSee https://helm.sh/docs/chart_best_practices/custom_resource_definitions/#some-caveats-and-explanations\nExample: helmify -crd-dir")
67+
flag.BoolVar(&result.Crd, "crd-dir", false, "Enable crd install into 'crds' directory. (cannot be used with 'optional-crds').\nWarning: CRDs placed in 'crds' directory will not be templated by Helm.\nSee https://helm.sh/docs/chart_best_practices/custom_resource_definitions/#some-caveats-and-explanations\nExample: helmify -crd-dir")
6468
flag.BoolVar(&result.ImagePullSecrets, "image-pull-secrets", false, "Allows the user to use existing secrets as imagePullSecrets in values.yaml.")
6569
flag.BoolVar(&result.GenerateDefaults, "generate-defaults", false, "Allows the user to add empty placeholders for typical customization options in values.yaml. Currently covers: topology constraints, node selectors, tolerances")
6670
flag.BoolVar(&result.CertManagerAsSubchart, "cert-manager-as-subchart", false, "Allows the user to add cert-manager as a subchart")
@@ -69,31 +73,29 @@ func ReadFlags() config.Config {
6973
flag.BoolVar(&result.FilesRecursively, "r", false, "Scan dirs from -f option recursively")
7074
flag.BoolVar(&result.OriginalName, "original-name", false, "Use the object's original name instead of adding the chart's release name as the common prefix.")
7175
flag.Var(&files, "f", "File or directory containing k8s manifests.")
72-
flag.BoolVar(&preservens, "preserve-ns", false, "Use the object's original namespace instead of adding all the resources to a common namespace.")
76+
flag.BoolVar(&result.PreserveNs, "preserve-ns", false, "Use the object's original namespace instead of adding all the resources to a common namespace.")
7377
flag.BoolVar(&result.AddWebhookOption, "add-webhook-option", false, "Allows the user to add webhook option in values.yaml.")
74-
flag.BoolVar(&result.OptionalCRDs, "optional-crds", false, "Enable optional CRD installation through values.")
78+
flag.BoolVar(&result.OptionalCRDs, "optional-crds", false, "Enable optional CRD installation through values. (cannot be used with 'crd-dir')")
7579

7680
flag.Parse()
7781
if h || help {
7882
fmt.Print(helpText)
83+
flag.CommandLine.SetOutput(os.Stdout)
7984
flag.PrintDefaults()
80-
os.Exit(0)
85+
osExit(0)
8186
}
8287
if version {
8388
printVersion()
84-
os.Exit(0)
89+
osExit(0)
8590
}
8691
name := flag.Arg(0)
8792
if name != "" {
8893
result.ChartName = filepath.Base(name)
8994
result.ChartDir = filepath.Dir(name)
9095
}
91-
if crd {
92-
result.Crd = crd
93-
}
94-
if preservens {
95-
result.PreserveNs = true
96+
if result.Crd && result.OptionalCRDs {
97+
return config.Config{}, errMutuallyExclusiveCRDs
9698
}
9799
result.Files = files
98-
return result
100+
return result, nil
99101
}

cmd/helmify/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package main
22

33
import (
4+
"flag"
5+
"fmt"
46
"os"
57

68
"github.com/arttor/helmify/pkg/app"
79
"github.com/sirupsen/logrus"
810
)
911

1012
func main() {
11-
conf := ReadFlags()
13+
conf, err := ReadFlags()
14+
if err != nil {
15+
fmt.Println(err)
16+
flag.Usage()
17+
os.Exit(1)
18+
}
1219
stat, err := os.Stdin.Stat()
1320
if err != nil {
1421
logrus.WithError(err).Error("stdin error")

0 commit comments

Comments
 (0)