Skip to content

Commit

Permalink
Merge pull request #10173 from glours/dry-run
Browse files Browse the repository at this point in the history
Skeleton for dry-run under alpha command
  • Loading branch information
glours committed Jan 20, 2023
2 parents a2899d5 + fb36f7f commit d5d9f67
Show file tree
Hide file tree
Showing 12 changed files with 651 additions and 6 deletions.
25 changes: 24 additions & 1 deletion cmd/compose/alpha.go
Expand Up @@ -15,6 +15,8 @@
package compose

import (
"context"

"github.com/docker/compose/v2/pkg/api"
"github.com/spf13/cobra"
)
Expand All @@ -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
}
9 changes: 7 additions & 2 deletions cmd/compose/compose.go
Expand Up @@ -26,6 +26,8 @@ import (
"strings"
"syscall"

"github.com/docker/cli/cli/command"

"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/types"
composegoutils "github.com/compose-spec/compose-go/utils"
Expand Down Expand Up @@ -243,7 +245,7 @@ func RunningAsStandalone() bool {
}

// RootCommand returns the compose command with its child commands
func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //nolint:gocyclo
func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //nolint:gocyclo
// filter out useless commandConn.CloseWrite warning message that can occur
// when using a remote context that is unreachable: "commandConn.CloseWrite: commandconn: failed to wait: signal: killed"
// https://github.com/docker/cli/blob/e1f24d3c93df6752d3c27c8d61d18260f141310c/cli/connhelper/commandconn/commandconn.go#L203-L215
Expand All @@ -261,6 +263,7 @@ func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //no
verbose bool
version bool
parallel int
dryRun bool
)
c := &cobra.Command{
Short: "Docker Compose",
Expand Down Expand Up @@ -335,7 +338,7 @@ func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //no
if parallel > 0 {
backend.MaxConcurrency(parallel)
}
return nil
return backend.DryRunMode(dryRun)
},
}

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

Expand Down
7 changes: 4 additions & 3 deletions docs/reference/compose_alpha.md
Expand Up @@ -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 |



Expand Down
8 changes: 8 additions & 0 deletions 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-->

10 changes: 10 additions & 0 deletions docs/reference/docker_compose.yaml
Expand Up @@ -178,6 +178,16 @@ options:
experimentalcli: false
kubernetes: false
swarm: false
- option: dry-run
value_type: bool
default_value: "false"
description: Execute command in dry run mode
deprecated: false
hidden: true
experimental: false
experimentalcli: false
kubernetes: false
swarm: false
- option: env-file
value_type: string
description: Specify an alternate environment file.
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/docker_compose_alpha.yaml
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions 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

2 changes: 2 additions & 0 deletions pkg/api/api.go
Expand Up @@ -77,6 +77,8 @@ type Service interface {
Images(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error)
// MaxConcurrency defines upper limit for concurrent operations against engine API
MaxConcurrency(parallel int)
// DryRunMode defines if dry run applies to the command
DryRunMode(dryRun bool) error
// Watch services' development context and sync/notify/rebuild/restart on changes
Watch(ctx context.Context, project *types.Project, services []string, options WatchOptions) error
}
Expand Down

0 comments on commit d5d9f67

Please sign in to comment.