Skip to content

Commit 2336d9f

Browse files
committed
support dry-run for cp command
Signed-off-by: Guillaume Lours <[email protected]>
1 parent bf0ed9a commit 2336d9f

File tree

6 files changed

+44
-20
lines changed

6 files changed

+44
-20
lines changed

pkg/api/dryrunclient.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ package api
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"io"
2223
"net"
2324
"net/http"
25+
"strings"
2426

2527
moby "github.com/docker/docker/api/types"
2628
containerType "github.com/docker/docker/api/types/container"
@@ -35,6 +37,10 @@ import (
3537
specs "github.com/opencontainers/image-spec/specs-go/v1"
3638
)
3739

40+
const (
41+
DRYRUN_PREFIX = " DRY-RUN MODE - "
42+
)
43+
3844
var _ client.APIClient = &DryRunClient{}
3945

4046
type DryRunKey struct{}
@@ -95,11 +101,18 @@ func (d *DryRunClient) ContainerUnpause(ctx context.Context, container string) e
95101
}
96102

97103
func (d *DryRunClient) CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, moby.ContainerPathStat, error) {
98-
return nil, moby.ContainerPathStat{}, ErrNotImplemented
104+
rc := io.NopCloser(strings.NewReader(""))
105+
if _, err := d.ContainerStatPath(ctx, container, srcPath); err != nil {
106+
return rc, moby.ContainerPathStat{}, fmt.Errorf(" %s Could not find the file %s in container %s", DRYRUN_PREFIX, srcPath, container)
107+
}
108+
return rc, moby.ContainerPathStat{}, nil
99109
}
100110

101111
func (d *DryRunClient) CopyToContainer(ctx context.Context, container, path string, content io.Reader, options moby.CopyToContainerOptions) error {
102-
return ErrNotImplemented
112+
if _, err := d.ContainerStatPath(ctx, container, path); err != nil {
113+
return fmt.Errorf(" %s Could not find the file %s in container %s", DRYRUN_PREFIX, path, container)
114+
}
115+
return nil
103116
}
104117

105118
func (d *DryRunClient) ImageBuild(ctx context.Context, reader io.Reader, options moby.ImageBuildOptions) (moby.ImageBuildResponse, error) {

pkg/compose/compose.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func (s *composeService) MaxConcurrency(i int) {
6767
}
6868

6969
func (s *composeService) DryRunMode(ctx context.Context, dryRun bool) (context.Context, error) {
70+
s.dryRun = dryRun
7071
if dryRun {
7172
cli, err := command.NewDockerCli()
7273
if err != nil {

pkg/compose/cp.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,18 @@ func (s *composeService) Copy(ctx context.Context, projectName string, options a
7979
}
8080

8181
g := errgroup.Group{}
82-
for _, container := range containers {
83-
containerID := container.ID
82+
for _, cont := range containers {
83+
container := cont
8484
g.Go(func() error {
85-
return copyFunc(ctx, containerID, srcPath, dstPath, options)
85+
err := copyFunc(ctx, container.ID, srcPath, dstPath, options)
86+
if s.dryRun && err == nil {
87+
fromOrInside := "inside"
88+
if direction == fromService {
89+
fromOrInside = "from"
90+
}
91+
fmt.Printf("Copy %s to path %s %s %s service container\n", srcPath, dstPath, fromOrInside, getCanonicalContainerName(container))
92+
}
93+
return err
8694
})
8795
}
8896

@@ -194,14 +202,17 @@ func (s *composeService) copyToContainer(ctx context.Context, containerID string
194202
// extracted. This function also infers from the source and destination
195203
// info which directory to extract to, which may be the parent of the
196204
// destination that the user specified.
197-
dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
198-
if err != nil {
199-
return err
200-
}
201-
defer preparedArchive.Close() //nolint:errcheck
205+
// Don't create the archive if running in Dry Run mode
206+
if !s.dryRun {
207+
dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
208+
if err != nil {
209+
return err
210+
}
211+
defer preparedArchive.Close() //nolint:errcheck
202212

203-
resolvedDstPath = dstDir
204-
content = preparedArchive
213+
resolvedDstPath = dstDir
214+
content = preparedArchive
215+
}
205216
}
206217

207218
options := moby.CopyToContainerOptions{

pkg/progress/plain.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23+
24+
"github.com/docker/compose/v2/pkg/api"
2325
)
2426

2527
type plainWriter struct {
@@ -40,7 +42,7 @@ func (p *plainWriter) Start(ctx context.Context) error {
4042
func (p *plainWriter) Event(e Event) {
4143
prefix := ""
4244
if p.dryRun {
43-
prefix = "DRY RUN MODE - "
45+
prefix = api.DRYRUN_PREFIX
4446
}
4547
fmt.Fprintln(p.out, prefix, e.ID, e.Text, e.StatusText)
4648
}
@@ -54,7 +56,7 @@ func (p *plainWriter) Events(events []Event) {
5456
func (p *plainWriter) TailMsgf(m string, args ...interface{}) {
5557
prefix := ""
5658
if p.dryRun {
57-
prefix = DRYRUN_PREFIX
59+
prefix = api.DRYRUN_PREFIX
5860
}
5961
fmt.Fprintln(p.out, append([]interface{}{prefix, m}, args...)...)
6062
}

pkg/progress/tty.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"sync"
2626
"time"
2727

28+
"github.com/docker/compose/v2/pkg/api"
2829
"github.com/docker/compose/v2/pkg/utils"
2930

3031
"github.com/buger/goterm"
@@ -110,7 +111,7 @@ func (w *ttyWriter) TailMsgf(msg string, args ...interface{}) {
110111
defer w.mtx.Unlock()
111112
msgWithPrefix := msg
112113
if w.dryRun {
113-
msgWithPrefix = strings.TrimSpace(DRYRUN_PREFIX + msg)
114+
msgWithPrefix = strings.TrimSpace(api.DRYRUN_PREFIX + msg)
114115
}
115116
w.tailEvents = append(w.tailEvents, fmt.Sprintf(msgWithPrefix, args...))
116117
}
@@ -206,7 +207,7 @@ func lineText(event Event, pad string, terminalWidth, statusPadding int, color b
206207
}
207208
prefix := ""
208209
if dryRun {
209-
prefix = DRYRUN_PREFIX
210+
prefix = api.DRYRUN_PREFIX
210211
}
211212

212213
elapsed := endTime.Sub(event.startTime).Seconds()

pkg/progress/writer.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ import (
2828
"golang.org/x/sync/errgroup"
2929
)
3030

31-
const (
32-
DRYRUN_PREFIX = " DRY-RUN MODE - "
33-
)
34-
3531
// Writer can write multiple progress events
3632
type Writer interface {
3733
Start(context.Context) error

0 commit comments

Comments
 (0)