Skip to content

Commit 7cbaf78

Browse files
authored
fix:kustomize cfg grep with no arguments causes panic (#5707)
* fix:kustomize cfg grep with no arguments causes panic * add test for kustomize cfg grep with no arguments
1 parent 735ad0b commit 7cbaf78

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

cmd/config/internal/commands/grep.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ type GrepRunner struct {
5454
}
5555

5656
func (r *GrepRunner) preRunE(c *cobra.Command, args []string) error {
57+
if len(args) == 0 {
58+
return fmt.Errorf("missing required argument: QUERY")
59+
}
5760
r.GrepFilter.Compare = func(a, b string) (int, error) {
5861
qa, err := resource.ParseQuantity(a)
5962
if err != nil {

cmd/config/internal/commands/grep_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,17 @@ spec:
421421
})
422422
}
423423
}
424+
425+
// TestGrepCmd_noQuery verifies the grep command errors when QUERY argument is missing
426+
func TestGrepCmd_noQuery(t *testing.T) {
427+
b := &bytes.Buffer{}
428+
r := commands.GetGrepRunner("")
429+
// No QUERY argument
430+
r.Command.SetArgs([]string{})
431+
r.Command.SetOut(b)
432+
433+
err := r.Command.Execute()
434+
if assert.Error(t, err) {
435+
assert.Contains(t, err.Error(), "missing required argument: QUERY")
436+
}
437+
}

0 commit comments

Comments
 (0)