-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameter.go
58 lines (49 loc) · 1.4 KB
/
parameter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package preview
import (
"fmt"
"strings"
"github.com/aquasecurity/trivy/pkg/iac/terraform"
"github.com/hashicorp/hcl/v2"
"github.com/coder/preview/extract"
"github.com/coder/preview/types"
)
func RichParameters(modules terraform.Modules) ([]types.Parameter, hcl.Diagnostics) {
diags := make(hcl.Diagnostics, 0)
params := make([]types.Parameter, 0)
exists := make(map[string][]types.Parameter)
for _, mod := range modules {
blocks := mod.GetDatasByType(types.BlockTypeParameter)
for _, block := range blocks {
param, pDiags := extract.ParameterFromBlock(block)
if len(pDiags) > 0 {
diags = diags.Extend(pDiags)
}
if param != nil {
params = append(params, *param)
if _, ok := exists[param.Name]; !ok {
exists[param.Name] = make([]types.Parameter, 0)
}
exists[param.Name] = append(exists[param.Name], *param)
}
}
}
for k, v := range exists {
var detail strings.Builder
for _, p := range v {
if p.Source != nil {
detail.WriteString(fmt.Sprintf("block %q at %s\n",
p.Source.Type()+"."+strings.Join(p.Source.Labels(), "."),
p.Source.HCLBlock().TypeRange))
}
}
if len(v) > 1 {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Found %d duplicate parameters with name %q, this is not allowed", len(v), k),
Detail: detail.String(),
})
}
}
types.SortParameters(params)
return params, diags
}