make DRYRUN_PREFIX a display attribute, move DryRunClient out of pkg/api

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2025-11-06 11:15:26 +01:00 committed by Guillaume Lours
parent aff5c115d6
commit 45def51117
5 changed files with 29 additions and 24 deletions

21
cmd/display/dryrun.go Normal file
View File

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

View File

@ -41,7 +41,7 @@ func (p *plainWriter) Start(ctx context.Context, operation string) {
func (p *plainWriter) Event(e api.Resource) { func (p *plainWriter) Event(e api.Resource) {
prefix := "" prefix := ""
if p.dryRun { if p.dryRun {
prefix = api.DRYRUN_PREFIX prefix = DRYRUN_PREFIX
} }
_, _ = fmt.Fprintln(p.out, prefix, e.ID, e.Text, e.Details) _, _ = fmt.Fprintln(p.out, prefix, e.ID, e.Text, e.Details)
} }

View File

@ -286,7 +286,7 @@ func (w *ttyWriter) lineText(t task, pad string, terminalWidth, statusPadding in
} }
prefix := "" prefix := ""
if dryRun { if dryRun {
prefix = PrefixColor(api.DRYRUN_PREFIX) prefix = PrefixColor(DRYRUN_PREFIX)
} }
elapsed := endTime.Sub(t.startTime).Seconds() elapsed := endTime.Sub(t.startTime).Seconds()

View File

@ -31,6 +31,7 @@ import (
"github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/flags" "github.com/docker/cli/cli/flags"
"github.com/docker/cli/cli/streams" "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/container"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
@ -173,7 +174,7 @@ func WithDryRun(s *composeService) error {
options := flags.NewClientOptions() options := flags.NewClientOptions()
options.Context = s.dockerCli.CurrentContext() options.Context = s.dockerCli.CurrentContext()
err = cli.Initialize(options, command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) { 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 { if err != nil {
return err return err

View File

@ -14,7 +14,7 @@
limitations under the License. limitations under the License.
*/ */
package api package dryrun
import ( import (
"bytes" "bytes"
@ -50,10 +50,6 @@ import (
specs "github.com/opencontainers/image-spec/specs-go/v1" specs "github.com/opencontainers/image-spec/specs-go/v1"
) )
const (
DRYRUN_PREFIX = " DRY-RUN MODE - "
)
var _ client.APIClient = &DryRunClient{} var _ client.APIClient = &DryRunClient{}
// DryRunClient implements APIClient by delegating to implementation functions. This allows lazy init and per-method overrides // 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) { func (d *DryRunClient) CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, containerType.PathStat, error) {
rc := io.NopCloser(strings.NewReader("")) rc := io.NopCloser(strings.NewReader(""))
if _, err := d.ContainerStatPath(ctx, container, srcPath); err != nil { 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 return rc, containerType.PathStat{}, nil
} }
func (d *DryRunClient) CopyToContainer(ctx context.Context, container, path string, content io.Reader, options containerType.CopyToContainerOptions) error { 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 return nil
} }
func (d *DryRunClient) ImageBuild(ctx context.Context, reader io.Reader, options build.ImageBuildOptions) (build.ImageBuildResponse, error) { func (d *DryRunClient) ImageBuild(ctx context.Context, reader io.Reader, options build.ImageBuildOptions) (build.ImageBuildResponse, error) {
jsonMessage, err := json.Marshal(&jsonmessage.JSONMessage{ rc := io.NopCloser(bytes.NewReader(nil))
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))
return build.ImageBuildResponse{ return build.ImageBuildResponse{
Body: rc, 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 { 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 { if !ok {
return fmt.Errorf("invalid exec ID %q", execID) 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 return nil
} }