mirror of
https://github.com/docker/compose.git
synced 2026-11-17 05:36:08 +00:00
fix(bridge): reject --dry-run on bridge commands
Some checks are pending
ci / validate (lint) (push) Waiting to run
ci / validate (relay-lint) (push) Waiting to run
ci / validate (relay-test) (push) Waiting to run
ci / validate (validate-docs) (push) Waiting to run
ci / validate (validate-go-mod) (push) Waiting to run
ci / validate (validate-headers) (push) Waiting to run
ci / validate (validate-mocks) (push) Waiting to run
ci / binary (push) Waiting to run
ci / binary-finalize (push) Blocked by required conditions
ci / bin-image-test (push) Waiting to run
ci / relay-image-test (push) Waiting to run
ci / test (push) Waiting to run
ci / e2e (plugin, oldstable, graphdriver) (push) Waiting to run
ci / e2e (standalone, oldstable, graphdriver) (push) Waiting to run
ci / e2e (plugin, stable, containerd) (push) Waiting to run
ci / e2e (plugin, stable, graphdriver) (push) Waiting to run
ci / e2e (standalone, stable, containerd) (push) Waiting to run
ci / e2e (standalone, stable, graphdriver) (push) Waiting to run
ci / coverage (push) Blocked by required conditions
ci / release (push) Blocked by required conditions
merge / bin-image-prepare (push) Waiting to run
merge / bin-image (push) Blocked by required conditions
merge / relay-image (push) Waiting to run
merge / module-image (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run
zizmor / zizmor (push) Waiting to run
Some checks are pending
ci / validate (lint) (push) Waiting to run
ci / validate (relay-lint) (push) Waiting to run
ci / validate (relay-test) (push) Waiting to run
ci / validate (validate-docs) (push) Waiting to run
ci / validate (validate-go-mod) (push) Waiting to run
ci / validate (validate-headers) (push) Waiting to run
ci / validate (validate-mocks) (push) Waiting to run
ci / binary (push) Waiting to run
ci / binary-finalize (push) Blocked by required conditions
ci / bin-image-test (push) Waiting to run
ci / relay-image-test (push) Waiting to run
ci / test (push) Waiting to run
ci / e2e (plugin, oldstable, graphdriver) (push) Waiting to run
ci / e2e (standalone, oldstable, graphdriver) (push) Waiting to run
ci / e2e (plugin, stable, containerd) (push) Waiting to run
ci / e2e (plugin, stable, graphdriver) (push) Waiting to run
ci / e2e (standalone, stable, containerd) (push) Waiting to run
ci / e2e (standalone, stable, graphdriver) (push) Waiting to run
ci / coverage (push) Blocked by required conditions
ci / release (push) Blocked by required conditions
merge / bin-image-prepare (push) Waiting to run
merge / bin-image (push) Blocked by required conditions
merge / relay-image (push) Waiting to run
merge / module-image (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run
zizmor / zizmor (push) Waiting to run
The bridge commands never go through the dry-run backend, so the inherited --dry-run flag was silently ignored: `bridge convert` still wiped and rewrote the output directory, pulled the transformer image and ran it, and `bridge transformations create` still wrote its destination folder. Fail fast with an explicit error before any side effect instead of pretending to honor the flag. Signed-off-by: Guillaume Lours <glours@users.noreply.github.com>
This commit is contained in:
parent
7acb2b9f25
commit
4e1cd3f4e1
2 changed files with 38 additions and 6 deletions
|
|
@ -63,13 +63,24 @@ func rejectUnknownSubcommand(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
}
|
||||
|
||||
// rejectDryRun is a PreRunE: bridge commands write to the local filesystem and
|
||||
// don't go through the dry-run backend, so honoring the flag silently would
|
||||
// still modify (or delete) files.
|
||||
func rejectDryRun(cmd *cobra.Command, _ []string) error {
|
||||
if dryRun, _ := cmd.Flags().GetBool("dry-run"); dryRun {
|
||||
return fmt.Errorf("--dry-run is not supported by %q", cmd.CommandPath())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func convertCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
|
||||
convertOpts := bridge.ConvertOptions{}
|
||||
var assumeYes bool
|
||||
cmd := &cobra.Command{
|
||||
Use: "convert",
|
||||
Short: "Convert compose files to Kubernetes manifests, Helm charts, or another model",
|
||||
Args: cobra.NoArgs,
|
||||
Use: "convert",
|
||||
Short: "Convert compose files to Kubernetes manifests, Helm charts, or another model",
|
||||
Args: cobra.NoArgs,
|
||||
PreRunE: rejectDryRun,
|
||||
RunE: Adapt(func(ctx context.Context, args []string) error {
|
||||
if assumeYes {
|
||||
convertOpts.Confirm = func(string, bool) (bool, error) { return true, nil }
|
||||
|
|
@ -121,6 +132,7 @@ func listTransformersCommand(dockerCli command.Cli) *cobra.Command {
|
|||
Aliases: []string{"ls"},
|
||||
Short: "List available transformations",
|
||||
Args: cobra.NoArgs,
|
||||
PreRunE: rejectDryRun,
|
||||
RunE: Adapt(func(ctx context.Context, args []string) error {
|
||||
transformers, err := bridge.ListTransformers(ctx, dockerCli)
|
||||
if err != nil {
|
||||
|
|
@ -171,9 +183,10 @@ func displayTransformer(dockerCli command.Cli, transformers []image.Summary, opt
|
|||
func createTransformerCommand(dockerCli command.Cli) *cobra.Command {
|
||||
var opts bridge.CreateTransformerOptions
|
||||
cmd := &cobra.Command{
|
||||
Use: "create [OPTION] PATH",
|
||||
Short: "Create a new transformation",
|
||||
Args: cli.ExactArgs(1),
|
||||
Use: "create [OPTION] PATH",
|
||||
Short: "Create a new transformation",
|
||||
Args: cli.ExactArgs(1),
|
||||
PreRunE: rejectDryRun,
|
||||
RunE: Adapt(func(ctx context.Context, args []string) error {
|
||||
opts.Dest = args[0]
|
||||
return bridge.CreateTransformer(ctx, dockerCli, opts)
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package compose
|
|||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -93,3 +94,21 @@ func TestBridgeCommandsArgsValidation(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBridgeCommandsRejectDryRun(t *testing.T) {
|
||||
for _, args := range [][]string{
|
||||
{"convert"},
|
||||
{"transformations", "create", "dest"},
|
||||
{"transformations", "list"},
|
||||
} {
|
||||
t.Run(strings.Join(args, " "), func(t *testing.T) {
|
||||
root := &cobra.Command{Use: "compose", SilenceUsage: true, SilenceErrors: true}
|
||||
root.PersistentFlags().Bool("dry-run", false, "")
|
||||
root.AddCommand(bridgeCommand(&ProjectOptions{}, nil))
|
||||
root.SetArgs(append([]string{"bridge", "--dry-run"}, args...))
|
||||
root.SetOut(io.Discard)
|
||||
root.SetErr(io.Discard)
|
||||
assert.ErrorContains(t, root.Execute(), "--dry-run is not supported")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue