diff --git a/cmd/config/internal/commands/grep.go b/cmd/config/internal/commands/grep.go index 54659f5100..9e2f4b47de 100644 --- a/cmd/config/internal/commands/grep.go +++ b/cmd/config/internal/commands/grep.go @@ -54,6 +54,9 @@ type GrepRunner struct { } func (r *GrepRunner) preRunE(c *cobra.Command, args []string) error { + if len(args) == 0 { + return fmt.Errorf("missing required argument: QUERY") + } r.GrepFilter.Compare = func(a, b string) (int, error) { qa, err := resource.ParseQuantity(a) if err != nil { diff --git a/cmd/config/internal/commands/grep_test.go b/cmd/config/internal/commands/grep_test.go index dddcbbdb39..3043f7a8a6 100644 --- a/cmd/config/internal/commands/grep_test.go +++ b/cmd/config/internal/commands/grep_test.go @@ -421,3 +421,17 @@ spec: }) } } + +// TestGrepCmd_noQuery verifies the grep command errors when QUERY argument is missing +func TestGrepCmd_noQuery(t *testing.T) { + b := &bytes.Buffer{} + r := commands.GetGrepRunner("") + // No QUERY argument + r.Command.SetArgs([]string{}) + r.Command.SetOut(b) + + err := r.Command.Execute() + if assert.Error(t, err) { + assert.Contains(t, err.Error(), "missing required argument: QUERY") + } +}