@@ -16,9 +16,9 @@ package tfsandbox
16
16
17
17
import (
18
18
"context"
19
+ "errors"
19
20
"fmt"
20
21
"path"
21
- "sync"
22
22
23
23
"github.com/hashicorp/terraform-exec/tfexec"
24
24
tfjson "github.com/hashicorp/terraform-json"
@@ -67,58 +67,31 @@ func (t *Tofu) planWithOptions(ctx context.Context, logger Logger, refreshOnly b
67
67
return nil , fmt .Errorf ("error running plan: %w" , err )
68
68
}
69
69
70
- var wg sync.WaitGroup
71
- wg .Add (2 )
72
- planChan := make (chan * tfjson.Plan , 1 )
73
- humanPlanChan := make (chan string , 1 )
74
- errChan := make (chan error , 2 )
75
- defer close (planChan )
76
- defer close (errChan )
77
- defer close (humanPlanChan )
70
+ var (
71
+ plan * tfjson.Plan
72
+ planErr error
73
+ planCh = make (chan bool )
74
+ )
78
75
76
+ // fork
79
77
go func () {
80
- defer wg . Done ( )
78
+ defer close ( planCh )
81
79
// NOTE: the recommended default from terraform-json is to set JSONNumber=true
82
80
// otherwise some number values will lose precision when converted to float64
83
- plan , err := t .tf .ShowPlanFile (ctx , planFile , tfexec .JSONNumber (true ))
84
- if err != nil {
85
- errChan <- fmt .Errorf ("error running show plan: %w" , err )
86
- return
87
- }
88
- planChan <- plan
81
+ plan , planErr = t .tf .ShowPlanFile (ctx , planFile , tfexec .JSONNumber (true ))
89
82
}()
90
83
91
- go func () {
92
- defer wg .Done ()
93
- humanPlan , err := t .tf .ShowPlanFileRaw (ctx , planFile , tfexec .JSONNumber (true ))
94
- if err != nil {
95
- errChan <- fmt .Errorf ("error running show human plan: %w" , err )
96
- return
97
- }
98
- humanPlanChan <- humanPlan
99
- }()
84
+ humanPlan , humanPlanErr := t .tf .ShowPlanFileRaw (ctx , planFile , tfexec .JSONNumber (true ))
100
85
101
- wg .Wait ()
102
-
103
- var plan * tfjson.Plan
104
- var humanPlan string
105
- var finalErr error
106
- for range 2 {
107
- select {
108
- case p := <- planChan :
109
- plan = p
110
- case hp := <- humanPlanChan :
111
- humanPlan = hp
112
- case err := <- errChan :
113
- finalErr = err
114
- }
115
- }
86
+ // join
87
+ <- planCh
116
88
117
- if finalErr != nil {
118
- return nil , finalErr
89
+ err = errors .Join (planErr , humanPlanErr )
90
+ if err != nil {
91
+ return nil , err
119
92
}
120
93
121
- logger .Log (Debug , humanPlan , false )
94
+ logger .Log (ctx , Debug , humanPlan )
122
95
123
96
return plan , nil
124
97
}
0 commit comments