@@ -192,7 +192,7 @@ func (d DiggerExecutor) RetrievePlanJson() (string, error) {
192
192
}
193
193
194
194
showArgs := make ([]string , 0 )
195
- terraformPlanOutput , _ , _ := executor .TerraformExecutor .Show (showArgs , executor .CommandEnvVars , * storedPlanPath )
195
+ terraformPlanOutput , _ , _ := executor .TerraformExecutor .Show (showArgs , executor .CommandEnvVars , * storedPlanPath , true )
196
196
return terraformPlanOutput , nil
197
197
198
198
} else {
@@ -202,7 +202,7 @@ func (d DiggerExecutor) RetrievePlanJson() (string, error) {
202
202
203
203
func (d DiggerExecutor ) Plan () (* iac_utils.IacSummary , bool , bool , string , string , error ) {
204
204
plan := ""
205
- terraformPlanOutput := ""
205
+ terraformPlanOutputJsonString := ""
206
206
planSummary := & iac_utils.IacSummary {}
207
207
isEmptyPlan := true
208
208
var planSteps []scheduler.Step
@@ -219,6 +219,11 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin
219
219
},
220
220
}
221
221
}
222
+
223
+ hasPlanStep := lo .ContainsBy (planSteps , func (step scheduler.Step ) bool {
224
+ return step .Action == "plan"
225
+ })
226
+
222
227
for _ , step := range planSteps {
223
228
slog .Info ("Running step" , "action" , step .Action )
224
229
if step .Action == "init" {
@@ -234,46 +239,22 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin
234
239
// TODO remove those only for pulumi project
235
240
planArgs = append (planArgs , step .ExtraArgs ... )
236
241
237
- _ , stdout , stderr , err := d .TerraformExecutor .Plan (planArgs , d .CommandEnvVars , d .PlanPathProvider .LocalPlanFilePath (), d .PlanStage .FilterRegex )
238
- if err != nil {
239
- return nil , false , false , "" , "" , fmt .Errorf ("error executing plan: %v" , err )
240
- }
241
- showArgs := make ([]string , 0 )
242
- terraformPlanOutput , _ , _ = d .TerraformExecutor .Show (showArgs , d .CommandEnvVars , d .PlanPathProvider .LocalPlanFilePath ())
243
-
244
- isEmptyPlan , planSummary , err = d .IacUtils .GetSummaryFromPlanJson (terraformPlanOutput )
242
+ var err error
243
+ var stdout , stderr string
244
+ isEmptyPlan , stdout , stderr , err = d .TerraformExecutor .Plan (planArgs , d .CommandEnvVars , d .PlanPathProvider .LocalPlanFilePath (), d .PlanStage .FilterRegex )
245
245
if err != nil {
246
- return nil , false , false , "" , "" , fmt .Errorf ("error checking for empty plan: %v" , err )
247
- }
248
-
249
- if ! isEmptyPlan {
250
- nonEmptyPlanFilepath := strings .Replace (d .PlanPathProvider .LocalPlanFilePath (), d .PlanPathProvider .StoredPlanFilePath (), "isNonEmptyPlan.txt" , 1 )
251
- file , err := os .Create (nonEmptyPlanFilepath )
252
- if err != nil {
253
- return nil , false , false , "" , "" , fmt .Errorf ("unable to create file: %v" , err )
254
- }
255
- defer file .Close ()
256
- }
257
-
258
- if d .PlanStorage != nil {
259
-
260
- fileBytes , err := os .ReadFile (d .PlanPathProvider .LocalPlanFilePath ())
261
- if err != nil {
262
- fmt .Println ("Error reading file:" , err )
263
- return nil , false , false , "" , "" , fmt .Errorf ("error reading file bytes: %v" , err )
264
- }
265
-
266
- err = d .PlanStorage .StorePlanFile (fileBytes , d .PlanPathProvider .ArtifactName (), d .PlanPathProvider .StoredPlanFilePath ())
267
- if err != nil {
268
- fmt .Println ("Error storing artifact file:" , err )
269
- return nil , false , false , "" , "" , fmt .Errorf ("error storing artifact file: %v" , err )
270
- }
246
+ return nil , false , false , "" , "" , fmt .Errorf ("error executing plan: %v, stdout: %v, stderr: %v" , err , stdout , stderr )
271
247
}
272
248
273
- // TODO: move this function to iacUtils interface and implement for pulumi
274
- plan = cleanupTerraformPlan (! isEmptyPlan , err , stdout , stderr )
249
+ plan , terraformPlanOutputJsonString , planSummary , isEmptyPlan , err = d .postProcessPlan (stdout )
275
250
if err != nil {
276
- slog .Error ("error publishing comment" , "error" , err )
251
+ slog .Debug ("error post processing plan" ,
252
+ "error" , err ,
253
+ "plan" , plan ,
254
+ "planSummary" , planSummary ,
255
+ "isEmptyPlan" , isEmptyPlan ,
256
+ )
257
+ return nil , false , false , "" , "" , fmt .Errorf ("error post processing plan: %v" , err ) //nolint:wrapcheck // err
277
258
}
278
259
}
279
260
if step .Action == "run" {
@@ -297,8 +278,67 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin
297
278
}
298
279
}
299
280
}
281
+
282
+ if ! hasPlanStep {
283
+ rawPlan , _ , err := d .TerraformExecutor .Show (make ([]string , 0 ), d .CommandEnvVars , d .PlanPathProvider .LocalPlanFilePath (), false )
284
+ if err != nil {
285
+ return nil , false , false , "" , "" , fmt .Errorf ("error running terraform show: %v" , err )
286
+ }
287
+ plan , terraformPlanOutputJsonString , planSummary , isEmptyPlan , err = d .postProcessPlan (rawPlan )
288
+ if err != nil {
289
+ slog .Debug ("error post processing plan" ,
290
+ "error" , err ,
291
+ "plan" , plan ,
292
+ "planSummary" , planSummary ,
293
+ "isEmptyPlan" , isEmptyPlan ,
294
+ )
295
+ return nil , false , false , "" , "" , fmt .Errorf ("error post processing plan: %v" , err ) //nolint:wrapcheck // err
296
+ }
297
+ }
298
+
300
299
reportAdditionalOutput (d .Reporter , d .projectId ())
301
- return planSummary , true , ! isEmptyPlan , plan , terraformPlanOutput , nil
300
+ return planSummary , true , ! isEmptyPlan , plan , terraformPlanOutputJsonString , nil
301
+ }
302
+
303
+ func (d DiggerExecutor ) postProcessPlan (stdout string ) (string , string , * iac_utils.IacSummary , bool , error ) {
304
+ showArgs := make ([]string , 0 )
305
+ terraformPlanJsonOutputString , _ , err := d .TerraformExecutor .Show (showArgs , d .CommandEnvVars , d .PlanPathProvider .LocalPlanFilePath (), true )
306
+ if err != nil {
307
+ return "" , "" , nil , false , fmt .Errorf ("error running terraform show: %v" , err )
308
+ }
309
+
310
+ isEmptyPlan , planSummary , err := d .IacUtils .GetSummaryFromPlanJson (terraformPlanJsonOutputString )
311
+ if err != nil {
312
+ return "" , "" , nil , false , fmt .Errorf ("error checking for empty plan: %v" , err )
313
+ }
314
+
315
+ if ! isEmptyPlan {
316
+ nonEmptyPlanFilepath := strings .Replace (d .PlanPathProvider .LocalPlanFilePath (), d .PlanPathProvider .StoredPlanFilePath (), "isNonEmptyPlan.txt" , 1 )
317
+ file , err := os .Create (nonEmptyPlanFilepath )
318
+ if err != nil {
319
+ return "" , "" , nil , false , fmt .Errorf ("unable to create file: %v" , err )
320
+ }
321
+ defer file .Close ()
322
+ }
323
+
324
+ if d .PlanStorage != nil {
325
+ fileBytes , err := os .ReadFile (d .PlanPathProvider .LocalPlanFilePath ())
326
+ if err != nil {
327
+ fmt .Println ("Error reading file:" , err )
328
+ return "" , "" , nil , false , fmt .Errorf ("error reading file bytes: %v" , err )
329
+ }
330
+
331
+ err = d .PlanStorage .StorePlanFile (fileBytes , d .PlanPathProvider .ArtifactName (), d .PlanPathProvider .StoredPlanFilePath ())
332
+ if err != nil {
333
+ fmt .Println ("Error storing artifact file:" , err )
334
+ return "" , "" , nil , false , fmt .Errorf ("error storing artifact file: %v" , err )
335
+
336
+ }
337
+ }
338
+
339
+ // TODO: move this function to iacUtils interface and implement for pulumi
340
+ cleanedUpPlan := cleanupTerraformPlan (stdout )
341
+ return cleanedUpPlan , terraformPlanJsonOutputString , planSummary , isEmptyPlan , nil
302
342
}
303
343
304
344
func reportError (r reporting.Reporter , stderr string ) {
@@ -483,25 +523,14 @@ func (d DiggerExecutor) Destroy() (bool, error) {
483
523
return true , nil
484
524
}
485
525
486
- func cleanupTerraformOutput (nonEmptyOutput bool , planError error , stdout string , stderr string , regexStr * string ) string {
487
- var errorStr string
488
-
526
+ func cleanupTerraformOutput (stdout string , regexStr * string ) string {
489
527
// removes output of terraform -version command that terraform-exec executes on every run
490
528
i := strings .Index (stdout , "Initializing the backend..." )
491
529
if i != - 1 {
492
530
stdout = stdout [i :]
493
531
}
494
532
endPos := len (stdout )
495
533
496
- if planError != nil {
497
- if stderr != "" {
498
- errorStr = stderr
499
- } else if stdout != "" {
500
- errorStr = stdout
501
- }
502
- return errorStr
503
- }
504
-
505
534
delimiters := []string {
506
535
"Terraform will perform the following actions:" ,
507
536
"OpenTofu will perform the following actions:" ,
@@ -535,12 +564,12 @@ func cleanupTerraformOutput(nonEmptyOutput bool, planError error, stdout string,
535
564
}
536
565
537
566
func cleanupTerraformApply (nonEmptyPlan bool , planError error , stdout string , stderr string ) string {
538
- return cleanupTerraformOutput (nonEmptyPlan , planError , stdout , stderr , nil )
567
+ return cleanupTerraformOutput (stdout , nil )
539
568
}
540
569
541
- func cleanupTerraformPlan (nonEmptyPlan bool , planError error , stdout string , stderr string ) string {
570
+ func cleanupTerraformPlan (stdout string ) string {
542
571
regex := `───────────.+`
543
- return cleanupTerraformOutput (nonEmptyPlan , planError , stdout , stderr , & regex )
572
+ return cleanupTerraformOutput (stdout , & regex )
544
573
}
545
574
546
575
func (d DiggerExecutor ) projectId () string {
0 commit comments