Skip to content

Commit

Permalink
Address fishfood feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
phanimarupaka committed Dec 4, 2020
1 parent 1387f08 commit dd27ed3
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 88 deletions.
8 changes: 8 additions & 0 deletions commands/configcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package commands

import (
"fmt"
"os"

"github.com/GoogleContainerTools/kpt/internal/cmdsearch"
"github.com/GoogleContainerTools/kpt/internal/docs/generated/cfgdocs"
"github.com/GoogleContainerTools/kpt/internal/util/functions"
"github.com/GoogleContainerTools/kpt/internal/util/setters"
Expand Down Expand Up @@ -100,13 +102,19 @@ func GetConfigCommand(name string) *cobra.Command {

set := SetCommand(name)

search := cmdsearch.SearchCommand(name)

tree := configcobra.Tree(name)
tree.Short = cfgdocs.TreeShort
tree.Long = cfgdocs.TreeShort + "\n" + cfgdocs.TreeLong
tree.Example = cfgdocs.TreeExamples

cfgCmd.AddCommand(an, cat, count, createSetter, deleteSetter, deleteSubstitution, createSubstitution, fmt,
grep, listSetters, set, tree)

if enableSearchCmd := os.Getenv("KPT_ENABLE_SEARCH_CMD"); enableSearchCmd != "" {
cfgCmd.AddCommand(search)
}
return cfgCmd
}

Expand Down
31 changes: 23 additions & 8 deletions internal/cmdsearch/cmdsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package cmdsearch
import (
"fmt"
"io"
"io/ioutil"
"path/filepath"

"github.com/GoogleContainerTools/kpt/internal/util/search"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -73,6 +75,8 @@ type SearchRunner struct {
PutLiteral string
PutPattern string
RecurseSubPackages bool
MatchCount int
Writer io.Writer
}

func (r *SearchRunner) preRunE(c *cobra.Command, args []string) error {
Expand All @@ -86,21 +90,34 @@ func (r *SearchRunner) preRunE(c *cobra.Command, args []string) error {
c.Flag("by-value-regex").Changed {
return errors.Errorf(`only one of ["by-value", "by-value-regex"] can be provided`)
}
r.Writer = c.OutOrStdout()
return nil
}

func (r *SearchRunner) runE(c *cobra.Command, args []string) error {
e := runner.ExecuteCmdOnPkgs{
Writer: c.OutOrStdout(),
Writer: ioutil.Discard, // dummy writer, runner need not print any info
RecurseSubPackages: r.RecurseSubPackages,
CmdRunner: r,
RootPkgPath: args[0],
NeedOpenAPI: true,
SkipPkgPathPrint: true,
}
return e.Execute()
err := e.Execute()
if err != nil {
return err
}
var action string
if r.PutPattern != "" || r.PutLiteral != "" {
action = "Mutated"
} else {
action = "Matched"
}
fmt.Fprintf(r.Writer, "%s %d field(s)\n", action, r.MatchCount)
return nil
}

func (r *SearchRunner) ExecuteCmd(w io.Writer, pkgPath string) error {
func (r *SearchRunner) ExecuteCmd(_ io.Writer, pkgPath string) error {
s := search.SearchReplace{
ByValue: r.ByValue,
ByValueRegex: r.ByValueRegex,
Expand All @@ -111,11 +128,9 @@ func (r *SearchRunner) ExecuteCmd(w io.Writer, pkgPath string) error {
PackagePath: pkgPath,
}
err := s.Perform(pkgPath)
fmt.Fprintf(w, "matched %d field(s)\n", s.Count)
for filePath, nodeVals := range s.Match {
for _, nodeVal := range nodeVals {
fmt.Fprintf(w, "%s: %s\n", filePath, nodeVal)
}
r.MatchCount += s.Count
for _, res := range s.Result {
fmt.Fprintf(r.Writer, "%s\nfieldPath: %s\nvalue: %s\n\n", filepath.Join(pkgPath, res.FilePath), res.FieldPath, res.Value)
}
return errors.Wrap(err)
}
34 changes: 17 additions & 17 deletions internal/cmdsearch/cmdsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/cmd/config/ext"
"sigs.k8s.io/kustomize/kyaml/copyutil"
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
)

type test struct {
Expand Down Expand Up @@ -134,39 +135,38 @@ func TestSearchSubPackages(t *testing.T) {
name: "search-replace-recurse-subpackages",
dataset: "dataset-with-autosetters",
args: []string{"--by-value", "myspace", "--put-literal", "otherspace"},
out: `${baseDir}/mysql/
matched 1 field(s)
deployment.yaml: metadata.namespace: otherspace
out: `${baseDir}/mysql/deployment.yaml
fieldPath: metadata.namespace
value: otherspace # {"$openapi":"gcloud.core.project"}
${baseDir}/mysql/nosetters/
matched 1 field(s)
deployment.yaml: metadata.namespace: otherspace
${baseDir}/mysql/nosetters/deployment.yaml
fieldPath: metadata.namespace
value: otherspace
${baseDir}/mysql/storage/
matched 1 field(s)
deployment.yaml: metadata.namespace: otherspace
${baseDir}/mysql/storage/deployment.yaml
fieldPath: metadata.namespace
value: otherspace # {"$openapi":"gcloud.core.project"}
Mutated 3 field(s)
`,
},
{
name: "search-recurse-subpackages",
dataset: "dataset-with-autosetters",
args: []string{"--by-value", "mysql"},
out: `${baseDir}/mysql/
matched 1 field(s)
deployment.yaml: spec.template.spec.containers[0].name: mysql
${baseDir}/mysql/nosetters/
matched 0 field(s)
out: `${baseDir}/mysql/deployment.yaml
fieldPath: spec.template.spec.containers[0].name
value: mysql
${baseDir}/mysql/storage/
matched 0 field(s)
Matched 1 field(s)
`,
},
}
for i := range tests {
test := tests[i]
t.Run(test.name, func(t *testing.T) {
testDataDir := filepath.Join("../", "testutil", "testdata")
fieldmeta.SetShortHandRef("$kpt-set")
sourceDir := filepath.Join(testDataDir, test.dataset)
baseDir, err := ioutil.TempDir("", "")
if !assert.NoError(t, err) {
Expand Down
37 changes: 24 additions & 13 deletions internal/cmdsearch/putpattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ openAPI:
setter:
name: replicas
value: "3"`,
out: `${baseDir}/
matched 1 field(s)
${filePath}: spec.replicas: 3
out: `${baseDir}/${filePath}
fieldPath: spec.replicas
value: 3 # {"$kpt-set":"${replicas}"}
Mutated 1 field(s)
`,
expectedResources: `
apiVersion: apps/v1
Expand Down Expand Up @@ -73,9 +75,11 @@ openAPI:
setter:
name: kind
value: "deployment"`,
out: `${baseDir}/
matched 1 field(s)
${filePath}: metadata.name: nginx-deployment
out: `${baseDir}/${filePath}
fieldPath: metadata.name
value: nginx-deployment # {"$kpt-set":"${image}-${kind}"}
Mutated 1 field(s)
`,
expectedResources: `
apiVersion: apps/v1
Expand Down Expand Up @@ -107,10 +111,15 @@ openAPI:
setter:
name: project
value: "my-project"`,
out: `${baseDir}/
matched 2 field(s)
${filePath}: metadata.name: my-project-deployment
${filePath}: metadata.namespace: my-project-namespace
out: `${baseDir}/${filePath}
fieldPath: metadata.name
value: my-project-deployment # {"$kpt-set":"${project}-deployment"}
${baseDir}/${filePath}
fieldPath: metadata.namespace
value: my-project-namespace # {"$kpt-set":"${project}-namespace"}
Mutated 2 field(s)
`,
expectedResources: `
apiVersion: apps/v1
Expand Down Expand Up @@ -157,9 +166,11 @@ openAPI:
setter:
name: namespace
value: "my-space"`,
out: `${baseDir}/
matched 1 field(s)
${filePath}: metadata.name: dev/my-project/nginx
out: `${baseDir}/${filePath}
fieldPath: metadata.name
value: dev/my-project/nginx # {"$kpt-set":"${env}/${project}/${name}"}
Mutated 1 field(s)
`,
expectedResources: `
apiVersion: apps/v1
Expand Down
Loading

0 comments on commit dd27ed3

Please sign in to comment.