terraform/command/command.go

72 lines
1.5 KiB
Go
Raw Normal View History

2014-07-01 19:02:13 +02:00
package command
import (
"fmt"
"os"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform"
)
2014-07-03 20:46:40 +02:00
func ContextArg(
2014-07-01 19:02:13 +02:00
path string,
statePath string,
2014-07-03 20:46:40 +02:00
opts *terraform.ContextOpts) (*terraform.Context, error) {
2014-07-01 19:02:13 +02:00
// First try to just read the plan directly from the path given.
f, err := os.Open(path)
if err == nil {
plan, err := terraform.ReadPlan(f)
f.Close()
if err == nil {
2014-07-03 20:46:40 +02:00
return plan.Context(opts), nil
2014-07-01 19:02:13 +02:00
}
}
if statePath != "" {
if _, err := os.Stat(statePath); err != nil {
return nil, fmt.Errorf(
"There was an error reading the state file. The path\n"+
"and error are shown below. If you're trying to build a\n"+
"brand new infrastructure, explicitly pass the '-init'\n"+
"flag to Terraform to tell it it is okay to build new\n"+
"state.\n\n"+
"Path: %s\n"+
"Error: %s",
statePath,
err)
}
}
// Load up the state
var state *terraform.State
if statePath != "" {
f, err := os.Open(statePath)
if err == nil {
state, err = terraform.ReadState(f)
f.Close()
}
if err != nil {
return nil, fmt.Errorf("Error loading state: %s", err)
}
}
config, err := config.Load(path)
if err != nil {
return nil, fmt.Errorf("Error loading config: %s", err)
}
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("Error validating config: %s", err)
}
2014-07-01 19:02:13 +02:00
2014-07-03 20:46:40 +02:00
opts.Config = config
opts.State = state
ctx := terraform.NewContext(opts)
if _, err := ctx.Plan(nil); err != nil {
2014-07-01 19:02:13 +02:00
return nil, fmt.Errorf("Error running plan: %s", err)
}
2014-07-03 20:46:40 +02:00
return ctx, nil
2014-07-01 19:02:13 +02:00
}