Improve completion for "--log-driver"

Signed-off-by: Harald Albers <github@albersweb.de>
This commit is contained in:
Harald Albers 2024-10-30 17:08:33 +00:00
parent adcce2b4ef
commit a40ea78579
1 changed files with 20 additions and 7 deletions

View File

@ -50,7 +50,6 @@ var logDriverOptions = map[string][]string{
"max-buffer-size", "mode", "awslogs-create-group", "awslogs-credentials-endpoint", "awslogs-datetime-format", "max-buffer-size", "mode", "awslogs-create-group", "awslogs-credentials-endpoint", "awslogs-datetime-format",
"awslogs-group", "awslogs-multiline-pattern", "awslogs-region", "awslogs-stream", "tag", "awslogs-group", "awslogs-multiline-pattern", "awslogs-region", "awslogs-stream", "tag",
}, },
"etwlogs": {},
"fluentd": { "fluentd": {
"max-buffer-size", "mode", "env", "env-regex", "labels", "fluentd-address", "fluentd-async", "max-buffer-size", "mode", "env", "env-regex", "labels", "fluentd-address", "fluentd-async",
"fluentd-buffer-limit", "fluentd-request-ack", "fluentd-retry-wait", "fluentd-max-retries", "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. // allLogDriverOptions provides all options of the built-in logging drivers.
// The list does not contain duplicates. // The list does not contain duplicates.
var allLogDriverOptions = sync.OnceValue(func() []string { 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("label", completion.NoComplete)
_ = cmd.RegisterFlagCompletionFunc("link", completeLink(dockerCLI)) _ = cmd.RegisterFlagCompletionFunc("link", completeLink(dockerCLI))
_ = cmd.RegisterFlagCompletionFunc("link-local-ip", completion.NoComplete) _ = 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("log-opt", completeLogOpt)
_ = cmd.RegisterFlagCompletionFunc("mac-address", completion.NoComplete) _ = cmd.RegisterFlagCompletionFunc("mac-address", completion.NoComplete)
_ = cmd.RegisterFlagCompletionFunc("memory", 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`. // completeLogDriver implements shell completion for the `--log-driver` option of `run` and `create`.
func completeLogDriver(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { // The log drivers are collected from a call to the Info endpoint with a fallback to a hard-coded list
drivers := make([]string, 0, len(logDriverOptions)) // of the build-in log drivers.
for driver := range logDriverOptions { func completeLogDriver(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
drivers = append(drivers, driver) 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`. // completeLogOpt implements shell completion for the `--log-opt` option of `run` and `create`.