From 594a06d35bd004b3fc641912e3d20d21837b5304 Mon Sep 17 00:00:00 2001 From: Richard Marshall Date: Wed, 21 Aug 2019 08:59:21 -0700 Subject: [PATCH] Fixes to create sub-command --- pkg/commands/create/create.go | 16 ++++++++++------ pkg/commands/create/create_test.go | 12 ++++++++++++ pkg/commands/util/util.go | 3 +++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pkg/commands/create/create.go b/pkg/commands/create/create.go index 4dcbb516d..a823f553c 100644 --- a/pkg/commands/create/create.go +++ b/pkg/commands/create/create.go @@ -63,12 +63,12 @@ func NewCmdCreate(fSys fs.FileSystem, uf ifc.KunstructuredFactory) *cobra.Comman "Set the value of the namespace field in the customization file.") c.Flags().StringVar( &opts.annotations, - "annotation", + "annotations", "", "Add one or more common annotations.") c.Flags().StringVar( &opts.labels, - "label", + "labels", "", "Add one or more common labels.") c.Flags().StringVar( @@ -95,11 +95,15 @@ func NewCmdCreate(fSys fs.FileSystem, uf ifc.KunstructuredFactory) *cobra.Comman } func runCreate(opts createFlags, fSys fs.FileSystem, uf ifc.KunstructuredFactory) error { - resources, err := util.GlobPatterns(fSys, strings.Split(opts.resources, ",")) - if err != nil { - return err + var resources []string + var err error + if opts.resources != "" { + resources, err = util.GlobPatterns(fSys, strings.Split(opts.resources, ",")) + if err != nil { + return err + } } - if _, err := kustfile.NewKustomizationFile(fSys); err == nil { + if _, err = kustfile.NewKustomizationFile(fSys); err == nil { return fmt.Errorf("kustomization file already exists") } if opts.detectResources { diff --git a/pkg/commands/create/create_test.go b/pkg/commands/create/create_test.go index 6bf1840db..00608e113 100644 --- a/pkg/commands/create/create_test.go +++ b/pkg/commands/create/create_test.go @@ -132,6 +132,12 @@ metadata: fakeFS.WriteFile("/README.md", []byte(` # Not a k8s resource This file is not a valid kubernetes object.`)) + fakeFS.WriteFile("/non-k8s.yaml", []byte(` +# Not a k8s resource +other: yaml +foo: +- bar +- baz`)) fakeFS.Mkdir("/sub") fakeFS.WriteFile("/sub/test.yaml", []byte(` apiVersion: v1 @@ -141,6 +147,12 @@ metadata: fakeFS.WriteFile("/sub/README.md", []byte(` # Not a k8s resource This file in a subdirectory is not a valid kubernetes object.`)) + fakeFS.WriteFile("/sub/non-k8s.yaml", []byte(` +# Not a k8s resource +other: yaml +foo: +- bar +- baz`)) fakeFS.Mkdir("/overlay") fakeFS.WriteFile("/overlay/test.yaml", []byte(` apiVersion: v1 diff --git a/pkg/commands/util/util.go b/pkg/commands/util/util.go index cc75353b0..96a077faf 100644 --- a/pkg/commands/util/util.go +++ b/pkg/commands/util/util.go @@ -33,6 +33,9 @@ func GlobPatterns(fsys fs.FileSystem, patterns []string) ([]string, error) { // `key:value` into a map. func ConvertToMap(input string, kind string) (map[string]string, error) { result := make(map[string]string) + if input == "" { + return result, nil + } inputs := strings.Split(input, ",") for _, input := range inputs { c := strings.Index(input, ":")