From 8325214ccc6dde492c9a221a0bef757779a1ccc8 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 10 Sep 2025 10:31:36 +0200 Subject: [PATCH] cli/command: fix completion for secret create, config create These commands accept two arguments; the first is a custom name, the second is either a filename or "-" to create from STDIN. With this patch: # does not provide completion docker secret create # starts providing completion once a non-empty name is provided docker secret create somename file.txt other-file.txt Signed-off-by: Sebastiaan van Stijn --- cli/command/config/create.go | 18 +++++++++++++++++- cli/command/secret/create.go | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cli/command/config/create.go b/cli/command/config/create.go index 4b3903d71e..eba5f08e77 100644 --- a/cli/command/config/create.go +++ b/cli/command/config/create.go @@ -36,7 +36,23 @@ func newConfigCreateCommand(dockerCLI command.Cli) *cobra.Command { createOpts.file = args[1] return runCreate(cmd.Context(), dockerCLI, createOpts) }, - ValidArgsFunction: cobra.NoFileCompletions, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + switch len(args) { + case 0: + // No completion for the first argument, which is the name for + // the new config, but if a non-empty name is given, we return + // it as completion to allow "tab"-ing to the next completion. + return []string{toComplete}, cobra.ShellCompDirectiveNoFileComp + case 1: + // Second argument is either "-" or a file to load. + // + // TODO(thaJeztah): provide completion for "-". + return nil, cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveDefault + default: + // Command only accepts two arguments. + return nil, cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp + } + }, DisableFlagsInUseLine: true, } flags := cmd.Flags() diff --git a/cli/command/secret/create.go b/cli/command/secret/create.go index 5efaf717d3..5cb409e4d6 100644 --- a/cli/command/secret/create.go +++ b/cli/command/secret/create.go @@ -38,6 +38,23 @@ func newSecretCreateCommand(dockerCLI command.Cli) *cobra.Command { } return runSecretCreate(cmd.Context(), dockerCLI, options) }, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + switch len(args) { + case 0: + // No completion for the first argument, which is the name for + // the new secret, but if a non-empty name is given, we return + // it as completion to allow "tab"-ing to the next completion. + return []string{toComplete}, cobra.ShellCompDirectiveNoFileComp + case 1: + // Second argument is either "-" or a file to load. + // + // TODO(thaJeztah): provide completion for "-". + return nil, cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveDefault + default: + // Command only accepts two arguments. + return nil, cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp + } + }, DisableFlagsInUseLine: true, } flags := cmd.Flags()