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 {
backend.MaxConcurrency(parallel)
}
backend.DryRunMode(dryRun)
return nil
return backend.DryRunMode(dryRun)
},
}

View File

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

View File

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

View File

@ -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
}

View File

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

View File

@ -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 {

View File

@ -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.