Skip to content

Commit 0db6af4

Browse files
committed
#30 add a helper WalkResource, extend the Runner interface also
1 parent 7c3de47 commit 0db6af4

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/vendor
33
/dist
44
tflint-ruleset-*
5+
.vscode

helper/runner.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,40 @@ func (r *Runner) WalkResourceAttributes(resourceType, attributeName string, walk
5858
return nil
5959
}
6060

61+
// WalkResources searches for resources with a specific type and passes to the walker function
62+
func (r *Runner) WalkResources(resourceType string, walker func(*tflint.Resource) error) error {
63+
for _, file := range r.Files {
64+
resources, _, diags := file.Body.PartialContent(&hcl.BodySchema{
65+
Blocks: []hcl.BlockHeaderSchema{
66+
{
67+
Type: "resource",
68+
LabelNames: []string{"type", "name"},
69+
},
70+
},
71+
})
72+
if diags.HasErrors() {
73+
return diags
74+
}
75+
76+
for _, resource := range resources.Blocks {
77+
if resource.Labels[0] != resourceType {
78+
continue
79+
}
80+
err := walker(&tflint.Resource{
81+
Name: resource.Labels[1],
82+
Type: resource.Labels[0],
83+
DeclRange: resource.DefRange,
84+
TypeRange: resource.LabelRanges[0],
85+
})
86+
if err != nil {
87+
return err
88+
}
89+
}
90+
}
91+
92+
return nil
93+
}
94+
6195
// EvaluateExpr returns a value of the passed expression.
6296
// Note that there is no evaluation, no type conversion, etc.
6397
func (r *Runner) EvaluateExpr(expr hcl.Expression, ret interface{}) error {

tflint/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
type Runner interface {
99
WalkResourceAttributes(string, string, func(*hcl.Attribute) error) error
1010
WalkResourceBlocks(string, string, func(*hcl.Block) error) error
11+
WalkResources(string, func(*Resource) error) error
1112
EvaluateExpr(expr hcl.Expression, ret interface{}) error
1213
EmitIssue(rule Rule, message string, location hcl.Range, meta Metadata) error
1314
EnsureNoError(error, func() error) error

0 commit comments

Comments
 (0)