Skip to content

Commit 5081ab0

Browse files
committed
create custom CLI when dry-run mode active
update documentation Signed-off-by: Guillaume Lours <[email protected]>
1 parent 13ef440 commit 5081ab0

File tree

7 files changed

+28
-15
lines changed

7 files changed

+28
-15
lines changed

cmd/compose/compose.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,7 @@ func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //no
336336
if parallel > 0 {
337337
backend.MaxConcurrency(parallel)
338338
}
339-
backend.DryRunMode(dryRun)
340-
return nil
339+
return backend.DryRunMode(dryRun)
341340
},
342341
}
343342

docs/reference/compose.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ Docker Compose
4141
|:-----------------------|:--------------|:--------|:----------------------------------------------------------------------------------------------------|
4242
| `--ansi` | `string` | `auto` | Control when to print ANSI control characters ("never"\|"always"\|"auto") |
4343
| `--compatibility` | | | Run compose in backward compatibility mode |
44-
| `--dry-run` | | | Execute command in dry run mode |
4544
| `--env-file` | `string` | | Specify an alternate environment file. |
4645
| `-f`, `--file` | `stringArray` | | Compose configuration files |
4746
| `--parallel` | `int` | `-1` | Control max parallelism, -1 for unlimited |

docs/reference/docker_compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ options:
183183
default_value: "false"
184184
description: Execute command in dry run mode
185185
deprecated: false
186-
hidden: false
186+
hidden: true
187187
experimental: false
188188
experimentalcli: false
189189
kubernetes: false

pkg/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type Service interface {
7878
// MaxConcurrency defines upper limit for concurrent operations against engine API
7979
MaxConcurrency(parallel int)
8080
// DryRunMode defines if dry run applies to the command
81-
DryRunMode(dryRun bool)
81+
DryRunMode(dryRun bool) error
8282
// Watch services' development context and sync/notify/rebuild/restart on changes
8383
Watch(ctx context.Context, project *types.Project, services []string, options WatchOptions) error
8484
}

pkg/api/proxy.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type ServiceProxy struct {
5252
ImagesFn func(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error)
5353
WatchFn func(ctx context.Context, project *types.Project, services []string, options WatchOptions) error
5454
MaxConcurrencyFn func(parallel int)
55-
DryRunModeFn func(dryRun bool)
55+
DryRunModeFn func(dryRun bool) error
5656
interceptors []Interceptor
5757
}
5858

@@ -92,6 +92,7 @@ func (s *ServiceProxy) WithService(service Service) *ServiceProxy {
9292
s.ImagesFn = service.Images
9393
s.WatchFn = service.Watch
9494
s.MaxConcurrencyFn = service.MaxConcurrency
95+
s.DryRunModeFn = service.DryRunMode
9596
return s
9697
}
9798

@@ -326,6 +327,6 @@ func (s *ServiceProxy) MaxConcurrency(i int) {
326327
s.MaxConcurrencyFn(i)
327328
}
328329

329-
func (s *ServiceProxy) DryRunMode(dryRun bool) {
330-
s.DryRunModeFn(dryRun)
330+
func (s *ServiceProxy) DryRunMode(dryRun bool) error {
331+
return s.DryRunModeFn(dryRun)
331332
}

pkg/compose/compose.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23-
"io"
24-
"strings"
25-
2623
"github.com/compose-spec/compose-go/types"
2724
"github.com/distribution/distribution/v3/reference"
2825
"github.com/docker/cli/cli/command"
2926
"github.com/docker/cli/cli/config/configfile"
27+
"github.com/docker/cli/cli/flags"
3028
"github.com/docker/cli/cli/streams"
3129
moby "github.com/docker/docker/api/types"
3230
"github.com/docker/docker/api/types/filters"
3331
"github.com/docker/docker/client"
3432
"github.com/opencontainers/go-digest"
3533
"github.com/pkg/errors"
3634
"gopkg.in/yaml.v2"
35+
"io"
36+
"strings"
3737

3838
"github.com/docker/compose/v2/pkg/api"
3939
)
@@ -65,8 +65,20 @@ func (s *composeService) MaxConcurrency(i int) {
6565
s.maxConcurrency = i
6666
}
6767

68-
func (s *composeService) DryRunMode(dryRun bool) {
69-
s.dryRun = dryRun
68+
func (s *composeService) DryRunMode(dryRun bool) error {
69+
if dryRun {
70+
cli, err := command.NewDockerCli()
71+
if err != nil {
72+
return err
73+
}
74+
cli.Initialize(flags.NewClientOptions(), command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) {
75+
dryRunClient := api.NewDryRunClient()
76+
dryRunClient.WithAPIClient(s.apiClient())
77+
return dryRunClient, nil
78+
}))
79+
s.dockerCli = cli
80+
}
81+
return nil
7082
}
7183

7284
func (s *composeService) stdout() *streams.Out {

pkg/mocks/mock_docker_compose_api.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)