@@ -87,38 +87,34 @@ var (
8787// completeEventFilters provides completion for the filters that can be used with `--filter`.
8888func completeEventFilters (dockerCLI completion.APIClientProvider ) completion.ValidArgsFn {
8989return 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" :
9498return prefixWith ("daemon=" , daemonNames (dockerCLI , cmd )), cobra .ShellCompDirectiveNoFileComp
95- }
96- if strings .HasPrefix (toComplete , "event=" ) {
99+ case "event" :
97100return prefixWith ("event=" , validEventNames ()), cobra .ShellCompDirectiveNoFileComp
98- }
99- if strings .HasPrefix (toComplete , "image=" ) {
101+ case "image" :
100102return prefixWith ("image=" , imageNames (dockerCLI , cmd )), cobra .ShellCompDirectiveNoFileComp
101- }
102- if strings .HasPrefix (toComplete , "label=" ) {
103+ case "label" :
103104return nil , cobra .ShellCompDirectiveNoFileComp
104- }
105- if strings .HasPrefix (toComplete , "network=" ) {
105+ case "network" :
106106return prefixWith ("network=" , networkNames (dockerCLI , cmd )), cobra .ShellCompDirectiveNoFileComp
107- }
108- if strings .HasPrefix (toComplete , "node=" ) {
107+ case "node" :
109108return prefixWith ("node=" , nodeNames (dockerCLI , cmd )), cobra .ShellCompDirectiveNoFileComp
110- }
111- if strings .HasPrefix (toComplete , "scope=" ) {
109+ case "scope" :
112110return prefixWith ("scope=" , []string {"local" , "swarm" }), cobra .ShellCompDirectiveNoFileComp
113- }
114- if strings .HasPrefix (toComplete , "type=" ) {
111+ case "type" :
115112return prefixWith ("type=" , eventTypeNames ()), cobra .ShellCompDirectiveNoFileComp
116- }
117- if strings .HasPrefix (toComplete , "volume=" ) {
113+ case "volume" :
118114return 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.
156152func validEventNames () []string {
157- names := []string {}
153+ names := make ( []string , 0 , len ( eventActions ))
158154for _ , eventAction := range eventActions {
159155if strings .Contains (string (eventAction ), " " ) {
160156continue
@@ -191,7 +187,7 @@ func imageNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []st
191187if err != nil {
192188return []string {}
193189}
194- names := []string {}
190+ names := make ( []string , 0 , len ( list ))
195191for _ , img := range list {
196192names = append (names , img .RepoTags ... )
197193}
@@ -205,7 +201,7 @@ func networkNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []
205201if err != nil {
206202return []string {}
207203}
208- names := []string {}
204+ names := make ( []string , 0 , len ( list ))
209205for _ , nw := range list {
210206names = append (names , nw .Name )
211207}
@@ -219,7 +215,7 @@ func nodeNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []str
219215if err != nil {
220216return []string {}
221217}
222- names := []string {}
218+ names := make ( []string , 0 , len ( list ))
223219for _ , node := range list {
224220names = append (names , node .Description .Hostname )
225221}
@@ -233,7 +229,7 @@ func volumeNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []s
233229if err != nil {
234230return []string {}
235231}
236- names := []string {}
232+ names := make ( []string , 0 , len ( list . Volumes ))
237233for _ , v := range list .Volumes {
238234names = append (names , v .Name )
239235}
0 commit comments