From ad9b4b87d1b3df06fc4b3657e0ff6657a9ec951b Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Tue, 29 Oct 2024 13:05:24 +0100 Subject: [PATCH] Apply suggestions from code review Signed-off-by: Harald Albers Co-authored-by: Sebastiaan van Stijn --- cli/command/system/completion.go | 48 +++++++++++++++----------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/cli/command/system/completion.go b/cli/command/system/completion.go index cb7f36896228..0f70d1a8bf5c 100644 --- a/cli/command/system/completion.go +++ b/cli/command/system/completion.go @@ -87,38 +87,34 @@ var ( // completeEventFilters provides completion for the filters that can be used with `--filter`. func completeEventFilters(dockerCLI completion.APIClientProvider) completion.ValidArgsFn { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - if strings.HasPrefix(toComplete, "container=") { - return prefixWith("container=", containerNames(dockerCLI, cmd, args, toComplete)), cobra.ShellCompDirectiveNoFileComp + key, _, ok := strings.Cut(toComplete, "=") + if !ok { + return postfixWith("=", eventFilters), cobra.ShellCompDirectiveNoSpace } - if strings.HasPrefix(toComplete, "daemon=") { + switch key { + case "container": + return prefixWith("container=", containerNames(dockerCLI, cmd, args, toComplete)), cobra.ShellCompDirectiveNoFileComp + case "daemon": return prefixWith("daemon=", daemonNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp - } - if strings.HasPrefix(toComplete, "event=") { + case "event": return prefixWith("event=", validEventNames()), cobra.ShellCompDirectiveNoFileComp - } - if strings.HasPrefix(toComplete, "image=") { + case "image": return prefixWith("image=", imageNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp - } - if strings.HasPrefix(toComplete, "label=") { + case "label": return nil, cobra.ShellCompDirectiveNoFileComp - } - if strings.HasPrefix(toComplete, "network=") { + case "network": return prefixWith("network=", networkNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp - } - if strings.HasPrefix(toComplete, "node=") { + case "node": return prefixWith("node=", nodeNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp - } - if strings.HasPrefix(toComplete, "scope=") { + case "scope": return prefixWith("scope=", []string{"local", "swarm"}), cobra.ShellCompDirectiveNoFileComp - } - if strings.HasPrefix(toComplete, "type=") { + case "type": return prefixWith("type=", eventTypeNames()), cobra.ShellCompDirectiveNoFileComp - } - if strings.HasPrefix(toComplete, "volume=") { + case "volume": return prefixWith("volume=", volumeNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp + default: + return postfixWith("=", eventFilters), cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp } - - return postfixWith("=", eventFilters), cobra.ShellCompDirectiveNoSpace } } @@ -154,7 +150,7 @@ func eventTypeNames() []string { // The list is derived from eventActions. // Actions that are not suitable for usage in completions are removed. func validEventNames() []string { - names := []string{} + names := make([]string, 0, len(eventActions)) for _, eventAction := range eventActions { if strings.Contains(string(eventAction), " ") { continue @@ -191,7 +187,7 @@ func imageNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []st if err != nil { return []string{} } - names := []string{} + names := make([]string, 0, len(list)) for _, img := range list { names = append(names, img.RepoTags...) } @@ -205,7 +201,7 @@ func networkNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) [] if err != nil { return []string{} } - names := []string{} + names := make([]string, 0, len(list)) for _, nw := range list { names = append(names, nw.Name) } @@ -219,7 +215,7 @@ func nodeNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []str if err != nil { return []string{} } - names := []string{} + names := make([]string, 0, len(list)) for _, node := range list { names = append(names, node.Description.Hostname) } @@ -233,7 +229,7 @@ func volumeNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []s if err != nil { return []string{} } - names := []string{} + names := make([]string, 0, len(list.Volumes)) for _, v := range list.Volumes { names = append(names, v.Name) }