Skip to content

Commit a983cf5

Browse files
gloursndeloof
authored andcommitted
cp command: copy to all containers of a service as default behaviour
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 2f47e45 commit a983cf5

File tree

5 files changed

+20
-26
lines changed

5 files changed

+20
-26
lines changed

cmd/compose/cp.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func copyCommand(p *projectOptions, backend api.Service) *cobra.Command {
5555
}
5656
return nil
5757
}),
58-
RunE: Adapt(func(ctx context.Context, args []string) error {
58+
RunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
5959
opts.source = args[0]
6060
opts.destination = args[1]
6161
return runCopy(ctx, backend, opts)
@@ -64,8 +64,10 @@ func copyCommand(p *projectOptions, backend api.Service) *cobra.Command {
6464
}
6565

6666
flags := copyCmd.Flags()
67-
flags.IntVar(&opts.index, "index", 1, "Index of the container if there are multiple instances of a service [default: 1].")
67+
flags.IntVar(&opts.index, "index", 0, "Index of the container if there are multiple instances of a service [default: 1 when copying from a container].")
6868
flags.BoolVar(&opts.all, "all", false, "Copy to all the containers of the service.")
69+
flags.MarkHidden("all") //nolint:errcheck
70+
flags.MarkDeprecated("all", "By default all the containers of the service will get the source file/directory to be copied.") //nolint:errcheck
6971
flags.BoolVarP(&opts.followLink, "follow-link", "L", false, "Always follow symbol link in SRC_PATH")
7072
flags.BoolVarP(&opts.copyUIDGID, "archive", "a", false, "Archive mode (copy all uid/gid information)")
7173

docs/reference/compose_cp.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ Copy files/folders between a service container and the local filesystem
77

88
| Name | Type | Default | Description |
99
| --- | --- | --- | --- |
10-
| `--all` | | | Copy to all the containers of the service. |
1110
| `-a`, `--archive` | | | Archive mode (copy all uid/gid information) |
1211
| `-L`, `--follow-link` | | | Always follow symbol link in SRC_PATH |
13-
| `--index` | `int` | `1` | Index of the container if there are multiple instances of a service [default: 1]. |
12+
| `--index` | `int` | `0` | Index of the container if there are multiple instances of a service [default: 1 when copying from a container]. |
1413

1514

1615
<!---MARKER_GEN_END-->

docs/reference/docker_compose_cp.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ options:
1010
value_type: bool
1111
default_value: "false"
1212
description: Copy to all the containers of the service.
13-
deprecated: false
14-
hidden: false
13+
deprecated: true
14+
hidden: true
1515
experimental: false
1616
experimentalcli: false
1717
kubernetes: false
@@ -40,9 +40,9 @@ options:
4040
swarm: false
4141
- option: index
4242
value_type: int
43-
default_value: "1"
43+
default_value: "0"
4444
description: |
45-
Index of the container if there are multiple instances of a service [default: 1].
45+
Index of the container if there are multiple instances of a service [default: 1 when copying from a container].
4646
deprecated: false
4747
hidden: false
4848
experimental: false

pkg/compose/cp.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func (s *composeService) Copy(ctx context.Context, projectName string, options a
5757
if options.All {
5858
return errors.New("cannot use the --all flag when copying from a service")
5959
}
60+
// due to remove of the --all flag, restore the default value to 1 when copying from a service container to the host.
61+
if options.Index == 0 {
62+
options.Index = 1
63+
}
6064
}
6165
if destService != "" {
6266
direction |= toService
@@ -72,7 +76,7 @@ func (s *composeService) Copy(ctx context.Context, projectName string, options a
7276
return fmt.Errorf("no container found for service %q", serviceName)
7377
}
7478

75-
if !options.All {
79+
if direction == fromService || (direction == toService && options.Index > 0) {
7680
containers = containers.filter(indexed(options.Index))
7781
}
7882

pkg/e2e/cp_test.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,18 @@ func TestCopy(t *testing.T) {
4747
res.Assert(t, icmd.Expected{Out: `nginx running`})
4848
})
4949

50-
t.Run("copy to container copies the file to the first container by default", func(t *testing.T) {
50+
t.Run("copy to container copies the file to the all containers by default", func(t *testing.T) {
5151
res := c.RunDockerComposeCmd("-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", "./fixtures/cp-test/cp-me.txt", "nginx:/tmp/default.txt")
5252
res.Assert(t, icmd.Expected{ExitCode: 0})
5353

5454
output := c.RunDockerCmd("exec", projectName+"-nginx-1", "cat", "/tmp/default.txt").Stdout()
5555
assert.Assert(t, strings.Contains(output, `hello world`), output)
5656

57-
res = c.RunDockerOrExitError("exec", projectName+"_nginx_2", "cat", "/tmp/default.txt")
58-
res.Assert(t, icmd.Expected{ExitCode: 1})
57+
output = c.RunDockerCmd("exec", projectName+"-nginx-2", "cat", "/tmp/default.txt").Stdout()
58+
assert.Assert(t, strings.Contains(output, `hello world`), output)
59+
60+
output = c.RunDockerCmd("exec", projectName+"-nginx-3", "cat", "/tmp/default.txt").Stdout()
61+
assert.Assert(t, strings.Contains(output, `hello world`), output)
5962
})
6063

6164
t.Run("copy to container with a given index copies the file to the given container", func(t *testing.T) {
@@ -69,20 +72,6 @@ func TestCopy(t *testing.T) {
6972
res.Assert(t, icmd.Expected{ExitCode: 1})
7073
})
7174

72-
t.Run("copy to container with the all flag copies the file to all containers", func(t *testing.T) {
73-
res := c.RunDockerComposeCmd("-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", "--all", "./fixtures/cp-test/cp-me.txt", "nginx:/tmp/all.txt")
74-
res.Assert(t, icmd.Expected{ExitCode: 0})
75-
76-
output := c.RunDockerCmd("exec", projectName+"-nginx-1", "cat", "/tmp/all.txt").Stdout()
77-
assert.Assert(t, strings.Contains(output, `hello world`), output)
78-
79-
output = c.RunDockerCmd("exec", projectName+"-nginx-2", "cat", "/tmp/all.txt").Stdout()
80-
assert.Assert(t, strings.Contains(output, `hello world`), output)
81-
82-
output = c.RunDockerCmd("exec", projectName+"-nginx-3", "cat", "/tmp/all.txt").Stdout()
83-
assert.Assert(t, strings.Contains(output, `hello world`), output)
84-
})
85-
8675
t.Run("copy from a container copies the file to the host from the first container by default", func(t *testing.T) {
8776
res := c.RunDockerComposeCmd("-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", "nginx:/tmp/default.txt", "./fixtures/cp-test/from-default.txt")
8877
res.Assert(t, icmd.Expected{ExitCode: 0})

0 commit comments

Comments
 (0)