Skip to content

Commit d5d9f67

Browse files
authored
Merge pull request docker#10173 from glours/dry-run
Skeleton for dry-run under alpha command
2 parents a2899d5 + fb36f7f commit d5d9f67

File tree

12 files changed

+651
-6
lines changed

12 files changed

+651
-6
lines changed

cmd/compose/alpha.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package compose
1616

1717
import (
18+
"context"
19+
1820
"github.com/docker/compose/v2/pkg/api"
1921
"github.com/spf13/cobra"
2022
)
@@ -29,6 +31,27 @@ func alphaCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
2931
"experimentalCLI": "true",
3032
},
3133
}
32-
cmd.AddCommand(watchCommand(p, backend))
34+
cmd.AddCommand(
35+
watchCommand(p, backend),
36+
dryRunRedirectCommand(p),
37+
)
38+
return cmd
39+
}
40+
41+
// Temporary alpha command as the dry-run will be implemented with a flag
42+
func dryRunRedirectCommand(p *ProjectOptions) *cobra.Command {
43+
cmd := &cobra.Command{
44+
Use: "dry-run -- [COMMAND...]",
45+
Short: "EXPERIMENTAL - Dry run command allow you to test a command without applying changes",
46+
PreRunE: Adapt(func(ctx context.Context, args []string) error {
47+
return nil
48+
}),
49+
RunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
50+
rootCmd := cmd.Root()
51+
rootCmd.SetArgs(append([]string{"compose", "--dry-run"}, args...))
52+
return rootCmd.Execute()
53+
}),
54+
ValidArgsFunction: completeServiceNames(p),
55+
}
3356
return cmd
3457
}

cmd/compose/compose.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"strings"
2727
"syscall"
2828

29+
"github.com/docker/cli/cli/command"
30+
2931
"github.com/compose-spec/compose-go/cli"
3032
"github.com/compose-spec/compose-go/types"
3133
composegoutils "github.com/compose-spec/compose-go/utils"
@@ -243,7 +245,7 @@ func RunningAsStandalone() bool {
243245
}
244246

245247
// RootCommand returns the compose command with its child commands
246-
func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //nolint:gocyclo
248+
func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //nolint:gocyclo
247249
// filter out useless commandConn.CloseWrite warning message that can occur
248250
// when using a remote context that is unreachable: "commandConn.CloseWrite: commandconn: failed to wait: signal: killed"
249251
// https://github.com/docker/cli/blob/e1f24d3c93df6752d3c27c8d61d18260f141310c/cli/connhelper/commandconn/commandconn.go#L203-L215
@@ -261,6 +263,7 @@ func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //no
261263
verbose bool
262264
version bool
263265
parallel int
266+
dryRun bool
264267
)
265268
c := &cobra.Command{
266269
Short: "Docker Compose",
@@ -335,7 +338,7 @@ func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //no
335338
if parallel > 0 {
336339
backend.MaxConcurrency(parallel)
337340
}
338-
return nil
341+
return backend.DryRunMode(dryRun)
339342
},
340343
}
341344

@@ -389,6 +392,8 @@ func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //no
389392
c.Flags().MarkHidden("no-ansi") //nolint:errcheck
390393
c.Flags().BoolVar(&verbose, "verbose", false, "Show more output")
391394
c.Flags().MarkHidden("verbose") //nolint:errcheck
395+
c.Flags().BoolVar(&dryRun, "dry-run", false, "Execute command in dry run mode")
396+
c.Flags().MarkHidden("dry-run") //nolint:errcheck
392397
return c
393398
}
394399

docs/reference/compose_alpha.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ Experimental commands
55

66
### Subcommands
77

8-
| Name | Description |
9-
|:----------------------------------|:-----------------------------------------------------------------------------------------------------|
10-
| [`watch`](compose_alpha_watch.md) | EXPERIMENTAL - Watch build context for service and rebuild/refresh containers when files are updated |
8+
| Name | Description |
9+
|:--------------------------------------|:-----------------------------------------------------------------------------------------------------|
10+
| [`dry-run`](compose_alpha_dry-run.md) | EXPERIMENTAL - Dry run command allow you to test a command without applying changes |
11+
| [`watch`](compose_alpha_watch.md) | EXPERIMENTAL - Watch build context for service and rebuild/refresh containers when files are updated |
1112

1213

1314

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# docker compose alpha dry-run
2+
3+
<!---MARKER_GEN_START-->
4+
EXPERIMENTAL - Dry run command allow you to test a command without applying changes
5+
6+
7+
<!---MARKER_GEN_END-->
8+

docs/reference/docker_compose.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ options:
178178
experimentalcli: false
179179
kubernetes: false
180180
swarm: false
181+
- option: dry-run
182+
value_type: bool
183+
default_value: "false"
184+
description: Execute command in dry run mode
185+
deprecated: false
186+
hidden: true
187+
experimental: false
188+
experimentalcli: false
189+
kubernetes: false
190+
swarm: false
181191
- option: env-file
182192
value_type: string
183193
description: Specify an alternate environment file.

docs/reference/docker_compose_alpha.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ long: Experimental commands
44
pname: docker compose
55
plink: docker_compose.yaml
66
cname:
7+
- docker compose alpha dry-run
78
- docker compose alpha watch
89
clink:
10+
- docker_compose_alpha_dry-run.yaml
911
- docker_compose_alpha_watch.yaml
1012
deprecated: false
1113
experimental: false
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
command: docker compose alpha dry-run
2+
short: |
3+
EXPERIMENTAL - Dry run command allow you to test a command without applying changes
4+
long: |
5+
EXPERIMENTAL - Dry run command allow you to test a command without applying changes
6+
usage: docker compose alpha dry-run -- [COMMAND...]
7+
pname: docker compose alpha
8+
plink: docker_compose_alpha.yaml
9+
deprecated: false
10+
experimental: false
11+
experimentalcli: true
12+
kubernetes: false
13+
swarm: false
14+

pkg/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ type Service interface {
7777
Images(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error)
7878
// MaxConcurrency defines upper limit for concurrent operations against engine API
7979
MaxConcurrency(parallel int)
80+
// DryRunMode defines if dry run applies to the command
81+
DryRunMode(dryRun bool) error
8082
// Watch services' development context and sync/notify/rebuild/restart on changes
8183
Watch(ctx context.Context, project *types.Project, services []string, options WatchOptions) error
8284
}

0 commit comments

Comments
 (0)