Skip to content

Commit

Permalink
Merge pull request redpanda-data#18650 from r-vasquez/err-for-unknown
Browse files Browse the repository at this point in the history
rpk: add error for unknown command execution
  • Loading branch information
r-vasquez authored May 27, 2024
2 parents 2062ccb + d1e7f6a commit d8f01f3
Show file tree
Hide file tree
Showing 27 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/cloud/auth/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func newCreateCommand(_ afero.Fs, _ *config.Params) *cobra.Command {
cmd := &cobra.Command{
Use: "create [NAME]",
Short: "Create an rpk cloud auth",
Args: cobra.ExactArgs(1),
Hidden: true,
Run: func(*cobra.Command, []string) {
fmt.Println("'rpk cloud auth create' is deprecated as a no-op; use 'rpk cloud login' instead.")
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/cloud/auth/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func newRenameToCommand(_ afero.Fs, _ *config.Params) *cobra.Command {
Use: "rename-to [NAME]",
Short: "Rename the current rpk auth",
Aliases: []string{"rename"},
Args: cobra.ExactArgs(1),
Hidden: true,
Run: func(*cobra.Command, []string) {
fmt.Println("rename-to is deprecated, rpk now fully manages auth names.")
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/cloud/byoc/byoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func NewCommand(fs afero.Fs, p *config.Params, execFn func(string, []string) err
cmd := &cobra.Command{
Use: "byoc",
Short: "Manage a Redpanda cloud BYOC agent",
Args: cobra.MinimumNArgs(0), // This allows us to run "unknown" commands that live in the plugin.
Long: `Manage a Redpanda cloud BYOC agent
For BYOC, Redpanda installs an agent service in your owned cluster. The agent
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/cluster/config/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ command.
By default, low level tunables are excluded: use the '--all' flag
to include all properties including these low level tunables.
`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
p, err := p.LoadVirtualProfile(fs)
out.MaybeDie(err, "rpk unable to load config: %v", err)
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/cluster/config/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ corresponding 'export' command. This downloads the current cluster
configuration, calculates the difference with the YAML file, and
updates any properties that were changed. If a property is removed
from the YAML file, it is reset to its default value. `,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
p, err := p.LoadVirtualProfile(fs)
out.MaybeDie(err, "rpk unable to load config: %v", err)
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/cluster/config/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Deprecated content includes properties which were set via redpanda.yaml
in earlier versions of redpanda, but are now managed via Redpanda's
central configuration store (and via 'rpk cluster config edit').
`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, propertyNames []string) {
cfg, err := p.Load(fs)
out.MaybeDie(err, "rpk unable to load config: %v", err)
Expand Down
3 changes: 2 additions & 1 deletion src/go/rpk/pkg/cli/cluster/config/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Additionally show the version of cluster configuration that each node
has applied: under normal circumstances these should all be equal,
a lower number shows that a node is out of sync, perhaps because it
is offline.`,
Run: func(cmd *cobra.Command, args []string) {
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
p, err := p.LoadVirtualProfile(fs)
out.MaybeDie(err, "rpk unable to load config: %v", err)
config.CheckExitCloudAdmin(p)
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/cluster/partitions/unsafe_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ nodes are gone and irrecoverable; this may result in data loss.
You can perform a dry run and verify the partitions that will be recovered by
using the '--dry' flag.
`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
f := p.Formatter
if f.Kind != "text" && f.Kind != "help" && !dry {
Expand Down
3 changes: 2 additions & 1 deletion src/go/rpk/pkg/cli/cluster/storage/recovery/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func newStartCommand(fs afero.Fs, p *config.Params) *cobra.Command {
This command starts the process of restoring topics from the archival bucket.
If the wait flag (--wait/-w) is set, the command will poll the status of the
recovery process until it's finished.`,
Run: func(cmd *cobra.Command, args []string) {
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
p, err := p.LoadVirtualProfile(fs)
out.MaybeDie(err, "rpk unable to load config: %v", err)
config.CheckExitCloudAdmin(p)
Expand Down
3 changes: 2 additions & 1 deletion src/go/rpk/pkg/cli/cluster/storage/recovery/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func newStatusCommand(fs afero.Fs, p *config.Params) *cobra.Command {
This command fetches the status of the process of restoring topics from the
archival bucket.`,
Run: func(cmd *cobra.Command, args []string) {
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
p, err := p.LoadVirtualProfile(fs)
out.MaybeDie(err, "rpk unable to load config: %v", err)
config.CheckExitCloudAdmin(p)
Expand Down
1 change: 0 additions & 1 deletion src/go/rpk/pkg/cli/cluster/txn/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ format with print partitions included.
If no transactional IDs are requested, all transactional IDs are printed.
`,

Run: func(cmd *cobra.Command, txnIDs []string) {
f := p.Formatter
if h, ok := f.Help([]describeResponse{}); ok {
Expand Down
4 changes: 2 additions & 2 deletions src/go/rpk/pkg/cli/cluster/txn/describe_producers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ You can query all topics and partitions that have active producers with --all.
To filter for specific topics, use --topics. You can additionally filter by
partitions with --partitions.
`,

Run: func(cmd *cobra.Command, txnIDs []string) {
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
f := p.Formatter
if h, ok := f.Help([]describeProducersResponse{}); ok {
out.Exit(h)
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/container/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func newPurgeCommand(fs afero.Fs, p *config.Params) *cobra.Command {
command := &cobra.Command{
Use: "purge",
Short: "Stop and remove an existing local container cluster's data",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
c, err := common.NewDockerClient(cmd.Context())
out.MaybeDie(err, "unable to create docker client: %v", err)
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/container/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Start a 3-broker cluster, selecting every admin API port:
// (POSIX standard)
UnknownFlags: true,
},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
if nodes < 1 {
out.Die("--nodes should be 1 or greater")
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/container/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func newStatusCommand(fs afero.Fs, p *config.Params) *cobra.Command {
command := &cobra.Command{
Use: "status",
Short: "Get status",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
c, err := common.NewDockerClient(cmd.Context())
out.MaybeDieErr(err)
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/container/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func newStopCommand() *cobra.Command {
command := &cobra.Command{
Use: "stop",
Short: "Stop an existing local container cluster",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
c, err := common.NewDockerClient(cmd.Context())
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion src/go/rpk/pkg/cli/debug/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func NewCommand(fs afero.Fs, p *config.Params) *cobra.Command {
Use: "bundle",
Short: "Collect environment data and create a bundle file for the Redpanda Data support team to inspect",
Long: bundleHelpText,
Run: func(cmd *cobra.Command, args []string) {
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
// Redpanda queries for samples from Seastar every ~13 seconds by
// default. Setting wait_ms to anything less than 13 seconds will
// result in no samples being returned.
Expand Down
3 changes: 2 additions & 1 deletion src/go/rpk/pkg/cli/generate/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func newAppCmd(fs afero.Fs, p *config.Params) *cobra.Command {
Use: "app",
Short: "Generate a sample application to connect with Redpanda",
Long: appHelpText,
Run: func(cmd *cobra.Command, args []string) {
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
p, err := p.LoadVirtualProfile(fs)
out.MaybeDie(err, "rpk unable to load config: %v", err)

Expand Down
3 changes: 2 additions & 1 deletion src/go/rpk/pkg/cli/generate/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ metrics endpoint used.
To see a list of all available dashboards, use the '--dashboard help' flag.
`,
Run: func(cmd *cobra.Command, args []string) {
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
switch {
case dashboard == "legacy":
if datasource == "" {
Expand Down
3 changes: 2 additions & 1 deletion src/go/rpk/pkg/cli/generate/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func newPrometheusConfigCmd(fs afero.Fs, p *config.Params) *cobra.Command {
Use: "prometheus-config",
Short: "Generate the Prometheus configuration to scrape Redpanda nodes",
Long: prometheusHelpText,
Run: func(cmd *cobra.Command, args []string) {
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
y, err := p.LoadVirtualRedpandaYaml(fs)
out.MaybeDie(err, "rpk unable to load config: %v", err)

Expand Down
3 changes: 2 additions & 1 deletion src/go/rpk/pkg/cli/iotune/iotune.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func NewCommand(fs afero.Fs, p *config.Params) *cobra.Command {
cmd := &cobra.Command{
Use: "iotune",
Short: "Measure filesystem performance and create IO configuration file",
Run: func(cmd *cobra.Command, args []string) {
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
timeout += duration
y, err := p.LoadVirtualRedpandaYaml(fs)
out.MaybeDie(err, "rpk unable to load config: %v", err)
Expand Down
3 changes: 2 additions & 1 deletion src/go/rpk/pkg/cli/redpanda/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func NewCheckCommand(fs afero.Fs, p *config.Params) *cobra.Command {
cmd := &cobra.Command{
Use: "check",
Short: "Check if system meets redpanda requirements",
Run: func(_ *cobra.Command, args []string) {
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
y, err := p.LoadVirtualRedpandaYaml(fs)
out.MaybeDie(err, "rpk unable to load config: %v", err)
err = executeCheck(fs, y, timeout)
Expand Down
3 changes: 2 additions & 1 deletion src/go/rpk/pkg/cli/redpanda/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func NewStopCommand(fs afero.Fs, p *config.Params) *cobra.Command {
first sends SIGINT, and waits for the specified timeout. Then, if redpanda
hasn't stopped, it sends SIGTERM. Lastly, it sends SIGKILL if it's still
running.`,
Run: func(_ *cobra.Command, args []string) {
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
y, err := p.LoadVirtualRedpandaYaml(fs)
out.MaybeDie(err, "rpk unable to load config: %v", err)

Expand Down
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
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/security/role/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Print only the members of role 'red'
Print only the ACL associated to the role 'red'
rpk security role describe red --print-permissions
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
f := p.Formatter
if h, ok := f.Help(describeResponse{}); ok {
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/security/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func newListUsersCommand(fs afero.Fs, p *config.Params) *cobra.Command {
Use: "list",
Aliases: []string{"ls"},
Short: "List SASL users",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
f := p.Formatter
if h, ok := f.Help([]string{}); ok {
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ version running on each node in your cluster.
To list the Redpanda version of each node in your cluster you may pass the
Admin API hosts via flags, profile, or environment variables.`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
rv := rpkVersion{
Version: version,
Expand Down

0 comments on commit d8f01f3

Please sign in to comment.