mirror of
https://github.com/docker/compose.git
synced 2026-10-12 02:13:08 +00:00
fix(bridge): reject --dry-run on bridge commands
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 <[email protected]>
This commit is contained in:
committed by
Nicolas De loof
parent
7acb2b9f25
commit
4e1cd3f4e1
+19
-6
@@ -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")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user