-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspacetags.go
166 lines (146 loc) · 4.45 KB
/
workspacetags.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package preview
import (
"fmt"
"github.com/aquasecurity/trivy/pkg/iac/terraform"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
"github.com/coder/preview/types"
)
func WorkspaceTags(modules terraform.Modules, files map[string]*hcl.File) (types.TagBlocks, hcl.Diagnostics) {
diags := make(hcl.Diagnostics, 0)
tagBlocks := make(types.TagBlocks, 0)
for _, mod := range modules {
blocks := mod.GetDatasByType("coder_workspace_tags")
for _, block := range blocks {
evCtx := block.Context().Inner()
tagsAttr := block.GetAttribute("tags")
if tagsAttr.IsNil() {
r := block.HCLBlock().Body.MissingItemRange()
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Missing required argument",
Detail: `"tags" attribute is required by coder_workspace_tags blocks`,
Subject: &r,
EvalContext: evCtx,
})
continue
}
tagsValue := tagsAttr.Value()
if !tagsValue.Type().IsObjectType() {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Incorrect type for \"tags\" attribute",
// TODO: better error message for types
Detail: fmt.Sprintf(`"tags" attribute must be an 'Object', but got %q`, tagsValue.Type().FriendlyName()),
Subject: &tagsAttr.HCLAttribute().NameRange,
Context: &tagsAttr.HCLAttribute().Range,
Expression: tagsAttr.HCLAttribute().Expr,
EvalContext: block.Context().Inner(),
})
continue
}
//tagsObj, ok := tagsAttr.HCLAttribute().Expr.(*hclsyntax.ObjectConsExpr)
//if !ok {
// diags = diags.Append(&hcl.Diagnostic{
// Severity: hcl.DiagError,
// Summary: "Incorrect type for \"tags\" attribute",
// // TODO: better error message for types
// Detail: fmt.Sprintf(`"tags" attribute must be an 'ObjectConsExpr', but got %T`, tagsAttr.HCLAttribute().Expr),
// Subject: &tagsAttr.HCLAttribute().NameRange,
// Context: &tagsAttr.HCLAttribute().Range,
// Expression: tagsAttr.HCLAttribute().Expr,
// EvalContext: block.Context().Inner(),
// })
// continue
//}
var tags []types.Tag
tagsValue.ForEachElement(func(key cty.Value, val cty.Value) (stop bool) {
r := tagsAttr.HCLAttribute().Expr.Range()
tag, tagDiag := NewTag(&r, files, key, val)
if tagDiag != nil {
diags = diags.Append(tagDiag)
return false
}
tags = append(tags, tag)
return false
})
//for _, item := range tagsObj.Items {
// tag, tagDiag := NewTag(tagsObj, files, item, evCtx)
// if tagDiag != nil {
// diags = diags.Append(tagDiag)
// continue
// }
//
// tags = append(tags, tag)
//}
tagBlocks = append(tagBlocks, types.TagBlock{
Tags: tags,
Block: block,
})
}
}
return tagBlocks, diags
}
// NewTag creates a workspace tag from its hcl expression.
func NewTag(srcRange *hcl.Range, files map[string]*hcl.File, key, val cty.Value) (types.Tag, *hcl.Diagnostic) {
//key, kdiags := expr.KeyExpr.Value(evCtx)
//val, vdiags := expr.ValueExpr.Value(evCtx)
// TODO: ???
//if kdiags.HasErrors() {
// key = cty.UnknownVal(cty.String)
//}
//if vdiags.HasErrors() {
// val = cty.UnknownVal(cty.String)
//}
if key.IsKnown() && key.Type() != cty.String {
return types.Tag{}, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid key type for tags",
Detail: fmt.Sprintf("Key must be a string, but got %s", key.Type().FriendlyName()),
//Subject: &r,
Context: srcRange,
//Expression: expr.KeyExpr,
//EvalContext: evCtx,
}
}
if val.IsKnown() && val.Type() != cty.String {
fr := "<nil>"
if !val.Type().Equals(cty.NilType) {
fr = val.Type().FriendlyName()
}
//r := expr.ValueExpr.Range()
return types.Tag{}, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid value type for tag",
Detail: fmt.Sprintf("Value must be a string, but got %s", fr),
//Subject: &r,
Context: srcRange,
//Expression: expr.ValueExpr,
//EvalContext: evCtx,
}
}
tag := types.Tag{
Key: types.HCLString{
Value: key,
//ValueDiags: kdiags,
//ValueExpr: expr.KeyExpr,
},
Value: types.HCLString{
Value: val,
//ValueDiags: vdiags,
//ValueExpr: expr.ValueExpr,
},
}
//ks, err := source(expr.KeyExpr.Range(), files)
//if err == nil {
// src := string(ks)
// tag.Key.Source = &src
//}
//
//vs, err := source(expr.ValueExpr.Range(), files)
//if err == nil {
// src := string(vs)
// tag.Value.Source = &src
//}
return tag, nil
}