diff --git a/cli/command/system/completion.go b/cli/command/system/completion.go new file mode 100644 index 000000000000..37856790122c --- /dev/null +++ b/cli/command/system/completion.go @@ -0,0 +1,94 @@ +package system + +import ( + "github.com/docker/cli/cli/command/completion" + "github.com/spf13/cobra" + "strings" +) + +var eventFilters = []string{"container", "daemon", "event", "image", "label", "network", "node", "scope", "type", "volume"} +var eventNames = []string{ + "attach", + "commit", + "connect", + "copy", + "create", + "delete", + "destroy", + "detach", + "die", + "disable", + "disconnect", + "enable", + "exec_create", + "exec_detach", + "exec_die", + "exec_start", + "export", + "health_status", + "import", + "install", + "kill", + "load", + "mount", + "oom", + "pause", + "pull", + "push", + "reload", + "remove", + "rename", + "resize", + "restart", + "save", + "start", + "stop", + "tag", + "top", + "unmount", + "unpause", + "untag", + "update", +} +var eventTypes = []string{"config", "container", "daemon", "image", "network", "node", "plugin", "secret", "service", "volume"} + +func completeFilters(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { + return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if strings.HasPrefix(toComplete, "container=") { + // the pure container name list should be pulled out from ContainerNames. + names, _ := completion.ContainerNames(dockerCLI, true)(cmd, args, toComplete) + return prefixWith("container=", names), cobra.ShellCompDirectiveDefault + } + if strings.HasPrefix(toComplete, "event=") { + return prefixWith("event=", eventNames), cobra.ShellCompDirectiveDefault + } + if strings.HasPrefix(toComplete, "label=") { + return nil, cobra.ShellCompDirectiveNoFileComp + } + if strings.HasPrefix(toComplete, "network=") { + // the pure network name list should be pulled out from NetworkNames. + names, _ := completion.NetworkNames(dockerCLI)(cmd, args, toComplete) + return prefixWith("network=", names), cobra.ShellCompDirectiveDefault + } + if strings.HasPrefix(toComplete, "type=") { + return prefixWith("type=", eventTypes), cobra.ShellCompDirectiveDefault + } + return postfixWith("=", eventFilters), cobra.ShellCompDirectiveNoSpace + } +} + +func prefixWith(prefix string, values []string) []string { + var result = make([]string, len(values)) + for i, v := range values { + result[i] = prefix + v + } + return result +} + +func postfixWith(postfix string, values []string) []string { + var result = make([]string, len(values)) + for i, v := range values { + result[i] = v + postfix + } + return result +} diff --git a/cli/command/system/events.go b/cli/command/system/events.go index a54d30ffed38..e31256e215d7 100644 --- a/cli/command/system/events.go +++ b/cli/command/system/events.go @@ -50,6 +50,8 @@ func NewEventsCommand(dockerCli command.Cli) *cobra.Command { flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided") flags.StringVar(&options.format, "format", "", flagsHelper.InspectFormatHelp) // using the same flag description as "inspect" commands for now. + _ = cmd.RegisterFlagCompletionFunc("filter", completeFilters(dockerCli)) + return cmd }