Skip to content

Commit 7886b31

Browse files
committed
cli/command/completion: ContainerNames: skip legacy link names
Inline StripNamePrefix and skip legacy links for completion. Legacy links can be removed from a container, but only when using `docker [container] rm --link <link-name>`. When linking containers through legacy links, a container can get multiple names; its own name, and a name for each link it's providing: # create two containers with links between them docker run -d --name one nginx:alpine docker run -d --name two --link one:link1 --link one:link2 --link one:link3 nginx:alpine # container "one" now has multiple names docker ps --no-trunc --format '{{.Names}}' two one,two/link1,two/link2,two/link3 # running `docker rm --link` with a link-name removes a link: docker rm --link two/link3 docker ps --no-trunc --format '{{.Names}}' two one,two/link1,two/link2 # but without `--link`, it resolves the linked container and removes it: docker rm -fv two/link2 two/link2 docker ps --no-trunc --format '{{.Names}}' two Legacy links are deprecated, and this can be confusing, so let's not provide completion for secondary names. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 7922984 commit 7886b31

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

cli/command/completion/functions.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strings"
66

77
"github.com/distribution/reference"
8-
"github.com/docker/cli/cli/command/formatter"
98
"github.com/moby/moby/api/types/container"
109
"github.com/moby/moby/client"
1110
"github.com/spf13/cobra"
@@ -101,7 +100,13 @@ func ContainerNames(dockerCLI APIClientProvider, all bool, filters ...func(conta
101100
if showContainerIDs {
102101
names = append(names, ctr.ID)
103102
}
104-
names = append(names, formatter.StripNamePrefix(ctr.Names)...)
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+
}
105110
}
106111
return names, cobra.ShellCompDirectiveNoFileComp
107112
}

0 commit comments

Comments
 (0)