Skip to content

Commit 752aa5e

Browse files
author
Matt Cadorette
authored
fix: provide guidance after failed terraform execution (#1494)
1 parent 4dd94cc commit 752aa5e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

cli/cmd/generate_execute.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,22 @@ func provideGuidanceAfterSuccess(workingDir string, laceworkProfile string) stri
358358
return out.String()
359359
}
360360

361+
func provideGuidanceAfterFailure(err error, workingDir string, binaryLocation string) string {
362+
out := new(strings.Builder)
363+
fmt.Fprintf(out, "\n\n%s\n\n", err.Error())
364+
fmt.Fprintln(out, strings.Repeat("-", 80))
365+
fmt.Fprint(out, "Terraform encountered an error (see above)\n\n")
366+
fmt.Fprintf(out, "The Terraform code, state, and plan output have been saved in %s.\n\n", workingDir)
367+
fmt.Fprintln(out, "Once the issues have been resolved, the integration can be continued using the following commands:")
368+
fmt.Fprintf(out, " cd %s\n", workingDir)
369+
fmt.Fprintf(out, " %s apply\n\n", binaryLocation)
370+
fmt.Fprintln(out, "Should you simply want to clean up the failed deployment, use the following commands:")
371+
fmt.Fprintf(out, " cd %s\n", workingDir)
372+
fmt.Fprintf(out, " %s destroy\n\n", binaryLocation)
373+
374+
return out.String()
375+
}
376+
361377
// this helper function is called when the entire generation/apply flow is not completed; it provides
362378
// guidance on how to proceed from the last point of execution
363379
func provideGuidanceAfterExit(initRun bool, planRun bool, workingDir string, binaryLocation string) string {
@@ -423,7 +439,7 @@ func TerraformPlanAndExecute(workingDir string) error {
423439

424440
// Apply plan
425441
if err := TerraformExecApply(tf); err != nil {
426-
return err
442+
return errors.New(provideGuidanceAfterFailure(err, tf.WorkingDir(), tf.ExecPath()))
427443
}
428444
vw.Close()
429445

0 commit comments

Comments
 (0)