Skip to content

Commit eb59b0e

Browse files
committed
add alpha command to test dry-run
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 5081ab0 commit eb59b0e

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
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
}

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_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/compose/compose.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23+
"io"
24+
"strings"
25+
2326
"github.com/compose-spec/compose-go/types"
2427
"github.com/distribution/distribution/v3/reference"
2528
"github.com/docker/cli/cli/command"
@@ -32,8 +35,6 @@ import (
3235
"github.com/opencontainers/go-digest"
3336
"github.com/pkg/errors"
3437
"gopkg.in/yaml.v2"
35-
"io"
36-
"strings"
3738

3839
"github.com/docker/compose/v2/pkg/api"
3940
)
@@ -71,11 +72,14 @@ func (s *composeService) DryRunMode(dryRun bool) error {
7172
if err != nil {
7273
return err
7374
}
74-
cli.Initialize(flags.NewClientOptions(), command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) {
75+
err = cli.Initialize(flags.NewClientOptions(), command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) {
7576
dryRunClient := api.NewDryRunClient()
7677
dryRunClient.WithAPIClient(s.apiClient())
7778
return dryRunClient, nil
7879
}))
80+
if err != nil {
81+
return err
82+
}
7983
s.dockerCli = cli
8084
}
8185
return nil

0 commit comments

Comments
 (0)