forked from terraform-linters/tflint-ruleset-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.go
60 lines (51 loc) · 1.41 KB
/
runner.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
package aws
import (
"log"
"github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
)
// Runner is a wrapper of RPC client for inserting custom actions for AWS provider.
type Runner struct {
tflint.Runner
PluginConfig *Config
AwsClient *Client
}
// NewRunner returns a custom AWS runner.
func NewRunner(runner tflint.Runner, config *Config) (*Runner, error) {
var client *Client
if config.DeepCheck {
credentials, err := GetCredentialsFromProvider(runner)
if err != nil {
return nil, err
}
client, err = NewClient(config.toCredentials().Merge(credentials))
if err != nil {
return nil, err
}
}
return &Runner{
Runner: runner,
PluginConfig: config,
AwsClient: client,
}, nil
}
// EachStringSliceExprs iterates an evaluated value and the corresponding expression
// If the given expression is a static list, get an expression for each value
// If not, the given expression is used as it is
func (r *Runner) EachStringSliceExprs(expr hcl.Expression, proc func(val string, expr hcl.Expression)) error {
var vals []string
err := r.EvaluateExpr(expr, &vals, nil)
exprs, diags := hcl.ExprList(expr)
if diags.HasErrors() {
log.Printf("[DEBUG] Expr is not static list: %s", diags)
for range vals {
exprs = append(exprs, expr)
}
}
return r.EnsureNoError(err, func() error {
for idx, val := range vals {
proc(val, exprs[idx])
}
return nil
})
}