diff --git a/cmd/config/internal/commands/annotate_test.go b/cmd/config/internal/commands/annotate_test.go index 20e99d71c..ef855ae70 100644 --- a/cmd/config/internal/commands/annotate_test.go +++ b/cmd/config/internal/commands/annotate_test.go @@ -501,7 +501,10 @@ func TestAnnotateSubPackages(t *testing.T) { name: "annotate-recurse-subpackages", dataset: "dataset-without-setters", args: []string{"--kv", "foo=bar", "-R"}, - expected: `${baseDir}/mysql/ + expected: `${baseDir}/ +added annotations in the package + +${baseDir}/mysql/ added annotations in the package ${baseDir}/mysql/storage/ diff --git a/cmd/config/internal/commands/cat.go b/cmd/config/internal/commands/cat.go index a013816ad..0e3f860d2 100644 --- a/cmd/config/internal/commands/cat.go +++ b/cmd/config/internal/commands/cat.go @@ -4,9 +4,11 @@ package commands import ( + "bytes" "fmt" "io" "os" + "strings" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/cmd/config/ext" @@ -96,8 +98,10 @@ func (r *CatRunner) runE(c *cobra.Command, args []string) error { return handleError(c, kio.Pipeline{Inputs: []kio.Reader{input}, Filters: r.catFilters(), Outputs: outputs}.Execute()) } + out := &bytes.Buffer{} + e := executeCmdOnPkgs{ - writer: writer, + writer: out, needOpenAPI: false, recurseSubPackages: r.RecurseSubPackages, cmdRunner: r, @@ -105,12 +109,21 @@ func (r *CatRunner) runE(c *cobra.Command, args []string) error { skipPkgPathPrint: true, } - return e.execute() + err := e.execute() + if err != nil { + return err + } + + res := strings.TrimSuffix(out.String(), "---") + fmt.Fprintf(writer, "%s", res) + + return nil } func (r *CatRunner) executeCmd(w io.Writer, pkgPath string) error { input := kio.LocalPackageReader{PackagePath: pkgPath, PackageFileName: ext.KRMFileName()} - outputs, err := r.out(w) + out := &bytes.Buffer{} + outputs, err := r.out(out) if err != nil { return err } @@ -129,7 +142,10 @@ func (r *CatRunner) executeCmd(w io.Writer, pkgPath string) error { fmt.Fprintf(w, "%s in package %q\n", err.Error(), pkgPath) } } - fmt.Fprintf(w, "---") + fmt.Fprint(w, out.String()) + if out.String() != "" { + fmt.Fprint(w, "---") + } return nil } diff --git a/cmd/config/internal/commands/cat_test.go b/cmd/config/internal/commands/cat_test.go index 8ecfebbf4..c9b1ec024 100644 --- a/cmd/config/internal/commands/cat_test.go +++ b/cmd/config/internal/commands/cat_test.go @@ -115,7 +115,7 @@ metadata: app: nginx spec: replicas: 3 ----`, b.String()) { +`, b.String()) { return } } @@ -225,7 +225,7 @@ metadata: app: nginx spec: replicas: 3 ----`, b.String()) { +`, b.String()) { return } } @@ -307,7 +307,7 @@ metadata: image: gcr.io/example/reconciler:v1 spec: replicas: 3 ----`, b.String()) { +`, b.String()) { return } } @@ -421,7 +421,7 @@ metadata: app: nginx spec: replicas: 3 ----`, string(actual)) { +`, string(actual)) { return } } @@ -536,7 +536,7 @@ metadata: app: nginx spec: replicas: 3 ----`, string(actual)) { +`, string(actual)) { return } } @@ -552,7 +552,8 @@ func TestCatSubPackages(t *testing.T) { { name: "cat-recurse-subpackages", dataset: "dataset-without-setters", - expected: `# Copyright 2019 The Kubernetes Authors. + expected: ` +# Copyright 2019 The Kubernetes Authors. # SPDX-License-Identifier: Apache-2.0 apiVersion: apps/v1 @@ -583,7 +584,7 @@ spec: containers: - name: storage image: storage:1.7.7 ----`, +`, }, { name: "cat-top-level-pkg-no-recurse-subpackages", @@ -605,7 +606,7 @@ spec: containers: - name: mysql image: mysql:1.7.9 ----`, +`, }, { name: "cat-nested-pkg-no-recurse-subpackages", @@ -627,7 +628,7 @@ spec: containers: - name: storage image: storage:1.7.7 ----`, +`, }, } for i := range tests { diff --git a/cmd/config/internal/commands/count_test.go b/cmd/config/internal/commands/count_test.go index 78c4ddc98..c6d4a959b 100644 --- a/cmd/config/internal/commands/count_test.go +++ b/cmd/config/internal/commands/count_test.go @@ -86,7 +86,9 @@ func TestCountSubPackages(t *testing.T) { { name: "count-recurse-subpackages", dataset: "dataset-without-setters", - expected: `${baseDir}/mysql/ + expected: `${baseDir}/ + +${baseDir}/mysql/ Deployment: 1 ${baseDir}/mysql/storage/ diff --git a/cmd/config/internal/commands/fmt_test.go b/cmd/config/internal/commands/fmt_test.go index 8286ef855..9aba4fcf9 100644 --- a/cmd/config/internal/commands/fmt_test.go +++ b/cmd/config/internal/commands/fmt_test.go @@ -179,7 +179,10 @@ func TestFmtSubPackages(t *testing.T) { name: "fmt-recurse-subpackages", dataset: "dataset-with-setters", args: []string{"-R"}, - expected: `${baseDir}/mysql/ + expected: `${baseDir}/ +formatted resource files in the package + +${baseDir}/mysql/ formatted resource files in the package ${baseDir}/mysql/nosetters/ diff --git a/cmd/config/internal/commands/grep.go b/cmd/config/internal/commands/grep.go index 5778e57ab..ef3a67608 100644 --- a/cmd/config/internal/commands/grep.go +++ b/cmd/config/internal/commands/grep.go @@ -4,6 +4,7 @@ package commands import ( + "bytes" "fmt" "io" "strings" @@ -114,8 +115,10 @@ func (r *GrepRunner) runE(c *cobra.Command, args []string) error { }.Execute()) } + out := bytes.Buffer{} + e := executeCmdOnPkgs{ - writer: c.OutOrStdout(), + writer: &out, needOpenAPI: false, recurseSubPackages: r.RecurseSubPackages, cmdRunner: r, @@ -123,18 +126,26 @@ func (r *GrepRunner) runE(c *cobra.Command, args []string) error { skipPkgPathPrint: true, } - return e.execute() + err := e.execute() + if err != nil { + return err + } + + res := strings.TrimSuffix(out.String(), "---") + fmt.Fprintf(c.OutOrStdout(), "%s", res) + + return nil } func (r *GrepRunner) executeCmd(w io.Writer, pkgPath string) error { input := kio.LocalPackageReader{PackagePath: pkgPath, PackageFileName: ext.KRMFileName()} - + out := &bytes.Buffer{} err := kio.Pipeline{ Inputs: []kio.Reader{input}, Filters: []kio.Filter{r.GrepFilter}, Outputs: []kio.Writer{kio.ByteWriter{ - Writer: w, + Writer: out, KeepReaderAnnotations: r.KeepAnnotations, }}, }.Execute() @@ -148,6 +159,9 @@ func (r *GrepRunner) executeCmd(w io.Writer, pkgPath string) error { fmt.Fprintf(w, "%s\n", err.Error()) } } - fmt.Fprintf(w, "---") + fmt.Fprint(w, out.String()) + if out.String() != "" { + fmt.Fprint(w, "---") + } return nil } diff --git a/cmd/config/internal/commands/grep_test.go b/cmd/config/internal/commands/grep_test.go index 41f606fc6..0c3ac5435 100644 --- a/cmd/config/internal/commands/grep_test.go +++ b/cmd/config/internal/commands/grep_test.go @@ -283,7 +283,8 @@ func TestGrepSubPackages(t *testing.T) { name: "grep-recurse-subpackages", dataset: "dataset-without-setters", args: []string{"kind=Deployment"}, - expected: `# Copyright 2019 The Kubernetes Authors. + expected: ` +# Copyright 2019 The Kubernetes Authors. # SPDX-License-Identifier: Apache-2.0 apiVersion: apps/v1 @@ -320,7 +321,7 @@ spec: containers: - name: storage image: storage:1.7.7 ----`, +`, }, { name: "grep-top-level-pkg-no-recurse-subpackages", @@ -345,7 +346,7 @@ spec: containers: - name: mysql image: mysql:1.7.9 ----`, +`, }, { name: "grep-nested-pkg-no-recurse-subpackages", @@ -370,7 +371,15 @@ spec: containers: - name: storage image: storage:1.7.7 ----`, +`, + }, + { + name: "grep-recurse-subpackages-no-result", + dataset: "dataset-without-setters", + args: []string{"kind=ConfigMap"}, + expected: ` + +`, }, } for i := range tests { diff --git a/cmd/config/internal/commands/util.go b/cmd/config/internal/commands/util.go index 5a2c28e44..76b2df87d 100644 --- a/cmd/config/internal/commands/util.go +++ b/cmd/config/internal/commands/util.go @@ -53,6 +53,12 @@ func (e executeCmdOnPkgs) execute() error { pkgsPaths = []string{e.rootPkgPath} } + // for commands which doesn't need openAPI file, make sure that the root package is + // included all the times + if !e.needOpenAPI && !containsString(pkgsPaths, e.rootPkgPath) { + pkgsPaths = append([]string{e.rootPkgPath}, pkgsPaths...) + } + for i := range pkgsPaths { pkgPath := pkgsPaths[i] // Add schema present in openAPI file for current package @@ -145,3 +151,13 @@ func fixDocs(new string, c *cobra.Command) { c.Long = strings.ReplaceAll(c.Long, cmdName, new) c.Example = strings.ReplaceAll(c.Example, cmdName, new) } + +// containsString returns true if slice contains s +func containsString(slice []string, s string) bool { + for _, item := range slice { + if item == s { + return true + } + } + return false +}