cmd/docker: Check command exists before raising flag error

Signed-off-by: William North <billynorth10@gmail.com>
This commit is contained in:
William North 2024-05-24 09:04:13 +10:00
parent 421e3b5e91
commit 33845a4d5d
No known key found for this signature in database
GPG Key ID: 1678ADE6D06A5BE3
1 changed files with 9 additions and 0 deletions

View File

@ -116,6 +116,15 @@ func setFlagErrorFunc(dockerCli command.Cli, cmd *cobra.Command) {
// is called.
flagErrorFunc := cmd.FlagErrorFunc()
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
// When TraverseChildren is set to true on a parent command,
// Cobra will parse flags before looking for the appropriate command to run.
// First check the command exists before raising a flag error
args := os.Args[1:]
commandExists := findCommand(cmd, args)
if !commandExists {
return fmt.Errorf("docker: '%s' is not a docker command.\nSee 'docker --help'", args[0])
}
if err := pluginmanager.AddPluginCommandStubs(dockerCli, cmd.Root()); err != nil {
return err
}