Skip to content

Commit

Permalink
[WIP] Completion for events --filter
Browse files Browse the repository at this point in the history
Signed-off-by: Harald Albers <github@albersweb.de>
  • Loading branch information
albers committed Oct 17, 2024
1 parent 21eea1e commit 0eb6f97
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
94 changes: 94 additions & 0 deletions cli/command/system/completion.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions cli/command/system/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 0eb6f97

Please sign in to comment.