command: refactor so Context never plans

This commit is contained in:
Mitchell Hashimoto 2014-07-26 17:45:38 -07:00
parent 615d724aaa
commit ef6fba754d
5 changed files with 20 additions and 18 deletions

View File

@ -58,7 +58,7 @@ func (c *ApplyCommand) Run(args []string) int {
} }
// Build the context based on the arguments given // 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 { if err != nil {
c.Ui.Error(err.Error()) c.Ui.Error(err.Error())
return 1 return 1
@ -67,6 +67,15 @@ func (c *ApplyCommand) Run(args []string) int {
return 1 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. // Start the apply in a goroutine so that we can be interrupted.
var state *terraform.State var state *terraform.State
var applyErr error var applyErr error

View File

@ -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 { if err != nil {
c.Ui.Error(fmt.Sprintf("Error loading Terraform: %s", err)) c.Ui.Error(fmt.Sprintf("Error loading Terraform: %s", err))
return 1 return 1

View File

@ -38,7 +38,7 @@ func (m *Meta) Colorize() *colorstring.Colorize {
// Context returns a Terraform Context taking into account the context // Context returns a Terraform Context taking into account the context
// options used to initialize this meta configuration. // 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() opts := m.contextOpts()
// First try to just read the plan directly from the path given. // 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() f.Close()
if err == nil { if err == nil {
if len(m.variables) > 0 { 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" + "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" + "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" + "the plan was created will be used. If you wish to use different\n" +
"variable values, create a new plan file.") "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 { 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) config, err := config.LoadDir(path)
if err != nil { 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 { 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.Config = config
opts.State = state opts.State = state
ctx := terraform.NewContext(opts) ctx := terraform.NewContext(opts)
return ctx, false, nil
if doPlan {
if _, err := ctx.Plan(nil); err != nil {
return nil, fmt.Errorf("Error running plan: %s", err)
}
}
return ctx, nil
} }

View File

@ -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 { if err != nil {
c.Ui.Error(err.Error()) c.Ui.Error(err.Error())
return 1 return 1

View File

@ -77,7 +77,7 @@ func (c *RefreshCommand) Run(args []string) int {
} }
// Build the context based on the arguments given // Build the context based on the arguments given
ctx, err := c.Context(configPath, statePath, false) ctx, _, err := c.Context(configPath, statePath)
if err != nil { if err != nil {
c.Ui.Error(err.Error()) c.Ui.Error(err.Error())
return 1 return 1