Merge pull request #10235 from glours/dry-run-cp-support

support dry-run for cp command
This commit is contained in:
Guillaume Lours 2023-02-03 22:57:26 +01:00 committed by GitHub
commit 23585b9e6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 20 deletions

View File

@ -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"
@ -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{}
@ -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)
}
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
}
func (d *DryRunClient) ImageBuild(ctx context.Context, reader io.Reader, options moby.ImageBuildOptions) (moby.ImageBuildResponse, error) {

View File

@ -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 {

View File

@ -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
})
}
@ -194,6 +202,8 @@ 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.
// 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
@ -203,6 +213,7 @@ func (s *composeService) copyToContainer(ctx context.Context, containerID string
resolvedDstPath = dstDir
content = preparedArchive
}
}
options := moby.CopyToContainerOptions{
AllowOverwriteDirWithFile: false,

View File

@ -20,6 +20,8 @@ import (
"context"
"fmt"
"io"
"github.com/docker/compose/v2/pkg/api"
)
type plainWriter struct {
@ -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)
}
@ -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...)...)
}

View File

@ -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"
@ -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...))
}
@ -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()

View File

@ -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