From 6e1e614a56d6ea18e99484638de9f21d1fd49ba7 Mon Sep 17 00:00:00 2001 From: Laura Martin Date: Thu, 1 Feb 2018 00:14:42 +0000 Subject: [PATCH] Change -force to -auto-approve when destroying Since an early version of Terraform, the `destroy` command has always had the `-force` flag to allow an auto approval of the interactive prompt. 0.11 introduced `-auto-approve` as default to `false` when using the `apply` command. The `-auto-approve` flag was introduced to reduce ambiguity of it's function, but the `-force` flag was never updated for a destroy. People often use wrappers when automating commands in Terraform, and the inconsistency between `apply` and `destroy` means that additional logic must be added to the wrappers to do similar functions. Both commands are more or less able to run with similar syntax, and also heavily share their code. This commit updates the command in `destroy` to use the `-auto-approve` flag making working with the Terraform CLI a more consistent experience. We leave in `-force` in `destroy` for the time-being and flag it as deprecated to ensure a safe switchover period. --- backend/local/backend_apply.go | 2 +- command/apply.go | 12 +++++------- command/apply_destroy_test.go | 6 +++--- command/e2etest/primary_test.go | 2 +- website/docs/commands/destroy.html.markdown | 2 +- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/backend/local/backend_apply.go b/backend/local/backend_apply.go index 9789e0b7c..49d102dea 100644 --- a/backend/local/backend_apply.go +++ b/backend/local/backend_apply.go @@ -99,7 +99,7 @@ func (b *Local) opApply( dispPlan := format.NewPlan(plan) trivialPlan := dispPlan.Empty() hasUI := op.UIOut != nil && op.UIIn != nil - mustConfirm := hasUI && ((op.Destroy && !op.DestroyForce) || (!op.Destroy && !op.AutoApprove && !trivialPlan)) + mustConfirm := hasUI && ((op.Destroy && (!op.DestroyForce && !op.AutoApprove)) || (!op.Destroy && !op.AutoApprove && !trivialPlan)) if mustConfirm { var desc, query string if op.Destroy { diff --git a/command/apply.go b/command/apply.go index c65b2df51..d4253aed1 100644 --- a/command/apply.go +++ b/command/apply.go @@ -40,13 +40,11 @@ func (c *ApplyCommand) Run(args []string) int { } cmdFlags := c.Meta.flagSet(cmdName) + cmdFlags.BoolVar(&autoApprove, "auto-approve", false, "skip interactive approval of plan before applying") if c.Destroy { - cmdFlags.BoolVar(&destroyForce, "force", false, "force") + cmdFlags.BoolVar(&destroyForce, "force", false, "deprecated: same as auto-approve") } cmdFlags.BoolVar(&refresh, "refresh", true, "refresh") - if !c.Destroy { - cmdFlags.BoolVar(&autoApprove, "auto-approve", false, "skip interactive approval of plan before applying") - } cmdFlags.IntVar( &c.Meta.parallelism, "parallelism", DefaultParallelism, "parallelism") cmdFlags.StringVar(&c.Meta.statePath, "state", "", "path") @@ -250,8 +248,6 @@ Options: -lock-timeout=0s Duration to retry a state lock. - -auto-approve Skip interactive approval of plan before applying. - -input=true Ask for input for variables if not directly set. -no-color If specified, output won't contain any color. @@ -297,7 +293,9 @@ Options: modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup. - -force Don't ask for input for destroy confirmation. + -auto-approve Skip interactive approval before destroying. + + -force Deprecated: same as auto-approve. -lock=true Lock the state file when locking is supported. diff --git a/command/apply_destroy_test.go b/command/apply_destroy_test.go index c2d399e72..e9779190b 100644 --- a/command/apply_destroy_test.go +++ b/command/apply_destroy_test.go @@ -40,7 +40,7 @@ func TestApply_destroy(t *testing.T) { // Run the apply command pointing to our existing state args := []string{ - "-force", + "-auto-approve", "-state", statePath, testFixturePath("apply"), } @@ -130,7 +130,7 @@ func TestApply_destroyLockedState(t *testing.T) { // Run the apply command pointing to our existing state args := []string{ - "-force", + "-auto-approve", "-state", statePath, testFixturePath("apply"), } @@ -206,7 +206,7 @@ func TestApply_destroyTargeted(t *testing.T) { // Run the apply command pointing to our existing state args := []string{ - "-force", + "-auto-approve", "-target", "test_instance.foo", "-state", statePath, testFixturePath("apply-destroy-targeted"), diff --git a/command/e2etest/primary_test.go b/command/e2etest/primary_test.go index 385801e7d..fb825e477 100644 --- a/command/e2etest/primary_test.go +++ b/command/e2etest/primary_test.go @@ -111,7 +111,7 @@ func TestPrimarySeparatePlan(t *testing.T) { } //// DESTROY - stdout, stderr, err = tf.Run("destroy", "-force") + stdout, stderr, err = tf.Run("destroy", "-auto-approve") if err != nil { t.Fatalf("unexpected destroy error: %s\nstderr:\n%s", err, stderr) } diff --git a/website/docs/commands/destroy.html.markdown b/website/docs/commands/destroy.html.markdown index 98f1b79e6..d317dd3e0 100644 --- a/website/docs/commands/destroy.html.markdown +++ b/website/docs/commands/destroy.html.markdown @@ -22,7 +22,7 @@ This command accepts all the arguments and flags that the [apply command](/docs/commands/apply.html) accepts, with the exception of a plan file argument. -If `-force` is set, then the destroy confirmation will not be shown. +If `-auto-approve` is set, then the destroy confirmation will not be shown. The `-target` flag, instead of affecting "dependencies" will instead also destroy any resources that _depend on_ the target(s) specified.