@@ -38,6 +38,37 @@ func ImageNames(dockerCLI APIClientProvider, limit int) cobra.CompletionFunc {
3838 }
3939}
4040
41+ // ImageNamesWithBase offers completion for images present within the local store,
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 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+ list , 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 list {
56+ names = append (names , img .RepoTags ... )
57+ for _ , tag := range img .RepoTags {
58+ if baseName , _ , ok := strings .Cut (tag , ":" ); ok {
59+ baseNameCounts [baseName ]++
60+ }
61+ }
62+ }
63+ for baseName , count := range baseNameCounts {
64+ if count > 1 {
65+ names = append (names , baseName )
66+ }
67+ }
68+ return names , cobra .ShellCompDirectiveNoSpace
69+ }
70+ }
71+
4172// ContainerNames offers completion for container names and IDs
4273// By default, only names are returned.
4374// Set DOCKER_COMPLETION_SHOW_CONTAINER_IDS=yes to also complete IDs.
0 commit comments