@@ -192,7 +192,7 @@ func (d DiggerExecutor) RetrievePlanJson() (string, error) {
192192 }
193193
194194 showArgs := make ([]string , 0 )
195- terraformPlanOutput , _ , _ := executor .TerraformExecutor .Show (showArgs , executor .CommandEnvVars , * storedPlanPath )
195+ terraformPlanOutput , _ , _ := executor .TerraformExecutor .Show (showArgs , executor .CommandEnvVars , * storedPlanPath , true )
196196 return terraformPlanOutput , nil
197197
198198 } else {
@@ -202,7 +202,7 @@ func (d DiggerExecutor) RetrievePlanJson() (string, error) {
202202
203203func (d DiggerExecutor ) Plan () (* iac_utils.IacSummary , bool , bool , string , string , error ) {
204204 plan := ""
205- terraformPlanOutput := ""
205+ terraformPlanOutputJsonString := ""
206206 planSummary := & iac_utils.IacSummary {}
207207 isEmptyPlan := true
208208 var planSteps []scheduler.Step
@@ -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" {
@@ -234,46 +239,22 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin
234239 // TODO remove those only for pulumi project
235240 planArgs = append (planArgs , step .ExtraArgs ... )
236241
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 )
245245 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 )
271247 }
272248
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 )
275250 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
277258 }
278259 }
279260 if step .Action == "run" {
@@ -297,8 +278,67 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin
297278 }
298279 }
299280 }
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+
300299 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
302342}
303343
304344func reportError (r reporting.Reporter , stderr string ) {
@@ -483,25 +523,14 @@ func (d DiggerExecutor) Destroy() (bool, error) {
483523 return true , nil
484524}
485525
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 {
489527 // removes output of terraform -version command that terraform-exec executes on every run
490528 i := strings .Index (stdout , "Initializing the backend..." )
491529 if i != - 1 {
492530 stdout = stdout [i :]
493531 }
494532 endPos := len (stdout )
495533
496- if planError != nil {
497- if stderr != "" {
498- errorStr = stderr
499- } else if stdout != "" {
500- errorStr = stdout
501- }
502- return errorStr
503- }
504-
505534 delimiters := []string {
506535 "Terraform will perform the following actions:" ,
507536 "OpenTofu will perform the following actions:" ,
@@ -535,12 +564,12 @@ func cleanupTerraformOutput(nonEmptyOutput bool, planError error, stdout string,
535564}
536565
537566func cleanupTerraformApply (nonEmptyPlan bool , planError error , stdout string , stderr string ) string {
538- return cleanupTerraformOutput (nonEmptyPlan , planError , stdout , stderr , nil )
567+ return cleanupTerraformOutput (stdout , nil )
539568}
540569
541- func cleanupTerraformPlan (nonEmptyPlan bool , planError error , stdout string , stderr string ) string {
570+ func cleanupTerraformPlan (stdout string ) string {
542571 regex := `───────────.+`
543- return cleanupTerraformOutput (nonEmptyPlan , planError , stdout , stderr , & regex )
572+ return cleanupTerraformOutput (stdout , & regex )
544573}
545574
546575func (d DiggerExecutor ) projectId () string {
0 commit comments