Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit c2f2196

Browse files
committed
Add all parameter
Signed-off-by: Julien Tant <[email protected]>
1 parent 8f9ce9d commit c2f2196

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

api/compose/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ type PsOptions struct {
277277
type CopyOptions struct {
278278
Source string
279279
Destination string
280+
All bool
280281
Index int
281282
FollowLink bool
282283
CopyUIDGID bool

cli/cmd/compose/cp.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type copyOptions struct {
3232
source string
3333
destination string
3434
index int
35+
all bool
3536
followLink bool
3637
copyUIDGID bool
3738
}
@@ -60,7 +61,8 @@ func copyCommand(p *projectOptions, backend compose.Service) *cobra.Command {
6061
}
6162

6263
flags := copyCmd.Flags()
63-
flags.IntVar(&opts.index, "index", 1, "index of the container if there are multiple instances of a service [default: 1].")
64+
flags.IntVar(&opts.index, "index", 1, "Index of the container if there are multiple instances of a service [default: 1].")
65+
flags.BoolVar(&opts.all, "all", false, "Copy to all the containers of the service.")
6466
flags.BoolVarP(&opts.followLink, "follow-link", "L", false, "Always follow symbol link in SRC_PATH")
6567
flags.BoolVarP(&opts.copyUIDGID, "archive", "a", false, "Archive mode (copy all uid/gid information)")
6668

@@ -76,6 +78,7 @@ func runCopy(ctx context.Context, backend compose.Service, opts copyOptions) err
7678
return backend.Copy(ctx, projects, compose.CopyOptions{
7779
Source: opts.source,
7880
Destination: opts.destination,
81+
All: opts.all,
7982
Index: opts.index,
8083
FollowLink: opts.followLink,
8184
CopyUIDGID: opts.copyUIDGID,

local/compose/cp.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"path/filepath"
2525
"strings"
2626

27+
"golang.org/x/sync/errgroup"
28+
2729
"github.com/compose-spec/compose-go/types"
2830
"github.com/docker/cli/cli/command"
2931
"github.com/docker/compose-cli/api/compose"
@@ -51,19 +53,25 @@ func (s *composeService) Copy(ctx context.Context, project *types.Project, opts
5153
if srcService != "" {
5254
direction |= fromService
5355
serviceName = srcService
56+
57+
// copying from multiple containers of a services doesn't make sense.
58+
if opts.All {
59+
return errors.New("cannot use the --all flag when copying from a service")
60+
}
5461
}
5562
if destService != "" {
5663
direction |= toService
5764
serviceName = destService
5865
}
5966

60-
containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{
61-
Filters: filters.NewArgs(
62-
projectFilter(project.Name),
63-
serviceFilter(serviceName),
64-
filters.Arg("label", fmt.Sprintf("%s=%d", containerNumberLabel, opts.Index)),
65-
),
66-
})
67+
f := filters.NewArgs(
68+
projectFilter(project.Name),
69+
serviceFilter(serviceName),
70+
)
71+
if !opts.All {
72+
f.Add("label", fmt.Sprintf("%s=%d", containerNumberLabel, opts.Index))
73+
}
74+
containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{Filters: f})
6775
if err != nil {
6876
return err
6977
}
@@ -72,17 +80,25 @@ func (s *composeService) Copy(ctx context.Context, project *types.Project, opts
7280
return fmt.Errorf("service %s not running", serviceName)
7381
}
7482

75-
containerID := containers[0].ID
76-
switch direction {
77-
case fromService:
78-
return s.copyFromContainer(ctx, containerID, srcPath, dstPath, opts)
79-
case toService:
80-
return s.copyToContainer(ctx, containerID, srcPath, dstPath, opts)
81-
case acrossServices:
82-
return errors.New("copying between services is not supported")
83-
default:
84-
return errors.New("unknown copy direction")
83+
g := errgroup.Group{}
84+
for i := range containers {
85+
containerID := containers[i].ID
86+
87+
g.Go(func() error {
88+
switch direction {
89+
case fromService:
90+
return s.copyFromContainer(ctx, containerID, srcPath, dstPath, opts)
91+
case toService:
92+
return s.copyToContainer(ctx, containerID, srcPath, dstPath, opts)
93+
case acrossServices:
94+
return errors.New("copying between services is not supported")
95+
default:
96+
return errors.New("unknown copy direction")
97+
}
98+
})
8599
}
100+
101+
return g.Wait()
86102
}
87103

88104
func (s *composeService) copyToContainer(ctx context.Context, containerID string, srcPath string, dstPath string, opts compose.CopyOptions) error {

0 commit comments

Comments
 (0)