Merge pull request #6446 from thaJeztah/fix_completions

Improve shell completion for `docker secret` and `docker config` subcommands
This commit is contained in:
Sebastiaan van Stijn
2025-09-10 14:30:05 +02:00
committed by GitHub
12 changed files with 44 additions and 29 deletions

View File

@ -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()

View File

@ -32,9 +32,7 @@ func newConfigInspectCommand(dockerCLI command.Cli) *cobra.Command {
opts.names = args
return runInspect(cmd.Context(), dockerCLI, opts)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCLI)(cmd, args, toComplete)
},
ValidArgsFunction: completeNames(dockerCLI),
DisableFlagsInUseLine: true,
}

View File

@ -19,9 +19,7 @@ func newConfigRemoveCommand(dockerCLI command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runRemove(cmd.Context(), dockerCLI, args)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCLI)(cmd, args, toComplete)
},
ValidArgsFunction: completeNames(dockerCLI),
DisableFlagsInUseLine: true,
}
}

View File

@ -44,7 +44,7 @@ func completeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc
}
var names []string
for _, secret := range list {
names = append(names, secret.ID)
names = append(names, secret.Spec.Name)
}
return names, cobra.ShellCompDirectiveNoFileComp
}

View File

@ -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()

View File

@ -31,9 +31,7 @@ func newSecretInspectCommand(dockerCLI command.Cli) *cobra.Command {
opts.names = args
return runSecretInspect(cmd.Context(), dockerCLI, opts)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCLI)(cmd, args, toComplete)
},
ValidArgsFunction: completeNames(dockerCLI),
DisableFlagsInUseLine: true,
}

View File

@ -31,9 +31,7 @@ func newSecretListCommand(dockerCLI command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runSecretList(cmd.Context(), dockerCLI, options)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCLI)(cmd, args, toComplete)
},
ValidArgsFunction: cobra.NoFileCompletions,
DisableFlagsInUseLine: true,
}

View File

@ -26,9 +26,7 @@ func newSecretRemoveCommand(dockerCLI command.Cli) *cobra.Command {
}
return runRemove(cmd.Context(), dockerCLI, opts)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCLI)(cmd, args, toComplete)
},
ValidArgsFunction: completeNames(dockerCLI),
DisableFlagsInUseLine: true,
}
}

View File

@ -45,9 +45,7 @@ func newDeployCommand(dockerCLI command.Cli) *cobra.Command {
}
return runDeploy(cmd.Context(), dockerCLI, cmd.Flags(), &opts, config)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCLI)(cmd, args, toComplete)
},
ValidArgsFunction: completeNames(dockerCLI),
DisableFlagsInUseLine: true,
}

View File

@ -38,9 +38,7 @@ func newPsCommand(dockerCLI command.Cli) *cobra.Command {
}
return runPS(cmd.Context(), dockerCLI, opts)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCLI)(cmd, args, toComplete)
},
ValidArgsFunction: completeNames(dockerCLI),
DisableFlagsInUseLine: true,
}
flags := cmd.Flags()

View File

@ -36,9 +36,7 @@ func newRemoveCommand(dockerCLI command.Cli) *cobra.Command {
}
return runRemove(cmd.Context(), dockerCLI, opts)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCLI)(cmd, args, toComplete)
},
ValidArgsFunction: completeNames(dockerCLI),
DisableFlagsInUseLine: true,
}

View File

@ -38,9 +38,7 @@ func newServicesCommand(dockerCLI command.Cli) *cobra.Command {
}
return runServices(cmd.Context(), dockerCLI, opts)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCLI)(cmd, args, toComplete)
},
ValidArgsFunction: completeNames(dockerCLI),
DisableFlagsInUseLine: true,
}
flags := cmd.Flags()