Skip to content

Commit cc286e2

Browse files
committed
cli: error out on unknown command
Signed-off-by: CrazyMax <[email protected]>
1 parent 1de3325 commit cc286e2

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

commands/imagetools/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ type RootOptions struct {
1010
Builder *string
1111
}
1212

13-
func RootCmd(dockerCli command.Cli, opts RootOptions) *cobra.Command {
13+
func RootCmd(rootcmd *cobra.Command, dockerCli command.Cli, opts RootOptions) *cobra.Command {
1414
cmd := &cobra.Command{
1515
Use: "imagetools",
1616
Short: "Commands to work on images in registry",
1717
ValidArgsFunction: completion.Disable,
18+
RunE: rootcmd.RunE,
1819
}
1920

2021
cmd.AddCommand(

commands/root.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"fmt"
45
"os"
56

67
debugcmd "github.com/docker/buildx/commands/debug"
@@ -36,13 +37,22 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
3637
if opt.debug {
3738
debug.Enable()
3839
}
39-
4040
cmd.SetContext(appcontext.Context())
4141
if !isPlugin {
4242
return nil
4343
}
4444
return plugin.PersistentPreRunE(cmd, args)
4545
},
46+
RunE: func(cmd *cobra.Command, args []string) error {
47+
if len(args) == 0 {
48+
return cmd.Help()
49+
}
50+
_ = cmd.Help()
51+
return cli.StatusError{
52+
StatusCode: 1,
53+
Status: fmt.Sprintf("ERROR: unknown command: %q", args[0]),
54+
}
55+
},
4656
}
4757
if !isPlugin {
4858
// match plugin behavior for standalone mode
@@ -95,7 +105,7 @@ func addCommands(cmd *cobra.Command, opts *rootOptions, dockerCli command.Cli) {
95105
versionCmd(dockerCli),
96106
pruneCmd(dockerCli, opts),
97107
duCmd(dockerCli, opts),
98-
imagetoolscmd.RootCmd(dockerCli, imagetoolscmd.RootOptions{Builder: &opts.builder}),
108+
imagetoolscmd.RootCmd(cmd, dockerCli, imagetoolscmd.RootOptions{Builder: &opts.builder}),
99109
)
100110
if confutil.IsExperimental() {
101111
cmd.AddCommand(debugcmd.RootCmd(dockerCli,

tests/common.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tests
2+
3+
import (
4+
"testing"
5+
6+
"github.com/moby/buildkit/util/testutil/integration"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
var commonTests = []func(t *testing.T, sb integration.Sandbox){
11+
testUnknownCommand,
12+
testUnknownFlag,
13+
}
14+
15+
func testUnknownCommand(t *testing.T, sb integration.Sandbox) {
16+
cmd := buildxCmd(sb, withArgs("foo"))
17+
out, err := cmd.CombinedOutput()
18+
require.Error(t, err, string(out))
19+
20+
cmd = buildxCmd(sb, withArgs("imagetools", "foo"))
21+
out, err = cmd.CombinedOutput()
22+
require.Error(t, err, string(out))
23+
}
24+
25+
func testUnknownFlag(t *testing.T, sb integration.Sandbox) {
26+
cmd := buildxCmd(sb, withArgs("build", "--foo=bar"))
27+
out, err := cmd.CombinedOutput()
28+
require.Error(t, err, string(out))
29+
}

tests/integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func init() {
2121

2222
func TestIntegration(t *testing.T) {
2323
var tests []func(t *testing.T, sb integration.Sandbox)
24+
tests = append(tests, commonTests...)
2425
tests = append(tests, buildTests...)
2526
tests = append(tests, bakeTests...)
2627
tests = append(tests, inspectTests...)

0 commit comments

Comments
 (0)