diff --git a/command/apply.go b/command/apply.go index da5499970..3f072e436 100644 --- a/command/apply.go +++ b/command/apply.go @@ -56,6 +56,9 @@ func (c *ApplyCommand) Run(args []string) int { c.Ui.Error(err.Error()) return 1 } + if !validateContext(ctx, c.Ui) { + return 1 + } errCh := make(chan error) stateCh := make(chan *terraform.State) diff --git a/command/command.go b/command/command.go index f87ed0787..fd316f4b1 100644 --- a/command/command.go +++ b/command/command.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/terraform" + "github.com/mitchellh/cli" ) func ContextArg( @@ -69,3 +70,33 @@ func ContextArg( return ctx, nil } + +func validateContext(ctx *terraform.Context, ui cli.Ui) bool { + if ws, es := ctx.Validate(); len(ws) > 0 || len(es) > 0 { + ui.Output( + "There are warnings and/or errors related to your configuration. Please\n" + + "fix these before continuing.\n") + + if len(ws) > 0 { + ui.Output("Warnings:\n") + for _, w := range ws { + ui.Output(fmt.Sprintf(" * %s", w)) + } + + if len(es) > 0 { + ui.Output("") + } + } + + if len(es) > 0 { + ui.Output("Errors:\n") + for _, e := range es { + ui.Output(fmt.Sprintf(" * %s", e)) + } + } + + return false + } + + return true +} diff --git a/command/plan.go b/command/plan.go index 7f729a62d..06cb9e7bb 100644 --- a/command/plan.go +++ b/command/plan.go @@ -69,6 +69,9 @@ func (c *PlanCommand) Run(args []string) int { c.ContextOpts.Hooks = append(c.ContextOpts.Hooks, &UiHook{Ui: c.Ui}) c.ContextOpts.State = state ctx := terraform.NewContext(c.ContextOpts) + if !validateContext(ctx, c.Ui) { + return 1 + } if refresh { if _, err := ctx.Refresh(); err != nil { diff --git a/command/refresh.go b/command/refresh.go index 7bec37034..f1447b781 100644 --- a/command/refresh.go +++ b/command/refresh.go @@ -69,6 +69,9 @@ func (c *RefreshCommand) Run(args []string) int { c.ContextOpts.Config = b c.ContextOpts.Hooks = append(c.ContextOpts.Hooks, &UiHook{Ui: c.Ui}) ctx := terraform.NewContext(c.ContextOpts) + if !validateContext(ctx, c.Ui) { + return 1 + } state, err = ctx.Refresh() if err != nil {