Skip to content

Commit df57ff7

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 df57ff7

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
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
}

cli/command/completion/functions_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestCompleteContainerNames(t *testing.T) {
8484
{ID: "id-b", State: container.StateCreated, Names: []string{"/container-b"}},
8585
{ID: "id-a", State: container.StateExited, Names: []string{"/container-a"}},
8686
},
87-
expOut: []string{"container-c", "container-c/link-b", "container-b", "container-a"},
87+
expOut: []string{"container-c", "container-b", "container-a"},
8888
expOpts: client.ContainerListOptions{All: true},
8989
expDirective: cobra.ShellCompDirectiveNoFileComp,
9090
},
@@ -97,7 +97,7 @@ func TestCompleteContainerNames(t *testing.T) {
9797
{ID: "id-b", State: container.StateCreated, Names: []string{"/container-b"}},
9898
{ID: "id-a", State: container.StateExited, Names: []string{"/container-a"}},
9999
},
100-
expOut: []string{"id-c", "container-c", "container-c/link-b", "id-b", "container-b", "id-a", "container-a"},
100+
expOut: []string{"id-c", "container-c", "id-b", "container-b", "id-a", "container-a"},
101101
expOpts: client.ContainerListOptions{All: true},
102102
expDirective: cobra.ShellCompDirectiveNoFileComp,
103103
},
@@ -107,7 +107,7 @@ func TestCompleteContainerNames(t *testing.T) {
107107
containers: []container.Summary{
108108
{ID: "id-c", State: container.StateRunning, Names: []string{"/container-c", "/container-c/link-b"}},
109109
},
110-
expOut: []string{"container-c", "container-c/link-b"},
110+
expOut: []string{"container-c"},
111111
expDirective: cobra.ShellCompDirectiveNoFileComp,
112112
},
113113
{
@@ -117,7 +117,7 @@ func TestCompleteContainerNames(t *testing.T) {
117117
func(ctr container.Summary) bool { return ctr.State == container.StateCreated },
118118
},
119119
containers: []container.Summary{
120-
{ID: "id-c", State: container.StateRunning, Names: []string{"/container-c", "/container-c/link-b"}},
120+
{ID: "id-c", State: container.StateRunning, Names: []string{"/container-c"}},
121121
{ID: "id-b", State: container.StateCreated, Names: []string{"/container-b"}},
122122
{ID: "id-a", State: container.StateExited, Names: []string{"/container-a"}},
123123
},
@@ -133,7 +133,7 @@ func TestCompleteContainerNames(t *testing.T) {
133133
func(ctr container.Summary) bool { return ctr.State == container.StateCreated },
134134
},
135135
containers: []container.Summary{
136-
{ID: "id-c", State: container.StateRunning, Names: []string{"/container-c", "/container-c/link-b"}},
136+
{ID: "id-c", State: container.StateRunning, Names: []string{"/container-c"}},
137137
{ID: "id-b", State: container.StateCreated, Names: []string{"/container-b"}},
138138
{ID: "id-a", State: container.StateCreated, Names: []string{"/container-a"}},
139139
},

0 commit comments

Comments
 (0)