Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support dry-run for cp command #10235

Merged
merged 2 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions pkg/api/dryrunclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package api

import (
"context"
"fmt"
"io"
"net"
"net/http"
"strings"

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

const (
DRYRUN_PREFIX = " DRY-RUN MODE - "
)

var _ client.APIClient = &DryRunClient{}

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

func (d *DryRunClient) CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, moby.ContainerPathStat, error) {
return nil, moby.ContainerPathStat{}, ErrNotImplemented
rc := io.NopCloser(strings.NewReader(""))
if _, err := d.ContainerStatPath(ctx, container, srcPath); err != nil {
return rc, moby.ContainerPathStat{}, fmt.Errorf(" %s Could not find the file %s in container %s", DRYRUN_PREFIX, srcPath, container)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to suggest s/file/path but then realized this text comes from moby/moby 🙈

}
return rc, moby.ContainerPathStat{}, nil
}

func (d *DryRunClient) CopyToContainer(ctx context.Context, container, path string, content io.Reader, options moby.CopyToContainerOptions) error {
return ErrNotImplemented
if _, err := d.ContainerStatPath(ctx, container, path); err != nil {
return fmt.Errorf(" %s Could not find the file %s in container %s", DRYRUN_PREFIX, path, container)
}
return nil
glours marked this conversation as resolved.
Show resolved Hide resolved
}

func (d *DryRunClient) ImageBuild(ctx context.Context, reader io.Reader, options moby.ImageBuildOptions) (moby.ImageBuildResponse, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (s *composeService) MaxConcurrency(i int) {
}

func (s *composeService) DryRunMode(ctx context.Context, dryRun bool) (context.Context, error) {
s.dryRun = dryRun
if dryRun {
cli, err := command.NewDockerCli()
if err != nil {
Expand Down
31 changes: 21 additions & 10 deletions pkg/compose/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,18 @@ func (s *composeService) Copy(ctx context.Context, projectName string, options a
}

g := errgroup.Group{}
for _, container := range containers {
containerID := container.ID
for _, cont := range containers {
container := cont
g.Go(func() error {
return copyFunc(ctx, containerID, srcPath, dstPath, options)
if err := copyFunc(ctx, container.ID, srcPath, dstPath, options); err != nil {
return err
}
fromOrInside := "inside"
if direction == fromService {
fromOrInside = "from"
}
fmt.Fprintf(s.stderr(), "Copy %s to path %s %s %s service container\n", srcPath, dstPath, fromOrInside, getCanonicalContainerName(container))
return nil
})
}

Expand Down Expand Up @@ -194,14 +202,17 @@ func (s *composeService) copyToContainer(ctx context.Context, containerID string
// extracted. This function also infers from the source and destination
// info which directory to extract to, which may be the parent of the
// destination that the user specified.
dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
if err != nil {
return err
}
defer preparedArchive.Close() //nolint:errcheck
// Don't create the archive if running in Dry Run mode
if !s.dryRun {
dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
if err != nil {
return err
}
defer preparedArchive.Close() //nolint:errcheck

resolvedDstPath = dstDir
content = preparedArchive
resolvedDstPath = dstDir
content = preparedArchive
}
}

options := moby.CopyToContainerOptions{
Expand Down
6 changes: 4 additions & 2 deletions pkg/progress/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"fmt"
"io"

"github.com/docker/compose/v2/pkg/api"
)

type plainWriter struct {
Expand All @@ -40,7 +42,7 @@ func (p *plainWriter) Start(ctx context.Context) error {
func (p *plainWriter) Event(e Event) {
prefix := ""
if p.dryRun {
prefix = "DRY RUN MODE - "
prefix = api.DRYRUN_PREFIX
}
fmt.Fprintln(p.out, prefix, e.ID, e.Text, e.StatusText)
}
Expand All @@ -54,7 +56,7 @@ func (p *plainWriter) Events(events []Event) {
func (p *plainWriter) TailMsgf(m string, args ...interface{}) {
prefix := ""
if p.dryRun {
prefix = DRYRUN_PREFIX
prefix = api.DRYRUN_PREFIX
}
fmt.Fprintln(p.out, append([]interface{}{prefix, m}, args...)...)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/progress/tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sync"
"time"

"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/utils"

"github.com/buger/goterm"
Expand Down Expand Up @@ -110,7 +111,7 @@ func (w *ttyWriter) TailMsgf(msg string, args ...interface{}) {
defer w.mtx.Unlock()
msgWithPrefix := msg
if w.dryRun {
msgWithPrefix = strings.TrimSpace(DRYRUN_PREFIX + msg)
msgWithPrefix = strings.TrimSpace(api.DRYRUN_PREFIX + msg)
}
w.tailEvents = append(w.tailEvents, fmt.Sprintf(msgWithPrefix, args...))
}
Expand Down Expand Up @@ -206,7 +207,7 @@ func lineText(event Event, pad string, terminalWidth, statusPadding int, color b
}
prefix := ""
if dryRun {
prefix = DRYRUN_PREFIX
prefix = api.DRYRUN_PREFIX
}

elapsed := endTime.Sub(event.startTime).Seconds()
Expand Down
4 changes: 0 additions & 4 deletions pkg/progress/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ import (
"golang.org/x/sync/errgroup"
)

const (
DRYRUN_PREFIX = " DRY-RUN MODE - "
)

// Writer can write multiple progress events
type Writer interface {
Start(context.Context) error
Expand Down