command: validate context

This commit is contained in:
Mitchell Hashimoto 2014-07-03 13:12:45 -07:00
parent 5dbfed31b6
commit 43889a8c59
4 changed files with 40 additions and 0 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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 {

View File

@ -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 {