From 5081ab0507a0e585940408ef58c03c62cfcbf469 Mon Sep 17 00:00:00 2001 From: Guillaume Lours <705411+glours@users.noreply.github.com> Date: Thu, 12 Jan 2023 23:31:14 +0100 Subject: [PATCH] create custom CLI when dry-run mode active update documentation Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com> --- cmd/compose/compose.go | 3 +-- docs/reference/compose.md | 1 - docs/reference/docker_compose.yaml | 2 +- pkg/api/api.go | 2 +- pkg/api/proxy.go | 7 ++++--- pkg/compose/compose.go | 22 +++++++++++++++++----- pkg/mocks/mock_docker_compose_api.go | 6 ++++-- 7 files changed, 28 insertions(+), 15 deletions(-) diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index a76999d77..63f140619 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -336,8 +336,7 @@ func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //no if parallel > 0 { backend.MaxConcurrency(parallel) } - backend.DryRunMode(dryRun) - return nil + return backend.DryRunMode(dryRun) }, } diff --git a/docs/reference/compose.md b/docs/reference/compose.md index 4165918de..8ca43277d 100644 --- a/docs/reference/compose.md +++ b/docs/reference/compose.md @@ -41,7 +41,6 @@ Docker Compose |:-----------------------|:--------------|:--------|:----------------------------------------------------------------------------------------------------| | `--ansi` | `string` | `auto` | Control when to print ANSI control characters ("never"\|"always"\|"auto") | | `--compatibility` | | | Run compose in backward compatibility mode | -| `--dry-run` | | | Execute command in dry run mode | | `--env-file` | `string` | | Specify an alternate environment file. | | `-f`, `--file` | `stringArray` | | Compose configuration files | | `--parallel` | `int` | `-1` | Control max parallelism, -1 for unlimited | diff --git a/docs/reference/docker_compose.yaml b/docs/reference/docker_compose.yaml index ddf3d3520..190cf99fd 100644 --- a/docs/reference/docker_compose.yaml +++ b/docs/reference/docker_compose.yaml @@ -183,7 +183,7 @@ options: default_value: "false" description: Execute command in dry run mode deprecated: false - hidden: false + hidden: true experimental: false experimentalcli: false kubernetes: false diff --git a/pkg/api/api.go b/pkg/api/api.go index c957a9850..11f798b27 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -78,7 +78,7 @@ type Service interface { // MaxConcurrency defines upper limit for concurrent operations against engine API MaxConcurrency(parallel int) // DryRunMode defines if dry run applies to the command - DryRunMode(dryRun bool) + DryRunMode(dryRun bool) error // Watch services' development context and sync/notify/rebuild/restart on changes Watch(ctx context.Context, project *types.Project, services []string, options WatchOptions) error } diff --git a/pkg/api/proxy.go b/pkg/api/proxy.go index 76ce0c153..efcf79bbc 100644 --- a/pkg/api/proxy.go +++ b/pkg/api/proxy.go @@ -52,7 +52,7 @@ type ServiceProxy struct { ImagesFn func(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error) WatchFn func(ctx context.Context, project *types.Project, services []string, options WatchOptions) error MaxConcurrencyFn func(parallel int) - DryRunModeFn func(dryRun bool) + DryRunModeFn func(dryRun bool) error interceptors []Interceptor } @@ -92,6 +92,7 @@ func (s *ServiceProxy) WithService(service Service) *ServiceProxy { s.ImagesFn = service.Images s.WatchFn = service.Watch s.MaxConcurrencyFn = service.MaxConcurrency + s.DryRunModeFn = service.DryRunMode return s } @@ -326,6 +327,6 @@ func (s *ServiceProxy) MaxConcurrency(i int) { s.MaxConcurrencyFn(i) } -func (s *ServiceProxy) DryRunMode(dryRun bool) { - s.DryRunModeFn(dryRun) +func (s *ServiceProxy) DryRunMode(dryRun bool) error { + return s.DryRunModeFn(dryRun) } diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index 8cf820fdf..05db272d8 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -20,13 +20,11 @@ import ( "context" "encoding/json" "fmt" - "io" - "strings" - "github.com/compose-spec/compose-go/types" "github.com/distribution/distribution/v3/reference" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/config/configfile" + "github.com/docker/cli/cli/flags" "github.com/docker/cli/cli/streams" moby "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -34,6 +32,8 @@ import ( "github.com/opencontainers/go-digest" "github.com/pkg/errors" "gopkg.in/yaml.v2" + "io" + "strings" "github.com/docker/compose/v2/pkg/api" ) @@ -65,8 +65,20 @@ func (s *composeService) MaxConcurrency(i int) { s.maxConcurrency = i } -func (s *composeService) DryRunMode(dryRun bool) { - s.dryRun = dryRun +func (s *composeService) DryRunMode(dryRun bool) error { + if dryRun { + cli, err := command.NewDockerCli() + if err != nil { + return err + } + cli.Initialize(flags.NewClientOptions(), command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) { + dryRunClient := api.NewDryRunClient() + dryRunClient.WithAPIClient(s.apiClient()) + return dryRunClient, nil + })) + s.dockerCli = cli + } + return nil } func (s *composeService) stdout() *streams.Out { diff --git a/pkg/mocks/mock_docker_compose_api.go b/pkg/mocks/mock_docker_compose_api.go index 5868dbb8c..d98eda846 100644 --- a/pkg/mocks/mock_docker_compose_api.go +++ b/pkg/mocks/mock_docker_compose_api.go @@ -108,9 +108,11 @@ func (mr *MockServiceMockRecorder) Down(ctx, projectName, options interface{}) * } // DryRunMode mocks base method. -func (m *MockService) DryRunMode(dryRun bool) { +func (m *MockService) DryRunMode(dryRun bool) error { m.ctrl.T.Helper() - m.ctrl.Call(m, "DryRunMode", dryRun) + ret := m.ctrl.Call(m, "DryRunMode", dryRun) + ret0, _ := ret[0].(error) + return ret0 } // DryRunMode indicates an expected call of DryRunMode.