From 68fe829c968917232529a8f23a533339622b4fde Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 5 Jul 2024 13:42:46 +0200 Subject: [PATCH] cli/command/completion: add FileNames utility This is just a convenience function to allow defining completion to use the default (complete with filenames and directories). Signed-off-by: Sebastiaan van Stijn (cherry picked from commit 9207ff104681c56a8940ecb32c5080fa4c024444) Signed-off-by: Sebastiaan van Stijn --- cli/command/completion/functions.go | 7 +++++++ cli/command/container/exec.go | 4 +--- cli/command/container/run.go | 4 +--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cli/command/completion/functions.go b/cli/command/completion/functions.go index 6026ef7692..034e78852e 100644 --- a/cli/command/completion/functions.go +++ b/cli/command/completion/functions.go @@ -106,6 +106,13 @@ func NetworkNames(dockerCLI APIClientProvider) ValidArgsFn { } } +// FileNames is a convenience function to use [cobra.ShellCompDirectiveDefault], +// which indicates to let the shell perform its default behavior after +// completions have been provided. +func FileNames(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + return nil, cobra.ShellCompDirectiveDefault +} + // NoComplete is used for commands where there's no relevant completion func NoComplete(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { return nil, cobra.ShellCompDirectiveNoFileComp diff --git a/cli/command/container/exec.go b/cli/command/container/exec.go index 1c0a741263..cfb3e4baad 100644 --- a/cli/command/container/exec.go +++ b/cli/command/container/exec.go @@ -82,9 +82,7 @@ func NewExecCommand(dockerCli command.Cli) *cobra.Command { _ = cmd.RegisterFlagCompletionFunc("env", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { return os.Environ(), cobra.ShellCompDirectiveNoFileComp }) - _ = cmd.RegisterFlagCompletionFunc("env-file", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { - return nil, cobra.ShellCompDirectiveDefault // _filedir - }) + _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) return cmd } diff --git a/cli/command/container/run.go b/cli/command/container/run.go index 14322ef3d2..bdf8eff5be 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -73,9 +73,7 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command { _ = cmd.RegisterFlagCompletionFunc("env", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { return os.Environ(), cobra.ShellCompDirectiveNoFileComp }) - _ = cmd.RegisterFlagCompletionFunc("env-file", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { - return nil, cobra.ShellCompDirectiveDefault - }) + _ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames) _ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli)) return cmd }