inspect: disable default (file) completion

Before this patch, flags and arguments would complete using filenames
from the current directory;

    docker inspect --type <TAB>
    AUTHORS       CONTRIBUTING.md             docs/             Makefile            SECURITY.md
    ...

    docker inspect <TAB>

With this patch, no completion is provided;

    docker inspect --type <TAB>
    # no results

    docker inspect <TAB>
    # no results

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-05-06 14:23:15 +02:00
parent f61e2bb6f1
commit 877ea1ce35
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -11,6 +11,7 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/cli/command/inspect"
flagsHelper "github.com/docker/cli/cli/flags"
"github.com/docker/docker/api/types"
@ -20,6 +21,7 @@ import (
"github.com/docker/docker/errdefs"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type objectType = string
@ -56,6 +58,8 @@ func NewInspectCommand(dockerCli command.Cli) *cobra.Command {
opts.ids = args
return runInspect(cmd.Context(), dockerCli, opts)
},
// TODO(thaJeztah): should we consider adding completion for common object-types? (images, containers?)
ValidArgsFunction: completion.NoComplete,
}
flags := cmd.Flags()
@ -63,6 +67,12 @@ func NewInspectCommand(dockerCli command.Cli) *cobra.Command {
flags.StringVar(&opts.objectType, "type", "", "Return JSON for specified type")
flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes if the type is container")
flags.VisitAll(func(flag *pflag.Flag) {
// Set a default completion function if none was set. We don't look
// up if it does already have one set, because Cobra does this for
// us, and returns an error (which we ignore for this reason).
_ = cmd.RegisterFlagCompletionFunc(flag.Name, completion.NoComplete)
})
return cmd
}