Skip to content

Commit e4c90db

Browse files
committed
refactor to use postprocess step
1 parent b90269b commit e4c90db

File tree

2 files changed

+49
-37
lines changed

2 files changed

+49
-37
lines changed

libs/execution/execution.go

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin
219219
},
220220
}
221221
}
222+
223+
hasPlanStep := lo.ContainsBy(planSteps, func(step scheduler.Step) bool {
224+
return step.Action == "plan"
225+
})
226+
222227
for _, step := range planSteps {
223228
slog.Info("Running step", "action", step.Action)
224229
if step.Action == "init" {
@@ -239,6 +244,16 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin
239244
return nil, false, false, "", "", fmt.Errorf("error executing plan: %v", err)
240245
}
241246

247+
plan, planSummary, isEmptyPlan, err = d.postProcessPlan(plan)
248+
if err != nil {
249+
slog.Debug("error post processing plan",
250+
"error", err,
251+
"plan", plan,
252+
"planSummary", planSummary,
253+
"isEmptyPlan", isEmptyPlan,
254+
)
255+
return nil, false, false, "", "", fmt.Errorf("error post processing plan: %v", err) //nolint:wrapcheck // err
256+
}
242257
}
243258
if step.Action == "run" {
244259
var commands []string
@@ -262,55 +277,63 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin
262277
}
263278
}
264279

265-
///////
266-
showArgs := make([]string, 0)
267-
terraformPlanOutputJsonString, _, err := d.TerraformExecutor.Show(showArgs, d.CommandEnvVars, d.PlanPathProvider.LocalPlanFilePath(), true)
268-
if err != nil {
269-
return nil, false, false, "", "", fmt.Errorf("error showing plan: %v", err)
280+
if !hasPlanStep {
281+
var err error
282+
plan, planSummary, isEmptyPlan, err = d.postProcessPlan(plan)
283+
if err != nil {
284+
slog.Debug("error post processing plan",
285+
"error", err,
286+
"plan", plan,
287+
"planSummary", planSummary,
288+
"isEmptyPlan", isEmptyPlan,
289+
)
290+
return nil, false, false, "", "", fmt.Errorf("error post processing plan: %v", err) //nolint:wrapcheck // err
291+
}
270292
}
271293

272-
isEmptyPlan, planSummary, err = d.IacUtils.GetSummaryFromPlanJson(terraformPlanOutputJsonString)
294+
reportAdditionalOutput(d.Reporter, d.projectId())
295+
return planSummary, true, !isEmptyPlan, plan, terraformPlanOutputJsonString, nil
296+
}
297+
298+
func (d DiggerExecutor) postProcessPlan(stdout string) (string, *iac_utils.IacSummary, bool, error) {
299+
showArgs := make([]string, 0)
300+
terraformPlanOutput, _, _ := d.TerraformExecutor.Show(showArgs, d.CommandEnvVars, d.PlanPathProvider.LocalPlanFilePath(), true)
301+
302+
isEmptyPlan, planSummary, err := d.IacUtils.GetSummaryFromPlanJson(terraformPlanOutput)
273303
if err != nil {
274-
return nil, false, false, "", "", fmt.Errorf("error checking for empty plan: %v", err)
304+
return "", nil, false, fmt.Errorf("error checking for empty plan: %v", err)
275305
}
276306

277307
if !isEmptyPlan {
278308
nonEmptyPlanFilepath := strings.Replace(d.PlanPathProvider.LocalPlanFilePath(), d.PlanPathProvider.StoredPlanFilePath(), "isNonEmptyPlan.txt", 1)
279309
file, err := os.Create(nonEmptyPlanFilepath)
280310
if err != nil {
281-
return nil, false, false, "", "", fmt.Errorf("unable to create file: %v", err)
311+
return "", nil, false, fmt.Errorf("unable to create file: %v", err)
282312
}
283313
defer file.Close()
284314
}
285315

286316
if d.PlanStorage != nil {
317+
287318
fileBytes, err := os.ReadFile(d.PlanPathProvider.LocalPlanFilePath())
288319
if err != nil {
289320
fmt.Println("Error reading file:", err)
290-
return nil, false, false, "", "", fmt.Errorf("error reading file bytes: %v", err)
321+
return "", nil, false, fmt.Errorf("error reading file bytes: %v", err)
291322
}
292323

293324
err = d.PlanStorage.StorePlanFile(fileBytes, d.PlanPathProvider.ArtifactName(), d.PlanPathProvider.StoredPlanFilePath())
294325
if err != nil {
295326
fmt.Println("Error storing artifact file:", err)
296-
return nil, false, false, "", "", fmt.Errorf("error storing artifact file: %v", err)
327+
return "", nil, false, fmt.Errorf("error storing artifact file: %v", err)
297328
}
298329
}
299330

300-
terraformPlanOutput, _, err := d.TerraformExecutor.Show(showArgs, d.CommandEnvVars, d.PlanPathProvider.LocalPlanFilePath(), false)
301-
if err != nil {
302-
return nil, false, false, "", "", fmt.Errorf("error showing plan: %v", err)
303-
}
304-
305331
// TODO: move this function to iacUtils interface and implement for pulumi
306-
plan = cleanupTerraformPlan(!isEmptyPlan, nil, terraformPlanOutput, "")
332+
cleanedUpPlan := cleanupTerraformPlan(stdout)
307333
if err != nil {
308334
slog.Error("error publishing comment", "error", err)
309335
}
310-
//////
311-
312-
reportAdditionalOutput(d.Reporter, d.projectId())
313-
return planSummary, true, !isEmptyPlan, plan, terraformPlanOutputJsonString, nil
336+
return cleanedUpPlan, planSummary, isEmptyPlan, nil
314337
}
315338

316339
func reportError(r reporting.Reporter, stderr string) {
@@ -495,25 +518,14 @@ func (d DiggerExecutor) Destroy() (bool, error) {
495518
return true, nil
496519
}
497520

498-
func cleanupTerraformOutput(nonEmptyOutput bool, planError error, stdout string, stderr string, regexStr *string) string {
499-
var errorStr string
500-
521+
func cleanupTerraformOutput(stdout string, regexStr *string) string {
501522
// removes output of terraform -version command that terraform-exec executes on every run
502523
i := strings.Index(stdout, "Initializing the backend...")
503524
if i != -1 {
504525
stdout = stdout[i:]
505526
}
506527
endPos := len(stdout)
507528

508-
if planError != nil {
509-
if stderr != "" {
510-
errorStr = stderr
511-
} else if stdout != "" {
512-
errorStr = stdout
513-
}
514-
return errorStr
515-
}
516-
517529
delimiters := []string{
518530
"Terraform will perform the following actions:",
519531
"OpenTofu will perform the following actions:",
@@ -547,12 +559,12 @@ func cleanupTerraformOutput(nonEmptyOutput bool, planError error, stdout string,
547559
}
548560

549561
func cleanupTerraformApply(nonEmptyPlan bool, planError error, stdout string, stderr string) string {
550-
return cleanupTerraformOutput(nonEmptyPlan, planError, stdout, stderr, nil)
562+
return cleanupTerraformOutput(stdout, nil)
551563
}
552564

553-
func cleanupTerraformPlan(nonEmptyPlan bool, planError error, stdout string, stderr string) string {
565+
func cleanupTerraformPlan(stdout string) string {
554566
regex := `───────────.+`
555-
return cleanupTerraformOutput(nonEmptyPlan, planError, stdout, stderr, &regex)
567+
return cleanupTerraformOutput(stdout, &regex)
556568
}
557569

558570
func (d DiggerExecutor) projectId() string {

libs/execution/execution_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Terraform will perform the following actions:
6565
6666
Plan: 2 to add, 0 to change, 0 to destroy.
6767
`
68-
res := cleanupTerraformPlan(true, nil, stdout, "")
68+
res := cleanupTerraformPlan(stdout)
6969
index := strings.Index(stdout, "Terraform will perform the following actions:")
7070
assert.Equal(t, stdout[index:], res)
7171
}
@@ -256,7 +256,7 @@ Plan: 9 to add, 0 to change, 0 to destroy.
256256
Changes to Outputs:
257257
+ api_url = (known after apply)
258258
`
259-
res := cleanupTerraformPlan(true, nil, stdout, "")
259+
res := cleanupTerraformPlan(stdout)
260260
index := strings.Index(stdout, "OpenTofu will perform the following actions:")
261261
assert.Equal(t, stdout[index:], res)
262262
}

0 commit comments

Comments
 (0)