66 "os"
77 "strings"
88
9- "github.com/hashicorp/terraform/config"
109 "github.com/hashicorp/terraform/terraform"
1110 "github.com/mitchellh/cli"
1211)
@@ -54,26 +53,14 @@ func (c *ApplyCommand) Run(args []string) int {
5453
5554 // Attempt to read a plan from the path given. This is how we test that
5655 // it is a plan or not (kind of jank, but if it quacks like a duck...)
57- var plan * terraform.Plan
58- f , err := os .Open (configPath )
59- if err == nil {
60- plan , err = terraform .ReadPlan (f )
61- f .Close ()
62- if err != nil {
63- // Make sure the plan is nil so that we try to load as
64- // configuration.
65- plan = nil
66- }
56+ planStatePath := statePath
57+ if init {
58+ planStatePath = ""
6759 }
68-
69- if plan == nil {
70- // No plan was given, so we're loading from configuration. Generate
71- // the plan given the configuration.
72- plan , err = c .configToPlan (tf , init , statePath , configPath )
73- if err != nil {
74- c .Ui .Error (err .Error ())
75- return 1
76- }
60+ plan , err := PlanArg (configPath , planStatePath , tf )
61+ if err != nil {
62+ c .Ui .Error (err .Error ())
63+ return 1
7764 }
7865
7966 state , err := tf .Apply (plan )
@@ -83,7 +70,7 @@ func (c *ApplyCommand) Run(args []string) int {
8370 }
8471
8572 // Write state out to the file
86- f , err = os .Create (stateOutPath )
73+ f , err : = os .Create (stateOutPath )
8774 if err == nil {
8875 err = terraform .WriteState (state , f )
8976 f .Close ()
@@ -120,53 +107,3 @@ Options:
120107func (c * ApplyCommand ) Synopsis () string {
121108 return "Builds or changes infrastructure"
122109}
123-
124- func (c * ApplyCommand ) configToPlan (
125- tf * terraform.Terraform ,
126- init bool ,
127- statePath string ,
128- configPath string ) (* terraform.Plan , error ) {
129- if ! init {
130- if _ , err := os .Stat (statePath ); err != nil {
131- return nil , fmt .Errorf (
132- "There was an error reading the state file. The path\n " +
133- "and error are shown below. If you're trying to build a\n " +
134- "brand new infrastructure, explicitly pass the '-init'\n " +
135- "flag to Terraform to tell it it is okay to build new\n " +
136- "state.\n \n " +
137- "Path: %s\n " +
138- "Error: %s" ,
139- statePath ,
140- err )
141- }
142- }
143-
144- // Load up the state
145- var state * terraform.State
146- if ! init {
147- f , err := os .Open (statePath )
148- if err == nil {
149- state , err = terraform .ReadState (f )
150- f .Close ()
151- }
152-
153- if err != nil {
154- return nil , fmt .Errorf ("Error loading state: %s" , err )
155- }
156- }
157-
158- config , err := config .Load (configPath )
159- if err != nil {
160- return nil , fmt .Errorf ("Error loading config: %s" , err )
161- }
162-
163- plan , err := tf .Plan (& terraform.PlanOpts {
164- Config : config ,
165- State : state ,
166- })
167- if err != nil {
168- return nil , fmt .Errorf ("Error running plan: %s" , err )
169- }
170-
171- return plan , nil
172- }
0 commit comments