ImageNamesWithBase offers completion for images present within the local store, including both full image names with tags and base image names (repository names only) when multiple tags exist for the same base name
(dockerCLI APIClientProvider, limit int)
| 42 | // including both full image names with tags and base image names (repository names only) |
| 43 | // when multiple tags exist for the same base name |
| 44 | func ImageNamesWithBase(dockerCLI APIClientProvider, limit int) cobra.CompletionFunc { |
| 45 | return Unique(func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 46 | if limit > 0 && len(args) >= limit { |
| 47 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 48 | } |
| 49 | res, err := dockerCLI.Client().ImageList(cmd.Context(), client.ImageListOptions{}) |
| 50 | if err != nil { |
| 51 | return nil, cobra.ShellCompDirectiveError |
| 52 | } |
| 53 | var names []string |
| 54 | baseNameCounts := make(map[string]int) |
| 55 | for _, img := range res.Items { |
| 56 | names = append(names, img.RepoTags...) |
| 57 | for _, tag := range img.RepoTags { |
| 58 | ref, err := reference.ParseNormalizedNamed(tag) |
| 59 | if err != nil { |
| 60 | continue |
| 61 | } |
| 62 | baseNameCounts[reference.FamiliarName(ref)]++ |
| 63 | } |
| 64 | } |
| 65 | for baseName, count := range baseNameCounts { |
| 66 | if count > 1 { |
| 67 | names = append(names, baseName) |
| 68 | } |
| 69 | } |
| 70 | return names, cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp |
| 71 | }) |
| 72 | } |
| 73 | |
| 74 | // ContainerNames offers completion for container names and IDs |
| 75 | // By default, only names are returned. |