diff --git a/cli/command/container/completion.go b/cli/command/container/completion.go index c1f5c1eccc..94e9b50461 100644 --- a/cli/command/container/completion.go +++ b/cli/command/container/completion.go @@ -50,7 +50,6 @@ var logDriverOptions = map[string][]string{ "max-buffer-size", "mode", "awslogs-create-group", "awslogs-credentials-endpoint", "awslogs-datetime-format", "awslogs-group", "awslogs-multiline-pattern", "awslogs-region", "awslogs-stream", "tag", }, - "etwlogs": {}, "fluentd": { "max-buffer-size", "mode", "env", "env-regex", "labels", "fluentd-address", "fluentd-async", "fluentd-buffer-limit", "fluentd-request-ack", "fluentd-retry-wait", "fluentd-max-retries", @@ -79,6 +78,15 @@ var logDriverOptions = map[string][]string{ }, } +// builtInLogDrivers provides a list of the built-in logging drivers. +var builtInLogDrivers = sync.OnceValue(func() []string { + drivers := make([]string, 0, len(logDriverOptions)) + for driver := range logDriverOptions { + drivers = append(drivers, driver) + } + return drivers +}) + // allLogDriverOptions provides all options of the built-in logging drivers. // The list does not contain duplicates. var allLogDriverOptions = sync.OnceValue(func() []string { @@ -154,7 +162,7 @@ func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider) _ = cmd.RegisterFlagCompletionFunc("label", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("link", completeLink(dockerCLI)) _ = cmd.RegisterFlagCompletionFunc("link-local-ip", completion.NoComplete) - _ = cmd.RegisterFlagCompletionFunc("log-driver", completeLogDriver) + _ = cmd.RegisterFlagCompletionFunc("log-driver", completeLogDriver(dockerCLI)) _ = cmd.RegisterFlagCompletionFunc("log-opt", completeLogOpt) _ = cmd.RegisterFlagCompletionFunc("mac-address", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("memory", completion.NoComplete) @@ -220,12 +228,17 @@ func completeLink(dockerCLI completion.APIClientProvider) func(cmd *cobra.Comman } // completeLogDriver implements shell completion for the `--log-driver` option of `run` and `create`. -func completeLogDriver(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { - drivers := make([]string, 0, len(logDriverOptions)) - for driver := range logDriverOptions { - drivers = append(drivers, driver) +// The log drivers are collected from a call to the Info endpoint with a fallback to a hard-coded list +// of the build-in log drivers. +func completeLogDriver(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { + return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + info, err := dockerCLI.Client().Info(cmd.Context()) + if err != nil { + return builtInLogDrivers(), cobra.ShellCompDirectiveNoFileComp + } + drivers := info.Plugins.Log + return drivers, cobra.ShellCompDirectiveNoFileComp } - return drivers, cobra.ShellCompDirectiveNoFileComp } // completeLogOpt implements shell completion for the `--log-opt` option of `run` and `create`.