Skip to content

Commit 1733df8

Browse files
authored
Add GetOriginalwd method (#224)
1 parent 506b8ca commit 1733df8

File tree

9 files changed

+913
-621
lines changed

9 files changed

+913
-621
lines changed

helper/runner.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ type RuleConfig struct {
4444

4545
var _ tflint.Runner = &Runner{}
4646

47+
// GetOriginalwd always returns the current directory
48+
func (r *Runner) GetOriginalwd() (string, error) {
49+
return os.Getwd()
50+
}
51+
4752
// GetModulePath always returns the root module path address
4853
func (r *Runner) GetModulePath() (addrs.Module, error) {
4954
return []string{}, nil

plugin/host2plugin/host2plugin_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,10 @@ type mockServerImpl struct {
540540
getFile func(string) (*hcl.File, error)
541541
}
542542

543+
func (s *mockServer) GetOriginalwd() string {
544+
return "/work"
545+
}
546+
543547
func (s *mockServer) GetModulePath() []string {
544548
return []string{}
545549
}

plugin/plugin2host/client.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"os"
78
"strings"
89

910
"github.com/hashicorp/hcl/v2"
@@ -19,6 +20,8 @@ import (
1920
"github.com/zclconf/go-cty/cty/gocty"
2021
"github.com/zclconf/go-cty/cty/json"
2122
"github.com/zclconf/go-cty/cty/msgpack"
23+
"google.golang.org/grpc/codes"
24+
"google.golang.org/grpc/status"
2225
)
2326

2427
// GRPCClient is a plugin-side implementation. Plugin can send requests through the client to host's gRPC server.
@@ -28,6 +31,20 @@ type GRPCClient struct {
2831

2932
var _ tflint.Runner = &GRPCClient{}
3033

34+
// GetOriginalwd gets the original working directory.
35+
func (c *GRPCClient) GetOriginalwd() (string, error) {
36+
resp, err := c.Client.GetOriginalwd(context.Background(), &proto.GetOriginalwd_Request{})
37+
if err != nil {
38+
if st, ok := status.FromError(err); ok && st.Code() == codes.Unimplemented {
39+
// Originalwd is available in TFLint v0.44+
40+
// Fallback to os.Getwd() because it equals the current directory in earlier versions.
41+
return os.Getwd()
42+
}
43+
return "", fromproto.Error(err)
44+
}
45+
return resp.Path, err
46+
}
47+
3148
// GetModulePath gets the current module path address.
3249
func (c *GRPCClient) GetModulePath() (addrs.Module, error) {
3350
resp, err := c.Client.GetModulePath(context.Background(), &proto.GetModulePath_Request{})

plugin/plugin2host/plugin2host_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type mockServer struct {
3535
}
3636

3737
type mockServerImpl struct {
38+
getOriginalwd func() string
3839
getModulePath func() []string
3940
getModuleContent func(*hclext.BodySchema, tflint.GetModuleContentOption) (*hclext.BodyContent, hcl.Diagnostics)
4041
getFile func(string) (*hcl.File, error)
@@ -48,6 +49,13 @@ func newMockServer(impl mockServerImpl) *mockServer {
4849
return &mockServer{impl: impl}
4950
}
5051

52+
func (s *mockServer) GetOriginalwd() string {
53+
if s.impl.getOriginalwd != nil {
54+
return s.impl.getOriginalwd()
55+
}
56+
return ""
57+
}
58+
5159
func (s *mockServer) GetModulePath() []string {
5260
if s.impl.getModulePath != nil {
5361
return s.impl.getModulePath()
@@ -100,6 +108,36 @@ func (s *mockServer) EmitIssue(rule tflint.Rule, message string, location hcl.Ra
100108
// @see https://github.com/google/go-cmp/issues/40
101109
var allowAllUnexported = cmp.Exporter(func(reflect.Type) bool { return true })
102110

111+
func TestGetOriginalwd(t *testing.T) {
112+
tests := []struct {
113+
Name string
114+
ServerImpl func() string
115+
Want string
116+
}{
117+
{
118+
Name: "get the original working directory",
119+
ServerImpl: func() string {
120+
return "/work"
121+
},
122+
Want: "/work",
123+
},
124+
}
125+
126+
for _, test := range tests {
127+
t.Run(test.Name, func(t *testing.T) {
128+
client := startTestGRPCServer(t, newMockServer(mockServerImpl{getOriginalwd: test.ServerImpl}))
129+
130+
got, err := client.GetOriginalwd()
131+
if err != nil {
132+
t.Fatalf("failed to call GetOriginalwd: %s", err)
133+
}
134+
if diff := cmp.Diff(got, test.Want); diff != "" {
135+
t.Errorf("diff: %s", diff)
136+
}
137+
})
138+
}
139+
}
140+
103141
func TestGetModulePath(t *testing.T) {
104142
tests := []struct {
105143
Name string

plugin/plugin2host/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var _ proto.RunnerServer = &GRPCServer{}
2828

2929
// Server is the interface that the host should implement when a plugin communicates with the host.
3030
type Server interface {
31+
GetOriginalwd() string
3132
GetModulePath() []string
3233
GetModuleContent(*hclext.BodySchema, tflint.GetModuleContentOption) (*hclext.BodyContent, hcl.Diagnostics)
3334
GetFile(string) (*hcl.File, error)
@@ -38,6 +39,11 @@ type Server interface {
3839
EmitIssue(rule tflint.Rule, message string, location hcl.Range) error
3940
}
4041

42+
// GetOriginalwd gets the original working directory.
43+
func (s *GRPCServer) GetOriginalwd(ctx context.Context, req *proto.GetOriginalwd_Request) (*proto.GetOriginalwd_Response, error) {
44+
return &proto.GetOriginalwd_Response{Path: s.Impl.GetOriginalwd()}, nil
45+
}
46+
4147
// GetModulePath gets the current module path address.
4248
func (s *GRPCServer) GetModulePath(ctx context.Context, req *proto.GetModulePath_Request) (*proto.GetModulePath_Response, error) {
4349
return &proto.GetModulePath_Response{Path: s.Impl.GetModulePath()}, nil

0 commit comments

Comments
 (0)