[WIP] Completion for events --filter

Signed-off-by: Harald Albers <github@albersweb.de>
This commit is contained in:
Harald Albers 2024-10-15 19:05:36 +00:00
parent 21eea1e003
commit 616b7c974b
2 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,97 @@
package system
import (
"strings"
"github.com/docker/cli/cli/command/completion"
"github.com/spf13/cobra"
)
var (
eventFilters = []string{"container", "daemon", "event", "image", "label", "network", "node", "scope", "type", "volume"}
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 {
result := make([]string, len(values))
for i, v := range values {
result[i] = prefix + v
}
return result
}
func postfixWith(postfix string, values []string) []string {
result := make([]string, len(values))
for i, v := range values {
result[i] = v + postfix
}
return result
}

View File

@ -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
}