ContainerNames offers completion for container names and IDs By default, only names are returned. Set DOCKER_COMPLETION_SHOW_CONTAINER_IDS=yes to also complete IDs.
(dockerCLI APIClientProvider, all bool, filters ...func(container.Summary) bool)
| 75 | // By default, only names are returned. |
| 76 | // Set DOCKER_COMPLETION_SHOW_CONTAINER_IDS=yes to also complete IDs. |
| 77 | func ContainerNames(dockerCLI APIClientProvider, all bool, filters ...func(container.Summary) bool) cobra.CompletionFunc { |
| 78 | return Unique(func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 79 | res, err := dockerCLI.Client().ContainerList(cmd.Context(), client.ContainerListOptions{ |
| 80 | All: all, |
| 81 | }) |
| 82 | if err != nil { |
| 83 | return nil, cobra.ShellCompDirectiveError |
| 84 | } |
| 85 | |
| 86 | showContainerIDs := os.Getenv("DOCKER_COMPLETION_SHOW_CONTAINER_IDS") == "yes" |
| 87 | |
| 88 | var names []string |
| 89 | for _, ctr := range res.Items { |
| 90 | skip := false |
| 91 | for _, fn := range filters { |
| 92 | if fn != nil && !fn(ctr) { |
| 93 | skip = true |
| 94 | break |
| 95 | } |
| 96 | } |
| 97 | if skip { |
| 98 | continue |
| 99 | } |
| 100 | if showContainerIDs { |
| 101 | names = append(names, ctr.ID) |
| 102 | } |
| 103 | for _, n := range ctr.Names { |
| 104 | // Skip legacy link names: "/linked-container/link-name" |
| 105 | if len(n) <= 1 || strings.IndexByte(n[1:], '/') != -1 { |
| 106 | continue |
| 107 | } |
| 108 | names = append(names, strings.TrimPrefix(n, "/")) |
| 109 | } |
| 110 | } |
| 111 | return names, cobra.ShellCompDirectiveNoFileComp |
| 112 | }) |
| 113 | } |
| 114 | |
| 115 | // VolumeNames offers completion for volumes |
| 116 | func VolumeNames(dockerCLI APIClientProvider) cobra.CompletionFunc { |
searching dependent graphs…