Skip to content

Commit ad9b4b8

Browse files
albersthaJeztah
andcommitted
Apply suggestions from code review
Signed-off-by: Harald Albers <github@albersweb.de> Co-authored-by: Sebastiaan van Stijn <thaJeztah@users.noreply.github.com>
1 parent 2926d0f commit ad9b4b8

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

cli/command/system/completion.go

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,38 +87,34 @@ var (
8787
// completeEventFilters provides completion for the filters that can be used with `--filter`.
8888
func completeEventFilters(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
8989
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
90-
if strings.HasPrefix(toComplete, "container=") {
91-
return prefixWith("container=", containerNames(dockerCLI, cmd, args, toComplete)), cobra.ShellCompDirectiveNoFileComp
90+
key, _, ok := strings.Cut(toComplete, "=")
91+
if !ok {
92+
return postfixWith("=", eventFilters), cobra.ShellCompDirectiveNoSpace
9293
}
93-
if strings.HasPrefix(toComplete, "daemon=") {
94+
switch key {
95+
case "container":
96+
return prefixWith("container=", containerNames(dockerCLI, cmd, args, toComplete)), cobra.ShellCompDirectiveNoFileComp
97+
case "daemon":
9498
return prefixWith("daemon=", daemonNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp
95-
}
96-
if strings.HasPrefix(toComplete, "event=") {
99+
case "event":
97100
return prefixWith("event=", validEventNames()), cobra.ShellCompDirectiveNoFileComp
98-
}
99-
if strings.HasPrefix(toComplete, "image=") {
101+
case "image":
100102
return prefixWith("image=", imageNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp
101-
}
102-
if strings.HasPrefix(toComplete, "label=") {
103+
case "label":
103104
return nil, cobra.ShellCompDirectiveNoFileComp
104-
}
105-
if strings.HasPrefix(toComplete, "network=") {
105+
case "network":
106106
return prefixWith("network=", networkNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp
107-
}
108-
if strings.HasPrefix(toComplete, "node=") {
107+
case "node":
109108
return prefixWith("node=", nodeNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp
110-
}
111-
if strings.HasPrefix(toComplete, "scope=") {
109+
case "scope":
112110
return prefixWith("scope=", []string{"local", "swarm"}), cobra.ShellCompDirectiveNoFileComp
113-
}
114-
if strings.HasPrefix(toComplete, "type=") {
111+
case "type":
115112
return prefixWith("type=", eventTypeNames()), cobra.ShellCompDirectiveNoFileComp
116-
}
117-
if strings.HasPrefix(toComplete, "volume=") {
113+
case "volume":
118114
return prefixWith("volume=", volumeNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp
115+
default:
116+
return postfixWith("=", eventFilters), cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp
119117
}
120-
121-
return postfixWith("=", eventFilters), cobra.ShellCompDirectiveNoSpace
122118
}
123119
}
124120

@@ -154,7 +150,7 @@ func eventTypeNames() []string {
154150
// The list is derived from eventActions.
155151
// Actions that are not suitable for usage in completions are removed.
156152
func validEventNames() []string {
157-
names := []string{}
153+
names := make([]string, 0, len(eventActions))
158154
for _, eventAction := range eventActions {
159155
if strings.Contains(string(eventAction), " ") {
160156
continue
@@ -191,7 +187,7 @@ func imageNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []st
191187
if err != nil {
192188
return []string{}
193189
}
194-
names := []string{}
190+
names := make([]string, 0, len(list))
195191
for _, img := range list {
196192
names = append(names, img.RepoTags...)
197193
}
@@ -205,7 +201,7 @@ func networkNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []
205201
if err != nil {
206202
return []string{}
207203
}
208-
names := []string{}
204+
names := make([]string, 0, len(list))
209205
for _, nw := range list {
210206
names = append(names, nw.Name)
211207
}
@@ -219,7 +215,7 @@ func nodeNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []str
219215
if err != nil {
220216
return []string{}
221217
}
222-
names := []string{}
218+
names := make([]string, 0, len(list))
223219
for _, node := range list {
224220
names = append(names, node.Description.Hostname)
225221
}
@@ -233,7 +229,7 @@ func volumeNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []s
233229
if err != nil {
234230
return []string{}
235231
}
236-
names := []string{}
232+
names := make([]string, 0, len(list.Volumes))
237233
for _, v := range list.Volumes {
238234
names = append(names, v.Name)
239235
}

0 commit comments

Comments
 (0)