Browse Source

create custom CLI when dry-run mode active
update documentation

Signed-off-by: Guillaume Lours <[email protected]>

Guillaume Lours 2 years ago
parent
commit
5081ab0507

+ 1 - 2
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)
 		},
 	}
 

+ 0 - 1
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                                                           |

+ 1 - 1
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

+ 1 - 1
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
 }

+ 4 - 3
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)
 }

+ 17 - 5
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 {

+ 4 - 2
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.