@@ -88,6 +88,7 @@ func setupImageEngine(cmd *cobra.Command) (entities.ImageEngine, error) {
88
88
}
89
89
90
90
func getContainers (cmd * cobra.Command , toComplete string , cType completeType , statuses ... string ) ([]string , cobra.ShellCompDirective ) {
91
+ var listContainers []entities.ListContainer
91
92
suggestions := []string {}
92
93
listOpts := entities.ContainerListOptions {
93
94
Filters : make (map [string ][]string ),
@@ -109,7 +110,20 @@ func getContainers(cmd *cobra.Command, toComplete string, cType completeType, st
109
110
return nil , cobra .ShellCompDirectiveNoFileComp
110
111
}
111
112
112
- for _ , c := range containers {
113
+ listContainers = append (listContainers , containers ... )
114
+
115
+ // Add containers from the external storage into complete list
116
+ if ok , _ := cmd .Flags ().GetBool ("external" ); ok {
117
+ externalContainers , err := engine .ContainerListExternal (registry .Context ())
118
+ if err != nil {
119
+ cobra .CompErrorln (err .Error ())
120
+ return nil , cobra .ShellCompDirectiveNoFileComp
121
+ }
122
+
123
+ listContainers = append (listContainers , externalContainers ... )
124
+ }
125
+
126
+ for _ , c := range listContainers {
113
127
// include ids in suggestions if cType == completeIDs or
114
128
// more then 2 chars are typed and cType == completeDefault
115
129
if ((len (toComplete ) > 1 && cType == completeDefault ) ||
@@ -341,6 +355,43 @@ func getArtifacts(cmd *cobra.Command, toComplete string) ([]string, cobra.ShellC
341
355
return suggestions , cobra .ShellCompDirectiveNoFileComp
342
356
}
343
357
358
+ func getCommands (cmd * cobra.Command , toComplete string ) ([]string , cobra.ShellCompDirective ) {
359
+ suggestions := []string {}
360
+ lsOpts := entities.ContainerListOptions {}
361
+
362
+ engine , err := setupContainerEngine (cmd )
363
+ if err != nil {
364
+ cobra .CompErrorln (err .Error ())
365
+ return nil , cobra .ShellCompDirectiveNoFileComp
366
+ }
367
+
368
+ containers , err := engine .ContainerList (registry .Context (), lsOpts )
369
+ if err != nil {
370
+ cobra .CompErrorln (err .Error ())
371
+ return nil , cobra .ShellCompDirectiveNoFileComp
372
+ }
373
+
374
+ externalContainers , err := engine .ContainerListExternal (registry .Context ())
375
+ if err != nil {
376
+ cobra .CompErrorln (err .Error ())
377
+ return nil , cobra .ShellCompDirectiveNoFileComp
378
+ }
379
+ containers = append (containers , externalContainers ... )
380
+
381
+ for _ , container := range containers {
382
+ // taking of the first element of commands list is done intentionally
383
+ // to exclude command arguments from suggestions (e.g. exclude arguments "-g daemon"
384
+ // from "nginx -g daemon" output)
385
+ if len (container .Command ) > 0 {
386
+ if strings .HasPrefix (container .Command [0 ], toComplete ) {
387
+ suggestions = append (suggestions , container .Command [0 ])
388
+ }
389
+ }
390
+ }
391
+
392
+ return suggestions , cobra .ShellCompDirectiveNoFileComp
393
+ }
394
+
344
395
func fdIsNotDir (f * os.File ) bool {
345
396
stat , err := f .Stat ()
346
397
if err != nil {
@@ -1703,6 +1754,7 @@ func AutocompletePsFilters(cmd *cobra.Command, args []string, toComplete string)
1703
1754
kv := keyValueCompletion {
1704
1755
"ancestor=" : func (s string ) ([]string , cobra.ShellCompDirective ) { return getImages (cmd , s ) },
1705
1756
"before=" : func (s string ) ([]string , cobra.ShellCompDirective ) { return getContainers (cmd , s , completeDefault ) },
1757
+ "command=" : func (s string ) ([]string , cobra.ShellCompDirective ) { return getCommands (cmd , s ) },
1706
1758
"exited=" : nil ,
1707
1759
"health=" : func (_ string ) ([]string , cobra.ShellCompDirective ) {
1708
1760
return []string {define .HealthCheckHealthy ,
0 commit comments