-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Completion for events --filter
Signed-off-by: Harald Albers <[email protected]>
- Loading branch information
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters