Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 351e8cd

Browse files
committed
Return exit code 1 & usage message when an unknown command is passed to the CLI
Signed-off-by: Guillaume Lours <[email protected]>
1 parent d8b439a commit 351e8cd

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

e2e/commands_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ import (
2020
"gotest.tools/icmd"
2121
)
2222

23+
func TestExitErrorCode(t *testing.T) {
24+
cmd, cleanup := dockerCli.createTestCmd()
25+
defer cleanup()
26+
27+
cmd.Command = dockerCli.Command("app", "unknown_command")
28+
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
29+
ExitCode: 1,
30+
Out: "\"unknown_command\" is not a docker app command",
31+
})
32+
}
33+
2334
func TestRender(t *testing.T) {
2435
appsPath := filepath.Join("testdata", "render")
2536
apps, err := ioutil.ReadDir(appsPath)

internal/commands/root.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77

88
"github.com/docker/app/internal"
9-
109
"github.com/docker/app/internal/store"
1110
"github.com/docker/cli/cli/command"
1211
"github.com/docker/cli/cli/config"
@@ -37,12 +36,21 @@ func NewRootCmd(use string, dockerCli command.Cli) *cobra.Command {
3736
return cmd.GenBashCompletion(dockerCli.Out())
3837
case "zsh":
3938
return cmd.GenZshCompletion(dockerCli.Out())
40-
case "":
41-
// Actually unset
42-
return nil
4339
default:
44-
return fmt.Errorf("%q is not a supported shell", completion)
40+
if completion != "" {
41+
fmt.Printf("%q is not a supported shell", completion)
42+
cmd.HelpFunc()(cmd, args)
43+
os.Exit(1)
44+
}
45+
}
46+
47+
if len(args) != 0 {
48+
fmt.Printf("%q is not a docker app command", args[0])
49+
cmd.HelpFunc()(cmd, args)
50+
os.Exit(1)
4551
}
52+
cmd.HelpFunc()(cmd, args)
53+
return nil
4654
},
4755
}
4856
addCommands(cmd, dockerCli)

0 commit comments

Comments
 (0)