2020-12-09 15:31:34 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2021-01-15 11:44:13 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/parallel"
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ParallelResourceReader struct {
|
2021-01-15 11:44:13 +00:00
|
|
|
runner *parallel.ParallelRunner
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 11:44:13 +00:00
|
|
|
func NewParallelResourceReader(runner *parallel.ParallelRunner) *ParallelResourceReader {
|
2020-12-09 15:31:34 +00:00
|
|
|
return &ParallelResourceReader{
|
|
|
|
runner: runner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ParallelResourceReader) Wait() ([]cty.Value, error) {
|
|
|
|
results := make([]cty.Value, 0)
|
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case res, ok := <-p.runner.Read():
|
|
|
|
if !ok {
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
ctyVal := res.(cty.Value)
|
|
|
|
if !ctyVal.IsNull() {
|
|
|
|
results = append(results, ctyVal)
|
|
|
|
}
|
|
|
|
case <-p.runner.DoneChan():
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results, p.runner.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ParallelResourceReader) Run(runnable func() (cty.Value, error)) {
|
|
|
|
p.runner.Run(func() (interface{}, error) {
|
|
|
|
return runnable()
|
|
|
|
})
|
|
|
|
}
|