diff --git a/cmd/compose/alpha.go b/cmd/compose/alpha.go index 6f4277008..b4930f6e6 100644 --- a/cmd/compose/alpha.go +++ b/cmd/compose/alpha.go @@ -15,6 +15,8 @@ package compose import ( + "context" + "github.com/docker/compose/v2/pkg/api" "github.com/spf13/cobra" ) @@ -29,6 +31,27 @@ func alphaCommand(p *ProjectOptions, backend api.Service) *cobra.Command { "experimentalCLI": "true", }, } - cmd.AddCommand(watchCommand(p, backend)) + cmd.AddCommand( + watchCommand(p, backend), + dryRunRedirectCommand(p), + ) + return cmd +} + +// Temporary alpha command as the dry-run will be implemented with a flag +func dryRunRedirectCommand(p *ProjectOptions) *cobra.Command { + cmd := &cobra.Command{ + Use: "dry-run -- [COMMAND...]", + Short: "EXPERIMENTAL - Dry run command allow you to test a command without applying changes", + PreRunE: Adapt(func(ctx context.Context, args []string) error { + return nil + }), + RunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error { + rootCmd := cmd.Root() + rootCmd.SetArgs(append([]string{"compose", "--dry-run"}, args...)) + return rootCmd.Execute() + }), + ValidArgsFunction: completeServiceNames(p), + } return cmd } diff --git a/docs/reference/compose_alpha.md b/docs/reference/compose_alpha.md index b3b7ec056..1abd1e839 100644 --- a/docs/reference/compose_alpha.md +++ b/docs/reference/compose_alpha.md @@ -5,9 +5,10 @@ Experimental commands ### Subcommands -| Name | Description | -|:----------------------------------|:-----------------------------------------------------------------------------------------------------| -| [`watch`](compose_alpha_watch.md) | EXPERIMENTAL - Watch build context for service and rebuild/refresh containers when files are updated | +| Name | Description | +|:--------------------------------------|:-----------------------------------------------------------------------------------------------------| +| [`dry-run`](compose_alpha_dry-run.md) | EXPERIMENTAL - Dry run command allow you to test a command without applying changes | +| [`watch`](compose_alpha_watch.md) | EXPERIMENTAL - Watch build context for service and rebuild/refresh containers when files are updated | diff --git a/docs/reference/compose_alpha_dry-run.md b/docs/reference/compose_alpha_dry-run.md new file mode 100644 index 000000000..a66ed5340 --- /dev/null +++ b/docs/reference/compose_alpha_dry-run.md @@ -0,0 +1,8 @@ +# docker compose alpha dry-run + + +EXPERIMENTAL - Dry run command allow you to test a command without applying changes + + + + diff --git a/docs/reference/docker_compose_alpha.yaml b/docs/reference/docker_compose_alpha.yaml index 22ca5f98b..a8a83b6e7 100644 --- a/docs/reference/docker_compose_alpha.yaml +++ b/docs/reference/docker_compose_alpha.yaml @@ -4,8 +4,10 @@ long: Experimental commands pname: docker compose plink: docker_compose.yaml cname: + - docker compose alpha dry-run - docker compose alpha watch clink: + - docker_compose_alpha_dry-run.yaml - docker_compose_alpha_watch.yaml deprecated: false experimental: false diff --git a/docs/reference/docker_compose_alpha_dry-run.yaml b/docs/reference/docker_compose_alpha_dry-run.yaml new file mode 100644 index 000000000..d489d39ae --- /dev/null +++ b/docs/reference/docker_compose_alpha_dry-run.yaml @@ -0,0 +1,14 @@ +command: docker compose alpha dry-run +short: | + EXPERIMENTAL - Dry run command allow you to test a command without applying changes +long: | + EXPERIMENTAL - Dry run command allow you to test a command without applying changes +usage: docker compose alpha dry-run -- [COMMAND...] +pname: docker compose alpha +plink: docker_compose_alpha.yaml +deprecated: false +experimental: false +experimentalcli: true +kubernetes: false +swarm: false + diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index 05db272d8..033a340ef 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -20,6 +20,9 @@ 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" @@ -32,8 +35,6 @@ import ( "github.com/opencontainers/go-digest" "github.com/pkg/errors" "gopkg.in/yaml.v2" - "io" - "strings" "github.com/docker/compose/v2/pkg/api" ) @@ -71,11 +72,14 @@ func (s *composeService) DryRunMode(dryRun bool) error { if err != nil { return err } - cli.Initialize(flags.NewClientOptions(), command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) { + err = cli.Initialize(flags.NewClientOptions(), command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) { dryRunClient := api.NewDryRunClient() dryRunClient.WithAPIClient(s.apiClient()) return dryRunClient, nil })) + if err != nil { + return err + } s.dockerCli = cli } return nil