From 45def51117a24e823f5f937744ca3ae9f5821782 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 6 Nov 2025 11:15:26 +0100 Subject: [PATCH] make DRYRUN_PREFIX a display attribute, move DryRunClient out of pkg/api Signed-off-by: Nicolas De Loof --- cmd/display/dryrun.go | 21 +++++++++++++++++++++ cmd/display/plain.go | 2 +- cmd/display/tty.go | 2 +- pkg/compose/compose.go | 3 ++- pkg/{api => dryrun}/dryrunclient.go | 25 ++++--------------------- 5 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 cmd/display/dryrun.go rename pkg/{api => dryrun}/dryrunclient.go (96%) diff --git a/cmd/display/dryrun.go b/cmd/display/dryrun.go new file mode 100644 index 000000000..2ab542e5b --- /dev/null +++ b/cmd/display/dryrun.go @@ -0,0 +1,21 @@ +/* + Copyright 2020 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package display + +const ( + DRYRUN_PREFIX = " DRY-RUN MODE - " +) diff --git a/cmd/display/plain.go b/cmd/display/plain.go index 9af44a52a..39c0f323b 100644 --- a/cmd/display/plain.go +++ b/cmd/display/plain.go @@ -41,7 +41,7 @@ func (p *plainWriter) Start(ctx context.Context, operation string) { func (p *plainWriter) Event(e api.Resource) { prefix := "" if p.dryRun { - prefix = api.DRYRUN_PREFIX + prefix = DRYRUN_PREFIX } _, _ = fmt.Fprintln(p.out, prefix, e.ID, e.Text, e.Details) } diff --git a/cmd/display/tty.go b/cmd/display/tty.go index 228664502..e4bafca22 100644 --- a/cmd/display/tty.go +++ b/cmd/display/tty.go @@ -286,7 +286,7 @@ func (w *ttyWriter) lineText(t task, pad string, terminalWidth, statusPadding in } prefix := "" if dryRun { - prefix = PrefixColor(api.DRYRUN_PREFIX) + prefix = PrefixColor(DRYRUN_PREFIX) } elapsed := endTime.Sub(t.startTime).Seconds() diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index a54c32c15..228d3a66d 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -31,6 +31,7 @@ import ( "github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/cli/flags" "github.com/docker/cli/cli/streams" + "github.com/docker/compose/v2/pkg/dryrun" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/network" @@ -173,7 +174,7 @@ func WithDryRun(s *composeService) error { options := flags.NewClientOptions() options.Context = s.dockerCli.CurrentContext() err = cli.Initialize(options, command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) { - return api.NewDryRunClient(s.apiClient(), s.dockerCli) + return dryrun.NewDryRunClient(s.apiClient(), s.dockerCli) })) if err != nil { return err diff --git a/pkg/api/dryrunclient.go b/pkg/dryrun/dryrunclient.go similarity index 96% rename from pkg/api/dryrunclient.go rename to pkg/dryrun/dryrunclient.go index 4ca9f7993..6f393cac8 100644 --- a/pkg/api/dryrunclient.go +++ b/pkg/dryrun/dryrunclient.go @@ -14,7 +14,7 @@ limitations under the License. */ -package api +package dryrun import ( "bytes" @@ -50,10 +50,6 @@ import ( specs "github.com/opencontainers/image-spec/specs-go/v1" ) -const ( - DRYRUN_PREFIX = " DRY-RUN MODE - " -) - var _ client.APIClient = &DryRunClient{} // DryRunClient implements APIClient by delegating to implementation functions. This allows lazy init and per-method overrides @@ -192,28 +188,17 @@ func (d *DryRunClient) ContainerUnpause(ctx context.Context, container string) e func (d *DryRunClient) CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, containerType.PathStat, error) { rc := io.NopCloser(strings.NewReader("")) if _, err := d.ContainerStatPath(ctx, container, srcPath); err != nil { - return rc, containerType.PathStat{}, fmt.Errorf(" %s Could not find the file %s in container %s", DRYRUN_PREFIX, srcPath, container) + return rc, containerType.PathStat{}, fmt.Errorf("could not find the file %s in container %s", srcPath, container) } return rc, containerType.PathStat{}, nil } func (d *DryRunClient) CopyToContainer(ctx context.Context, container, path string, content io.Reader, options containerType.CopyToContainerOptions) error { - 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 build.ImageBuildOptions) (build.ImageBuildResponse, error) { - jsonMessage, err := json.Marshal(&jsonmessage.JSONMessage{ - Status: fmt.Sprintf("%[1]sSuccessfully built: dryRunID\n%[1]sSuccessfully tagged: %[2]s\n", DRYRUN_PREFIX, options.Tags[0]), - Progress: &jsonmessage.JSONProgress{}, - ID: "", - }) - if err != nil { - return build.ImageBuildResponse{}, err - } - rc := io.NopCloser(bytes.NewReader(jsonMessage)) + rc := io.NopCloser(bytes.NewReader(nil)) return build.ImageBuildResponse{ Body: rc, @@ -321,12 +306,10 @@ func (d *DryRunClient) ContainerExecCreate(ctx context.Context, container string } func (d *DryRunClient) ContainerExecStart(ctx context.Context, execID string, config containerType.ExecStartOptions) error { - v, ok := d.execs.LoadAndDelete(execID) + _, ok := d.execs.LoadAndDelete(execID) if !ok { return fmt.Errorf("invalid exec ID %q", execID) } - details := v.(execDetails) - fmt.Printf("%sExecuting command %q in %s (detached mode)\n", DRYRUN_PREFIX, details.command, details.container) return nil }