From fbf845c5f81e3d8099c307dafd02fbeb6c089565 Mon Sep 17 00:00:00 2001 From: Guillaume Lours <705411+glours@users.noreply.github.com> Date: Wed, 11 Jan 2023 16:38:57 +0100 Subject: [PATCH] add dry-run flag Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com> --- cmd/compose/compose.go | 4 ++++ docs/reference/compose.md | 1 + docs/reference/docker_compose.yaml | 10 ++++++++++ pkg/api/api.go | 2 ++ pkg/api/proxy.go | 5 +++++ pkg/compose/compose.go | 6 ++++++ 6 files changed, 28 insertions(+) diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 120726f07..a76999d77 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -261,6 +261,7 @@ func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //no verbose bool version bool parallel int + dryRun bool ) c := &cobra.Command{ Short: "Docker Compose", @@ -335,6 +336,7 @@ func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //no if parallel > 0 { backend.MaxConcurrency(parallel) } + backend.DryRunMode(dryRun) return nil }, } @@ -389,6 +391,8 @@ func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //no c.Flags().MarkHidden("no-ansi") //nolint:errcheck c.Flags().BoolVar(&verbose, "verbose", false, "Show more output") c.Flags().MarkHidden("verbose") //nolint:errcheck + c.Flags().BoolVar(&dryRun, "dry-run", false, "Execute command in dry run mode") + c.Flags().MarkHidden("dry-run") //nolint:errcheck return c } diff --git a/docs/reference/compose.md b/docs/reference/compose.md index 8ca43277d..4165918de 100644 --- a/docs/reference/compose.md +++ b/docs/reference/compose.md @@ -41,6 +41,7 @@ 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 9709ef880..ddf3d3520 100644 --- a/docs/reference/docker_compose.yaml +++ b/docs/reference/docker_compose.yaml @@ -178,6 +178,16 @@ options: experimentalcli: false kubernetes: false swarm: false + - option: dry-run + value_type: bool + default_value: "false" + description: Execute command in dry run mode + deprecated: false + hidden: false + experimental: false + experimentalcli: false + kubernetes: false + swarm: false - option: env-file value_type: string description: Specify an alternate environment file. diff --git a/pkg/api/api.go b/pkg/api/api.go index baded095f..c957a9850 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -77,6 +77,8 @@ type Service interface { Images(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error) // 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) // 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 34acd2cc9..76ce0c153 100644 --- a/pkg/api/proxy.go +++ b/pkg/api/proxy.go @@ -52,6 +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) interceptors []Interceptor } @@ -324,3 +325,7 @@ func (s *ServiceProxy) Watch(ctx context.Context, project *types.Project, servic func (s *ServiceProxy) MaxConcurrency(i int) { s.MaxConcurrencyFn(i) } + +func (s *ServiceProxy) DryRunMode(dryRun bool) { + s.DryRunModeFn(dryRun) +} diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index adc282303..8cf820fdf 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -43,12 +43,14 @@ func NewComposeService(dockerCli command.Cli) api.Service { return &composeService{ dockerCli: dockerCli, maxConcurrency: -1, + dryRun: false, } } type composeService struct { dockerCli command.Cli maxConcurrency int + dryRun bool } func (s *composeService) apiClient() client.APIClient { @@ -63,6 +65,10 @@ func (s *composeService) MaxConcurrency(i int) { s.maxConcurrency = i } +func (s *composeService) DryRunMode(dryRun bool) { + s.dryRun = dryRun +} + func (s *composeService) stdout() *streams.Out { return s.dockerCli.Out() }