Skip to content

Commit

Permalink
rpk: add cobra.NoArg to parent functions.
Browse files Browse the repository at this point in the history
Cobra won't recognize unknown commands unless
cobra.NoArg is set as an Arg validation parameter
in the command definition.

This means that running 'rpk cluster unknowncommand'
won't return a 'unknown command' error, therefore,
exit as a successful command execution.

Now, cobra will return an 'unknown error' and exit
with code 1.
  • Loading branch information
r-vasquez committed May 23, 2024
1 parent 6cbda3b commit d1e7f6a
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/go/rpk/pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ func Execute() {
}
})

// Cobra does not return an 'unknown command' error unless cobra.NoArgs is
// specified.
// See: https://github.com/spf13/cobra/issues/706
cobraext.Walk(root, func(c *cobra.Command) {
if c.Args == nil && c.HasSubCommands() {
c.Args = cobra.NoArgs
}
})

cobra.AddTemplateFunc("wrappedLocalFlagUsages", wrappedLocalFlagUsages)
cobra.AddTemplateFunc("wrappedGlobalFlagUsages", wrappedGlobalFlagUsages)
root.SetUsageTemplate(usageTemplate)
Expand Down

0 comments on commit d1e7f6a

Please sign in to comment.