Skip to content

Commit 69ee467

Browse files
committed
concurrency
1 parent 04afda1 commit 69ee467

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

pkg/tfsandbox/plan.go

+49-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"fmt"
2020
"path"
21+
"sync"
2122

2223
"github.com/hashicorp/terraform-exec/tfexec"
2324
tfjson "github.com/hashicorp/terraform-json"
@@ -66,17 +67,57 @@ func (t *Tofu) planWithOptions(ctx context.Context, logger Logger, refreshOnly b
6667
return nil, fmt.Errorf("error running plan: %w", err)
6768
}
6869

69-
// NOTE: the recommended default from terraform-json is to set JSONNumber=true
70-
// otherwise some number values will lose precision when converted to float64
71-
plan, err := t.tf.ShowPlanFile(ctx, planFile, tfexec.JSONNumber(true))
72-
if err != nil {
73-
return nil, fmt.Errorf("error running show plan: %w", err)
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)
78+
79+
go func() {
80+
defer wg.Done()
81+
// NOTE: the recommended default from terraform-json is to set JSONNumber=true
82+
// 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
89+
}()
90+
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+
}()
100+
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+
}
74115
}
75116

76-
humanPlan, err := t.tf.ShowPlanFileRaw(ctx, planFile, tfexec.JSONNumber(true))
77-
if err != nil {
78-
return nil, fmt.Errorf("error running show human plan: %w", err)
117+
if finalErr != nil {
118+
return nil, finalErr
79119
}
120+
80121
logger.Log(Debug, humanPlan, false)
81122

82123
return plan, nil

0 commit comments

Comments
 (0)