command: validate context
This commit is contained in:
parent
5dbfed31b6
commit
43889a8c59
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue