Skip to content

Commit 061d0cb

Browse files
committed
Move UnknownSubcommandAction to helpers
Signed-off-by: apostasie <[email protected]>
1 parent ec53014 commit 061d0cb

File tree

13 files changed

+56
-31
lines changed

13 files changed

+56
-31
lines changed

cmd/nerdctl/apparmor_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func newApparmorCommand() *cobra.Command {
2727
Annotations: map[string]string{helpers.Category: helpers.Management},
2828
Use: "apparmor",
2929
Short: "Manage AppArmor profiles",
30-
RunE: unknownSubcommandAction,
30+
RunE: helpers.UnknownSubcommandAction,
3131
SilenceUsage: true,
3232
SilenceErrors: true,
3333
}

cmd/nerdctl/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func newBuilderCommand() *cobra.Command {
3535
Annotations: map[string]string{helpers.Category: helpers.Management},
3636
Use: "builder",
3737
Short: "Manage builds",
38-
RunE: unknownSubcommandAction,
38+
RunE: helpers.UnknownSubcommandAction,
3939
SilenceUsage: true,
4040
SilenceErrors: true,
4141
}

cmd/nerdctl/compose.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ package main
1919
import (
2020
"github.com/spf13/cobra"
2121

22+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2223
"github.com/containerd/nerdctl/v2/pkg/composer"
2324
)
2425

2526
func newComposeCommand() *cobra.Command {
2627
var composeCommand = &cobra.Command{
2728
Use: "compose [flags] COMMAND",
2829
Short: "Compose",
29-
RunE: unknownSubcommandAction,
30+
RunE: helpers.UnknownSubcommandAction,
3031
SilenceUsage: true,
3132
SilenceErrors: true,
3233
TraverseChildren: true, // required for global short hands like -f

cmd/nerdctl/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func newContainerCommand() *cobra.Command {
2727
Annotations: map[string]string{helpers.Category: helpers.Management},
2828
Use: "container",
2929
Short: "Manage containers",
30-
RunE: unknownSubcommandAction,
30+
RunE: helpers.UnknownSubcommandAction,
3131
SilenceUsage: true,
3232
SilenceErrors: true,
3333
}

cmd/nerdctl/helpers/cobra.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package helpers
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
23+
"github.com/spf13/cobra"
24+
)
25+
26+
// UnknownSubcommandAction is needed to let `nerdctl system non-existent-command` fail
27+
// https://github.com/containerd/nerdctl/issues/487
28+
//
29+
// Ideally this should be implemented in Cobra itself.
30+
func UnknownSubcommandAction(cmd *cobra.Command, args []string) error {
31+
if len(args) == 0 {
32+
return cmd.Help()
33+
}
34+
// The output mimics https://github.com/spf13/cobra/blob/v1.2.1/command.go#L647-L662
35+
msg := fmt.Sprintf("unknown subcommand %q for %q", args[0], cmd.Name())
36+
if suggestions := cmd.SuggestionsFor(args[0]); len(suggestions) > 0 {
37+
msg += "\n\nDid you mean this?\n"
38+
for _, s := range suggestions {
39+
msg += fmt.Sprintf("\t%v\n", s)
40+
}
41+
}
42+
return errors.New(msg)
43+
}

cmd/nerdctl/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func newImageCommand() *cobra.Command {
2727
Annotations: map[string]string{helpers.Category: helpers.Management},
2828
Use: "image",
2929
Short: "Manage images",
30-
RunE: unknownSubcommandAction,
30+
RunE: helpers.UnknownSubcommandAction,
3131
SilenceUsage: true,
3232
SilenceErrors: true,
3333
}

cmd/nerdctl/ipfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func newIPFSCommand() *cobra.Command {
2727
Annotations: map[string]string{helpers.Category: helpers.Management},
2828
Use: "ipfs",
2929
Short: "Distributing images on IPFS",
30-
RunE: unknownSubcommandAction,
30+
RunE: helpers.UnknownSubcommandAction,
3131
SilenceUsage: true,
3232
SilenceErrors: true,
3333
}

cmd/nerdctl/ipfs_registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func newIPFSRegistryCommand() *cobra.Command {
2828
Use: "registry",
2929
Short: "Manage read-only registry backed by IPFS",
3030
PreRunE: checkExperimental("ipfs"),
31-
RunE: unknownSubcommandAction,
31+
RunE: helpers.UnknownSubcommandAction,
3232
SilenceUsage: true,
3333
SilenceErrors: true,
3434
}

cmd/nerdctl/main.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ Config file ($NERDCTL_TOML): %s
234234
}
235235
return nil
236236
}
237-
rootCmd.RunE = unknownSubcommandAction
237+
rootCmd.RunE = helpers.UnknownSubcommandAction
238238
rootCmd.AddCommand(
239239
newCreateCommand(),
240240
// #region Run & Exec
@@ -346,25 +346,6 @@ func globalFlags(cmd *cobra.Command) (string, []string) {
346346
return args0, args
347347
}
348348

349-
// unknownSubcommandAction is needed to let `nerdctl system non-existent-command` fail
350-
// https://github.com/containerd/nerdctl/issues/487
351-
//
352-
// Ideally this should be implemented in Cobra itself.
353-
func unknownSubcommandAction(cmd *cobra.Command, args []string) error {
354-
if len(args) == 0 {
355-
return cmd.Help()
356-
}
357-
// The output mimics https://github.com/spf13/cobra/blob/v1.2.1/command.go#L647-L662
358-
msg := fmt.Sprintf("unknown subcommand %q for %q", args[0], cmd.Name())
359-
if suggestions := cmd.SuggestionsFor(args[0]); len(suggestions) > 0 {
360-
msg += "\n\nDid you mean this?\n"
361-
for _, s := range suggestions {
362-
msg += fmt.Sprintf("\t%v\n", s)
363-
}
364-
}
365-
return errors.New(msg)
366-
}
367-
368349
// AddStringFlag is similar to cmd.Flags().String but supports aliases and env var
369350
func AddStringFlag(cmd *cobra.Command, name string, aliases []string, value string, env, usage string) {
370351
if env != "" {

cmd/nerdctl/namespace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func newNamespaceCommand() *cobra.Command {
4040
Aliases: []string{"ns"},
4141
Short: "Manage containerd namespaces",
4242
Long: "Unrelated to Linux namespaces and Kubernetes namespaces",
43-
RunE: unknownSubcommandAction,
43+
RunE: helpers.UnknownSubcommandAction,
4444
SilenceUsage: true,
4545
SilenceErrors: true,
4646
}

0 commit comments

Comments
 (0)