Skip to content

Commit

Permalink
fixup! plugin: gRPC-based new plugin systems
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Sep 25, 2021
1 parent aa5550a commit 72fe092
Show file tree
Hide file tree
Showing 11 changed files with 1,506 additions and 304 deletions.
11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/golang/protobuf v1.3.4 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
Expand All @@ -27,11 +27,10 @@ require (
github.com/oklog/run v1.0.0 // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
golang.org/x/net v0.0.0-20200301022130-244492dfa37a // indirect
golang.org/x/sys v0.0.0-20191008105621-543471e840be // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 // indirect
google.golang.org/grpc v1.27.1 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
)
6 changes: 3 additions & 3 deletions plugin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func (c *Client) ApplyConfig(config *tflint.MarshalledConfig) error {
return c.rpcClient.Call("Plugin.ApplyConfig", config, new(interface{}))
}

func (c *GRPCClient) ApplyConfig(config *schema.BodyContent) error {
_, err := c.client.ApplyConfig(context.Background(), toproto.ApplyConfig_Request(config))
func (c *GRPCClient) ApplyConfig(config *schema.BodyContent, sources map[string][]byte) error {
_, err := c.client.ApplyConfig(context.Background(), toproto.ApplyConfig_Request(config, sources))
return err
}

Expand All @@ -125,7 +125,7 @@ func (c *Client) Check(server tfserver.Server) error {
return c.rpcClient.Call("Plugin.Check", brokerID, new(interface{}))
}

func (c *GRPCClient) Check(r tflint.Runner) error {
func (c *GRPCClient) Check(r runner.Host) error {
brokerID := c.broker.NextId()
go c.broker.AcceptAndServe(brokerID, func(opts []grpc.ServerOption) *grpc.Server {
server := grpc.NewServer(opts...)
Expand Down
49 changes: 47 additions & 2 deletions plugin/fromproto/fromproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/hcl/v2/json"
"github.com/terraform-linters/tflint-plugin-sdk/plugin/proto"
"github.com/terraform-linters/tflint-plugin-sdk/schema"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
)

func BodySchema(body *proto.BodySchema) *schema.BodySchema {
Expand Down Expand Up @@ -43,7 +44,7 @@ func BodyContent(body *proto.BodyContent) (*schema.BodyContent, hcl.Diagnostics)

attributes := schema.Attributes{}
for key, attr := range body.Attributes {
expr, diags := parseExpression(attr.Expr, attr.Range.Filename, Pos(attr.Range.Start))
expr, diags := parseExpression(attr.Expr, attr.ExprRange.Filename, Pos(attr.ExprRange.Start))
// TODO: append
if diags.HasErrors() {
return nil, diags
Expand All @@ -54,7 +55,6 @@ func BodyContent(body *proto.BodyContent) (*schema.BodyContent, hcl.Diagnostics)
Expr: expr,
Range: Range(attr.Range),
NameRange: Range(attr.NameRange),
ExprBytes: attr.Expr,
}
}

Expand Down Expand Up @@ -88,6 +88,51 @@ func BodyContent(body *proto.BodyContent) (*schema.BodyContent, hcl.Diagnostics)
}, nil
}

type Rule struct {
Data RuleData
}

type RuleData struct {
Name string
Enabled bool
Severity string // TODO: enum?
Link string
}

func (r *Rule) Name() string { return r.Data.Name }
func (r *Rule) Enabled() bool { return r.Data.Enabled }
func (r *Rule) Severity() string { return r.Data.Severity }
func (r *Rule) Link() string { return r.Data.Link }
func (r *Rule) Check(tflint.Runner) error { return nil }

func EmitIssue_Rule(rule *proto.EmitIssue_Rule) *Rule {
if rule == nil {
return nil
}

return &Rule{
Data: RuleData{
Name: rule.Name,
Enabled: rule.Enabled,
Severity: EmitIssue_Severity(rule.Severity),
Link: rule.Link,
},
}
}

func EmitIssue_Severity(severity proto.EmitIssue_Severity) string {
switch severity {
case proto.EmitIssue_ERROR:
return tflint.ERROR
case proto.EmitIssue_WARNING:
return tflint.WARNING
case proto.EmitIssue_NOTICE:
return tflint.NOTICE
}

return tflint.ERROR
}

func Range(rng *proto.Range) hcl.Range {
if rng == nil {
return hcl.Range{}
Expand Down
Loading

0 comments on commit 72fe092

Please sign in to comment.