Browse Source

add alpha command to test dry-run

Signed-off-by: Guillaume Lours <[email protected]>
Guillaume Lours 2 years ago
parent
commit
eb59b0e265

+ 24 - 1
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
 }

+ 4 - 3
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 |
 
 
 

+ 8 - 0
docs/reference/compose_alpha_dry-run.md

@@ -0,0 +1,8 @@
+# docker compose alpha dry-run
+
+<!---MARKER_GEN_START-->
+EXPERIMENTAL - Dry run command allow you to test a command without applying changes
+
+
+<!---MARKER_GEN_END-->
+

+ 2 - 0
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

+ 14 - 0
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
+

+ 7 - 3
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