Skip to content

Commit a964d55

Browse files
gloursndeloof
authored andcommitted
align cp command index management with exec command
Signed-off-by: Guillaume Lours <[email protected]>
1 parent a983cf5 commit a964d55

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

pkg/compose/containers.go

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ package compose
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"sort"
22-
"strconv"
23-
24-
moby "github.com/docker/docker/api/types"
25-
"github.com/docker/docker/api/types/filters"
2623

2724
"github.com/docker/compose/v2/pkg/api"
2825
"github.com/docker/compose/v2/pkg/utils"
26+
moby "github.com/docker/docker/api/types"
27+
"github.com/docker/docker/api/types/filters"
2928
)
3029

3130
// Containers is a set of moby Container
@@ -41,7 +40,22 @@ const (
4140

4241
func (s *composeService) getContainers(ctx context.Context, project string, oneOff oneOff, stopped bool, selectedServices ...string) (Containers, error) {
4342
var containers Containers
44-
f := []filters.KeyValuePair{projectFilter(project)}
43+
f := getDefaultFilters(project, oneOff, selectedServices...)
44+
containers, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
45+
Filters: filters.NewArgs(f...),
46+
All: stopped,
47+
})
48+
if err != nil {
49+
return nil, err
50+
}
51+
if len(selectedServices) > 1 {
52+
containers = containers.filter(isService(selectedServices...))
53+
}
54+
return containers, nil
55+
}
56+
57+
func getDefaultFilters(projectName string, oneOff oneOff, selectedServices ...string) []filters.KeyValuePair {
58+
f := []filters.KeyValuePair{projectFilter(projectName)}
4559
if len(selectedServices) == 1 {
4660
f = append(f, serviceFilter(selectedServices[0]))
4761
}
@@ -52,17 +66,26 @@ func (s *composeService) getContainers(ctx context.Context, project string, oneO
5266
f = append(f, oneOffFilter(false))
5367
case oneOffInclude:
5468
}
69+
return f
70+
}
71+
72+
func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, stopped bool, serviceName string, containerIndex int) (moby.Container, error) {
73+
defaultFilters := getDefaultFilters(projectName, oneOff, serviceName)
74+
defaultFilters = append(defaultFilters, containerNumberFilter(containerIndex))
5575
containers, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
56-
Filters: filters.NewArgs(f...),
57-
All: stopped,
76+
Filters: filters.NewArgs(
77+
defaultFilters...,
78+
),
79+
All: stopped,
5880
})
5981
if err != nil {
60-
return nil, err
82+
return moby.Container{}, err
6183
}
62-
if len(selectedServices) > 1 {
63-
containers = containers.filter(isService(selectedServices...))
84+
if len(containers) < 1 {
85+
return moby.Container{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex)
6486
}
65-
return containers, nil
87+
container := containers[0]
88+
return container, nil
6689
}
6790

6891
// containerPredicate define a predicate we want container to satisfy for filtering operations
@@ -87,14 +110,6 @@ func isNotOneOff(c moby.Container) bool {
87110
return !ok || v == "False"
88111
}
89112

90-
func indexed(index int) containerPredicate {
91-
return func(c moby.Container) bool {
92-
number := c.Labels[api.ContainerNumberLabel]
93-
idx, err := strconv.Atoi(number)
94-
return err == nil && index == idx
95-
}
96-
}
97-
98113
// filter return Containers with elements to match predicate
99114
func (containers Containers) filter(predicate containerPredicate) Containers {
100115
var filtered Containers

pkg/compose/cp.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,23 @@ func (s *composeService) Copy(ctx context.Context, projectName string, options a
6666
direction |= toService
6767
serviceName = destService
6868
}
69-
70-
containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, serviceName)
71-
if err != nil {
72-
return err
73-
}
74-
75-
if len(containers) < 1 {
76-
return fmt.Errorf("no container found for service %q", serviceName)
77-
}
78-
69+
var containers Containers
70+
var err error
7971
if direction == fromService || (direction == toService && options.Index > 0) {
80-
containers = containers.filter(indexed(options.Index))
72+
container, err := s.getSpecifiedContainer(ctx, projectName, oneOffExclude, true, serviceName, options.Index)
73+
if err != nil {
74+
return err
75+
}
76+
containers = append(containers, container)
77+
} else {
78+
containers, err = s.getContainers(ctx, projectName, oneOffExclude, true, serviceName)
79+
if err != nil {
80+
return err
81+
}
82+
83+
if len(containers) < 1 {
84+
return fmt.Errorf("no container found for service %q", serviceName)
85+
}
8186
}
8287

8388
g := errgroup.Group{}

pkg/compose/exec.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ package compose
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"strings"
2322

2423
"github.com/docker/cli/cli"
2524
"github.com/docker/cli/cli/command/container"
2625
"github.com/docker/compose/v2/pkg/api"
2726
moby "github.com/docker/docker/api/types"
28-
"github.com/docker/docker/api/types/filters"
2927
)
3028

3129
func (s *composeService) Exec(ctx context.Context, projectName string, options api.RunOptions) (int, error) {
@@ -59,19 +57,5 @@ func (s *composeService) Exec(ctx context.Context, projectName string, options a
5957
}
6058

6159
func (s *composeService) getExecTarget(ctx context.Context, projectName string, opts api.RunOptions) (moby.Container, error) {
62-
containers, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
63-
Filters: filters.NewArgs(
64-
projectFilter(projectName),
65-
serviceFilter(opts.Service),
66-
containerNumberFilter(opts.Index),
67-
),
68-
})
69-
if err != nil {
70-
return moby.Container{}, err
71-
}
72-
if len(containers) < 1 {
73-
return moby.Container{}, fmt.Errorf("service %q is not running container #%d", opts.Service, opts.Index)
74-
}
75-
container := containers[0]
76-
return container, nil
60+
return s.getSpecifiedContainer(ctx, projectName, oneOffInclude, false, opts.Service, opts.Index)
7761
}

0 commit comments

Comments
 (0)