Skip to content

Commit

Permalink
Use errors.Is() instead of EnsureNoError
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Mar 19, 2023
1 parent 60146bd commit a56f91b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
26 changes: 16 additions & 10 deletions plugin/stub-generator/sources/customrulesettesting/custom/runner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package custom

import (
"errors"

"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
)
Expand Down Expand Up @@ -51,25 +53,29 @@ func NewRunner(runner tflint.Runner, config *Config) (*Runner, error) {
if attr, exists := provider.Body.Attributes["zone"]; exists {
var zone string
err := runner.EvaluateExpr(attr.Expr, &zone, opts)
err = runner.EnsureNoError(err, func() error {
config.Zone = zone
return nil
})
if err != nil {
return nil, err
if errors.Is(err, tflint.ErrUnknownValue) || errors.Is(err, tflint.ErrNullValue) || errors.Is(err, tflint.ErrSensitive) {
// skip
} else {
return nil, err
}
} else {
config.Zone = zone
}
}

for _, annotation := range provider.Body.Blocks {
if attr, exists := annotation.Body.Attributes["value"]; exists {
var val string
err := runner.EvaluateExpr(attr.Expr, &val, opts)
err = runner.EnsureNoError(err, func() error {
config.Annotation = val
return nil
})
if err != nil {
return nil, err
if errors.Is(err, tflint.ErrUnknownValue) || errors.Is(err, tflint.ErrNullValue) || errors.Is(err, tflint.ErrSensitive) {
// skip
} else {
return nil, err
}
} else {
config.Annotation = val
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions plugin/stub-generator/sources/example/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"

"github.com/terraform-linters/tflint-plugin-sdk/hclext"
Expand Down Expand Up @@ -67,14 +68,18 @@ func (r *AwsInstanceExampleTypeRule) Check(runner tflint.Runner) error {

var instanceType string
err := runner.EvaluateExpr(attribute.Expr, &instanceType, nil)
if err != nil {
if errors.Is(err, tflint.ErrUnknownValue) || errors.Is(err, tflint.ErrNullValue) || errors.Is(err, tflint.ErrSensitive) {
continue
}
return err
}

err = runner.EnsureNoError(err, func() error {
return runner.EmitIssue(
r,
fmt.Sprintf("instance type is %s", instanceType),
attribute.Expr.Range(),
)
})
err = runner.EmitIssue(
r,
fmt.Sprintf("instance type is %s", instanceType),
attribute.Expr.Range(),
)
if err != nil {
return err
}
Expand Down

0 comments on commit a56f91b

Please sign in to comment.