add alpha command to test dry-run

Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
This commit is contained in:
Guillaume Lours 2023-01-13 14:26:28 +01:00
parent 5081ab0507
commit eb59b0e265
6 changed files with 59 additions and 7 deletions

View File

@ -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
}

View File

@ -6,7 +6,8 @@ Experimental commands
### Subcommands
| 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 |

View File

@ -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-->

View File

@ -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

View File

@ -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

View File

@ -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