diff --git a/command/apply.go b/command/apply.go index 94f7d129c6..9b9b898716 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 b81109ec2c..28e4ffbbb7 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 f353651977..036e6fe35b 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 1949b54fe4..b6ae6b8543 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 d5ec580fe0..bd0af057bc 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 }