create custom CLI when dry-run mode active

update documentation

Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
This commit is contained in:
Guillaume Lours 2023-01-12 23:31:14 +01:00
parent 13ef440d6a
commit 5081ab0507
7 changed files with 28 additions and 15 deletions

View File

@ -336,8 +336,7 @@ func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //no
if parallel > 0 { if parallel > 0 {
backend.MaxConcurrency(parallel) backend.MaxConcurrency(parallel)
} }
backend.DryRunMode(dryRun) return backend.DryRunMode(dryRun)
return nil
}, },
} }

View File

@ -41,7 +41,6 @@ Docker Compose
|:-----------------------|:--------------|:--------|:----------------------------------------------------------------------------------------------------| |:-----------------------|:--------------|:--------|:----------------------------------------------------------------------------------------------------|
| `--ansi` | `string` | `auto` | Control when to print ANSI control characters ("never"\|"always"\|"auto") | | `--ansi` | `string` | `auto` | Control when to print ANSI control characters ("never"\|"always"\|"auto") |
| `--compatibility` | | | Run compose in backward compatibility mode | | `--compatibility` | | | Run compose in backward compatibility mode |
| `--dry-run` | | | Execute command in dry run mode |
| `--env-file` | `string` | | Specify an alternate environment file. | | `--env-file` | `string` | | Specify an alternate environment file. |
| `-f`, `--file` | `stringArray` | | Compose configuration files | | `-f`, `--file` | `stringArray` | | Compose configuration files |
| `--parallel` | `int` | `-1` | Control max parallelism, -1 for unlimited | | `--parallel` | `int` | `-1` | Control max parallelism, -1 for unlimited |

View File

@ -183,7 +183,7 @@ options:
default_value: "false" default_value: "false"
description: Execute command in dry run mode description: Execute command in dry run mode
deprecated: false deprecated: false
hidden: false hidden: true
experimental: false experimental: false
experimentalcli: false experimentalcli: false
kubernetes: false kubernetes: false

View File

@ -78,7 +78,7 @@ type Service interface {
// MaxConcurrency defines upper limit for concurrent operations against engine API // MaxConcurrency defines upper limit for concurrent operations against engine API
MaxConcurrency(parallel int) MaxConcurrency(parallel int)
// DryRunMode defines if dry run applies to the command // 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 services' development context and sync/notify/rebuild/restart on changes
Watch(ctx context.Context, project *types.Project, services []string, options WatchOptions) error Watch(ctx context.Context, project *types.Project, services []string, options WatchOptions) error
} }

View File

@ -52,7 +52,7 @@ type ServiceProxy struct {
ImagesFn func(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error) ImagesFn func(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error)
WatchFn func(ctx context.Context, project *types.Project, services []string, options WatchOptions) error WatchFn func(ctx context.Context, project *types.Project, services []string, options WatchOptions) error
MaxConcurrencyFn func(parallel int) MaxConcurrencyFn func(parallel int)
DryRunModeFn func(dryRun bool) DryRunModeFn func(dryRun bool) error
interceptors []Interceptor interceptors []Interceptor
} }
@ -92,6 +92,7 @@ func (s *ServiceProxy) WithService(service Service) *ServiceProxy {
s.ImagesFn = service.Images s.ImagesFn = service.Images
s.WatchFn = service.Watch s.WatchFn = service.Watch
s.MaxConcurrencyFn = service.MaxConcurrency s.MaxConcurrencyFn = service.MaxConcurrency
s.DryRunModeFn = service.DryRunMode
return s return s
} }
@ -326,6 +327,6 @@ func (s *ServiceProxy) MaxConcurrency(i int) {
s.MaxConcurrencyFn(i) s.MaxConcurrencyFn(i)
} }
func (s *ServiceProxy) DryRunMode(dryRun bool) { func (s *ServiceProxy) DryRunMode(dryRun bool) error {
s.DryRunModeFn(dryRun) return s.DryRunModeFn(dryRun)
} }

View File

@ -20,13 +20,11 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"strings"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"github.com/distribution/distribution/v3/reference" "github.com/distribution/distribution/v3/reference"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"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/streams" "github.com/docker/cli/cli/streams"
moby "github.com/docker/docker/api/types" moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
@ -34,6 +32,8 @@ import (
"github.com/opencontainers/go-digest" "github.com/opencontainers/go-digest"
"github.com/pkg/errors" "github.com/pkg/errors"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io"
"strings"
"github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/api"
) )
@ -65,8 +65,20 @@ func (s *composeService) MaxConcurrency(i int) {
s.maxConcurrency = i s.maxConcurrency = i
} }
func (s *composeService) DryRunMode(dryRun bool) { func (s *composeService) DryRunMode(dryRun bool) error {
s.dryRun = dryRun 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 { func (s *composeService) stdout() *streams.Out {

View File

@ -108,9 +108,11 @@ func (mr *MockServiceMockRecorder) Down(ctx, projectName, options interface{}) *
} }
// DryRunMode mocks base method. // DryRunMode mocks base method.
func (m *MockService) DryRunMode(dryRun bool) { func (m *MockService) DryRunMode(dryRun bool) error {
m.ctrl.T.Helper() 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. // DryRunMode indicates an expected call of DryRunMode.