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.
This commit is contained in:
Martin Atkins 2017-10-05 12:00:45 -07:00
parent ea81e75a4e
commit 5cd00a13ec
5 changed files with 24 additions and 11 deletions

View File

@ -8,6 +8,7 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/go-getter" "github.com/hashicorp/go-getter"
"github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config"
@ -130,7 +131,8 @@ func (c *ApplyCommand) Run(args []string) int {
if plan == nil { if plan == nil {
mod, err = c.Module(configPath) mod, err = c.Module(configPath)
if err != nil { 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 return 1
} }
} }

View File

@ -155,8 +155,10 @@ func (c *InitCommand) Run(args []string) int {
if flagGet || flagBackend { if flagGet || flagBackend {
conf, err := c.Config(path) conf, err := c.Config(path)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf( // Since this may be the user's first ever interaction with Terraform,
"Error loading configuration: %s", err)) // we'll provide some additional context in this case.
c.Ui.Error(strings.TrimSpace(errInitConfigError))
c.showDiagnostics(err)
return 1 return 1
} }
@ -551,6 +553,13 @@ func (c *InitCommand) Synopsis() string {
return "Initialize a Terraform working directory" 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 = ` const errInitCopyNotEmpty = `
The working directory already contains files. The -from-module option requires 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. an empty directory into which a copy of the referenced module will be placed.

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/config/module"
@ -73,7 +74,8 @@ func (c *PlanCommand) Run(args []string) int {
if plan == nil { if plan == nil {
mod, err = c.Module(configPath) mod, err = c.Module(configPath)
if err != nil { 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 return 1
} }
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
@ -43,7 +44,8 @@ func (c *RefreshCommand) Run(args []string) int {
// Load the module // Load the module
mod, err := c.Module(configPath) mod, err := c.Module(configPath)
if err != nil { 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 return 1
} }

View File

@ -91,21 +91,19 @@ Options:
func (c *ValidateCommand) validate(dir string, checkVars bool) int { func (c *ValidateCommand) validate(dir string, checkVars bool) int {
cfg, err := config.LoadDir(dir) cfg, err := config.LoadDir(dir)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf( c.showDiagnostics(err)
"Error loading files %v\n", err.Error()))
return 1 return 1
} }
err = cfg.Validate() err = cfg.Validate()
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf( c.showDiagnostics(err)
"Error validating: %v\n", err.Error()))
return 1 return 1
} }
if checkVars { if checkVars {
mod, err := c.Module(dir) mod, err := c.Module(dir)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err)) c.showDiagnostics(err)
return 1 return 1
} }
@ -114,7 +112,7 @@ func (c *ValidateCommand) validate(dir string, checkVars bool) int {
tfCtx, err := terraform.NewContext(opts) tfCtx, err := terraform.NewContext(opts)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error: %v\n", err.Error())) c.showDiagnostics(err)
return 1 return 1
} }