From 5cd00a13ec3b026d9f86c6595f6d42c112a9f1d4 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 5 Oct 2017 12:00:45 -0700 Subject: [PATCH] command: use new diagnostics output for config errors This uses the new diagnostics printer for config-related errors in the main five commands that deal with config. The immediate motivation for this is to allow HCL2-produced diagnostics to be printed out in their full fidelity, though it also slightly changes the presentation of other errors so that they are not presented in all red text, which can be hard to read on some terminals. --- command/apply.go | 4 +++- command/init.go | 13 +++++++++++-- command/plan.go | 4 +++- command/refresh.go | 4 +++- command/validate.go | 10 ++++------ 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/command/apply.go b/command/apply.go index 94f7d129c..9b9b89871 100644 --- a/command/apply.go +++ b/command/apply.go @@ -8,6 +8,7 @@ import ( "sort" "strings" + "github.com/hashicorp/errwrap" "github.com/hashicorp/go-getter" "github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/config" @@ -130,7 +131,8 @@ func (c *ApplyCommand) Run(args []string) int { if plan == nil { mod, err = c.Module(configPath) if err != nil { - c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err)) + err = errwrap.Wrapf("Failed to load root config module: {{err}}", err) + c.showDiagnostics(err) return 1 } } diff --git a/command/init.go b/command/init.go index b81109ec2..28e4ffbbb 100644 --- a/command/init.go +++ b/command/init.go @@ -155,8 +155,10 @@ func (c *InitCommand) Run(args []string) int { if flagGet || flagBackend { conf, err := c.Config(path) if err != nil { - c.Ui.Error(fmt.Sprintf( - "Error loading configuration: %s", err)) + // Since this may be the user's first ever interaction with Terraform, + // we'll provide some additional context in this case. + c.Ui.Error(strings.TrimSpace(errInitConfigError)) + c.showDiagnostics(err) return 1 } @@ -551,6 +553,13 @@ func (c *InitCommand) Synopsis() string { return "Initialize a Terraform working directory" } +const errInitConfigError = ` +There are some problems with the configuration, described below. + +The Terraform configuration must be valid before initialization so that +Terraform can determine which modules and providers need to be installed. +` + const errInitCopyNotEmpty = ` The working directory already contains files. The -from-module option requires an empty directory into which a copy of the referenced module will be placed. diff --git a/command/plan.go b/command/plan.go index f35365197..036e6fe35 100644 --- a/command/plan.go +++ b/command/plan.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + "github.com/hashicorp/errwrap" "github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config/module" @@ -73,7 +74,8 @@ func (c *PlanCommand) Run(args []string) int { if plan == nil { mod, err = c.Module(configPath) if err != nil { - c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err)) + err = errwrap.Wrapf("Failed to load root config module: {{err}}", err) + c.showDiagnostics(err) return 1 } } diff --git a/command/refresh.go b/command/refresh.go index 1949b54fe..b6ae6b854 100644 --- a/command/refresh.go +++ b/command/refresh.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + "github.com/hashicorp/errwrap" "github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/terraform" @@ -43,7 +44,8 @@ func (c *RefreshCommand) Run(args []string) int { // Load the module mod, err := c.Module(configPath) if err != nil { - c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err)) + err = errwrap.Wrapf("Failed to load root config module: {{err}}", err) + c.showDiagnostics(err) return 1 } diff --git a/command/validate.go b/command/validate.go index d5ec580fe..bd0af057b 100644 --- a/command/validate.go +++ b/command/validate.go @@ -91,21 +91,19 @@ Options: func (c *ValidateCommand) validate(dir string, checkVars bool) int { cfg, err := config.LoadDir(dir) if err != nil { - c.Ui.Error(fmt.Sprintf( - "Error loading files %v\n", err.Error())) + c.showDiagnostics(err) return 1 } err = cfg.Validate() if err != nil { - c.Ui.Error(fmt.Sprintf( - "Error validating: %v\n", err.Error())) + c.showDiagnostics(err) return 1 } if checkVars { mod, err := c.Module(dir) if err != nil { - c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err)) + c.showDiagnostics(err) return 1 } @@ -114,7 +112,7 @@ func (c *ValidateCommand) validate(dir string, checkVars bool) int { tfCtx, err := terraform.NewContext(opts) if err != nil { - c.Ui.Error(fmt.Sprintf("Error: %v\n", err.Error())) + c.showDiagnostics(err) return 1 }