registerCompletionFuncForGlobalFlags was called from newDockerCommand,
at which time no context-store is initialized yet, so it would return
a nil value, probably resulting in `store.Names` to panic, but these
errors are not shown when running the completion. As a result, the flag
completion would fall back to completing from filenames.
This patch changes the function to dynamically get the context-store;
this fixes the problem mentioned above, because at the time the completion
function is _invoked_, the CLI is fully initialized, and does have a
context-store available.
A (non-exported) interface is defined to allow the function to accept
alternative implementations (not requiring a full command.DockerCLI).
Before this patch:
docker context create one
docker context create two
docker --context <TAB>
.DS_Store .idea/ Makefile
.dockerignore .mailmap build/
...
With this patch:
docker context create one
docker context create two
docker --context <TAB>
default one two
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
28 lines
745 B
Go
28 lines
745 B
Go
package main
|
|
|
|
import (
|
|
"github.com/docker/cli/cli/command/completion"
|
|
"github.com/docker/cli/cli/context/store"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type contextStoreProvider interface {
|
|
ContextStore() store.Store
|
|
}
|
|
|
|
func registerCompletionFuncForGlobalFlags(dockerCLI contextStoreProvider, cmd *cobra.Command) error {
|
|
err := cmd.RegisterFlagCompletionFunc("context", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
|
|
names, _ := store.Names(dockerCLI.ContextStore())
|
|
return names, cobra.ShellCompDirectiveNoFileComp
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = cmd.RegisterFlagCompletionFunc("log-level", completion.FromList("debug", "info", "warn", "error", "fatal"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|