command: refactor so Context never plans
This commit is contained in:
parent
615d724aaa
commit
ef6fba754d
|
@ -58,7 +58,7 @@ func (c *ApplyCommand) Run(args []string) int {
|
|||
}
|
||||
|
||||
// Build the context based on the arguments given
|
||||
ctx, err := c.Context(configPath, statePath, true)
|
||||
ctx, planned, err := c.Context(configPath, statePath)
|
||||
if err != nil {
|
||||
c.Ui.Error(err.Error())
|
||||
return 1
|
||||
|
@ -67,6 +67,15 @@ func (c *ApplyCommand) Run(args []string) int {
|
|||
return 1
|
||||
}
|
||||
|
||||
// Plan if we haven't already
|
||||
if !planned {
|
||||
if _, err := ctx.Plan(nil); err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error creating plan: %s", err))
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
// Start the apply in a goroutine so that we can be interrupted.
|
||||
var state *terraform.State
|
||||
var applyErr error
|
||||
|
|
|
@ -40,7 +40,7 @@ func (c *GraphCommand) Run(args []string) int {
|
|||
}
|
||||
}
|
||||
|
||||
ctx, err := c.Context(path, "", false)
|
||||
ctx, _, err := c.Context(path, "")
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error loading Terraform: %s", err))
|
||||
return 1
|
||||
|
|
|
@ -38,7 +38,7 @@ func (m *Meta) Colorize() *colorstring.Colorize {
|
|||
|
||||
// Context returns a Terraform Context taking into account the context
|
||||
// options used to initialize this meta configuration.
|
||||
func (m *Meta) Context(path, statePath string, doPlan bool) (*terraform.Context, error) {
|
||||
func (m *Meta) Context(path, statePath string) (*terraform.Context, bool, error) {
|
||||
opts := m.contextOpts()
|
||||
|
||||
// First try to just read the plan directly from the path given.
|
||||
|
@ -48,14 +48,14 @@ func (m *Meta) Context(path, statePath string, doPlan bool) (*terraform.Context,
|
|||
f.Close()
|
||||
if err == nil {
|
||||
if len(m.variables) > 0 {
|
||||
return nil, fmt.Errorf(
|
||||
return nil, false, fmt.Errorf(
|
||||
"You can't set variables with the '-var' or '-var-file' flag\n" +
|
||||
"when you're applying a plan file. The variables used when\n" +
|
||||
"the plan was created will be used. If you wish to use different\n" +
|
||||
"variable values, create a new plan file.")
|
||||
}
|
||||
|
||||
return plan.Context(opts), nil
|
||||
return plan.Context(opts), true, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,29 +73,22 @@ func (m *Meta) Context(path, statePath string, doPlan bool) (*terraform.Context,
|
|||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error loading state: %s", err)
|
||||
return nil, false, fmt.Errorf("Error loading state: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
config, err := config.LoadDir(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error loading config: %s", err)
|
||||
return nil, false, fmt.Errorf("Error loading config: %s", err)
|
||||
}
|
||||
if err := config.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("Error validating config: %s", err)
|
||||
return nil, false, fmt.Errorf("Error validating config: %s", err)
|
||||
}
|
||||
|
||||
opts.Config = config
|
||||
opts.State = state
|
||||
ctx := terraform.NewContext(opts)
|
||||
|
||||
if doPlan {
|
||||
if _, err := ctx.Plan(nil); err != nil {
|
||||
return nil, fmt.Errorf("Error running plan: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, nil
|
||||
return ctx, false, nil
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -58,7 +58,7 @@ func (c *PlanCommand) Run(args []string) int {
|
|||
}
|
||||
}
|
||||
|
||||
ctx, err := c.Context(path, statePath, false)
|
||||
ctx, _, err := c.Context(path, statePath)
|
||||
if err != nil {
|
||||
c.Ui.Error(err.Error())
|
||||
return 1
|
||||
|
|
|
@ -77,7 +77,7 @@ func (c *RefreshCommand) Run(args []string) int {
|
|||
}
|
||||
|
||||
// Build the context based on the arguments given
|
||||
ctx, err := c.Context(configPath, statePath, false)
|
||||
ctx, _, err := c.Context(configPath, statePath)
|
||||
if err != nil {
|
||||
c.Ui.Error(err.Error())
|
||||
return 1
|
||||
|
|
Loading…
Reference in New Issue