Skip to content

Commit 14af879

Browse files
glasserapparentlymart
authored andcommitted
command: also print plan for destroy
1 parent 039d36b commit 14af879

3 files changed

Lines changed: 49 additions & 50 deletions

File tree

‎backend/backend.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,11 @@ type Operation struct {
121121

122122
// The options below are more self-explanatory and affect the runtime
123123
// behavior of the operation.
124-
Destroy bool
125-
Targets []string
126-
Variables map[string]interface{}
127-
AutoApprove bool
124+
Destroy bool
125+
Targets []string
126+
Variables map[string]interface{}
127+
AutoApprove bool
128+
DestroyForce bool
128129

129130
// Input/output/control options.
130131
UIIn terraform.UIInput

‎backend/local/backend_apply.go‎

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,58 @@ func (b *Local) opApply(
9898

9999
trivialPlan := plan.Diff == nil || plan.Diff.Empty()
100100
hasUI := op.UIOut != nil && op.UIIn != nil
101-
if hasUI && !op.AutoApprove && !trivialPlan {
102-
op.UIOut.Output(strings.TrimSpace(approvePlanHeader) + "\n")
103-
op.UIOut.Output(format.Plan(&format.PlanOpts{
104-
Plan: plan,
105-
Color: b.Colorize(),
106-
ModuleDepth: -1,
107-
}))
108-
desc := "Terraform will apply the plan described above.\n" +
109-
"Only 'yes' will be accepted to approve."
101+
if hasUI && ((op.Destroy && !op.DestroyForce) ||
102+
(!op.Destroy && !op.AutoApprove && !trivialPlan)) {
103+
var desc, query string
104+
if op.Destroy {
105+
// Default destroy message
106+
desc = "Terraform will delete all your managed infrastructure.\n" +
107+
"There is no undo. Only 'yes' will be accepted to confirm."
108+
109+
// If targets are specified, list those to user
110+
if op.Targets != nil {
111+
var descBuffer bytes.Buffer
112+
descBuffer.WriteString("Terraform will delete the following infrastructure:\n")
113+
for _, target := range op.Targets {
114+
descBuffer.WriteString("\t")
115+
descBuffer.WriteString(target)
116+
descBuffer.WriteString("\n")
117+
}
118+
descBuffer.WriteString("There is no undo. Only 'yes' will be accepted to confirm")
119+
desc = descBuffer.String()
120+
}
121+
query = "Do you really want to destroy?"
122+
} else {
123+
desc = "Terraform will apply the plan described above.\n" +
124+
"Only 'yes' will be accepted to approve."
125+
query = "Do you want to apply the plan above?"
126+
}
127+
128+
if !trivialPlan {
129+
// Display the plan of what we are going to apply/destroy.
130+
op.UIOut.Output(strings.TrimSpace(approvePlanHeader) + "\n")
131+
op.UIOut.Output(format.Plan(&format.PlanOpts{
132+
Plan: plan,
133+
Color: b.Colorize(),
134+
ModuleDepth: -1,
135+
}))
136+
}
137+
110138
v, err := op.UIIn.Input(&terraform.InputOpts{
111139
Id: "approve",
112-
Query: "Do you want to apply the plan above?",
140+
Query: query,
113141
Description: desc,
114142
})
115143
if err != nil {
116144
runningOp.Err = errwrap.Wrapf("Error asking for approval: {{err}}", err)
117145
return
118146
}
119147
if v != "yes" {
120-
runningOp.Err = errors.New("Apply cancelled.")
148+
if op.Destroy {
149+
runningOp.Err = errors.New("Destroy cancelled.")
150+
} else {
151+
runningOp.Err = errors.New("Apply cancelled.")
152+
}
121153
return
122154
}
123155
}

‎command/apply.go‎

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -161,41 +161,6 @@ func (c *ApplyCommand) Run(args []string) int {
161161
return 1
162162
}
163163

164-
// If we're not forcing and we're destroying, verify with the
165-
// user at this point.
166-
if !destroyForce && c.Destroy {
167-
// Default destroy message
168-
desc := "Terraform will delete all your managed infrastructure.\n" +
169-
"There is no undo. Only 'yes' will be accepted to confirm."
170-
171-
// If targets are specified, list those to user
172-
if c.Meta.targets != nil {
173-
var descBuffer bytes.Buffer
174-
descBuffer.WriteString("Terraform will delete the following infrastructure:\n")
175-
for _, target := range c.Meta.targets {
176-
descBuffer.WriteString("\t")
177-
descBuffer.WriteString(target)
178-
descBuffer.WriteString("\n")
179-
}
180-
descBuffer.WriteString("There is no undo. Only 'yes' will be accepted to confirm")
181-
desc = descBuffer.String()
182-
}
183-
184-
v, err := c.UIInput().Input(&terraform.InputOpts{
185-
Id: "destroy",
186-
Query: "Do you really want to destroy?",
187-
Description: desc,
188-
})
189-
if err != nil {
190-
c.Ui.Error(fmt.Sprintf("Error asking for confirmation: %s", err))
191-
return 1
192-
}
193-
if v != "yes" {
194-
c.Ui.Output("Destroy cancelled.")
195-
return 1
196-
}
197-
}
198-
199164
// Build the operation
200165
opReq := c.Operation()
201166
opReq.Destroy = c.Destroy
@@ -204,6 +169,7 @@ func (c *ApplyCommand) Run(args []string) int {
204169
opReq.PlanRefresh = refresh
205170
opReq.Type = backend.OperationTypeApply
206171
opReq.AutoApprove = autoApprove
172+
opReq.DestroyForce = destroyForce
207173

208174
// Perform the operation
209175
ctx, ctxCancel := context.WithCancel(context.Background())

0 commit comments

Comments
 (0)