Skip to content

Commit 2442e34

Browse files
authored
Merge pull request #65 from AubreySLavigne/viper-flags
Implement Viper to manage flags
2 parents 3626372 + 1efb5ef commit 2442e34

File tree

11 files changed

+190
-118
lines changed

11 files changed

+190
-118
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ require (
88
github.com/davecgh/go-spew v1.1.1
99
github.com/go-errors/errors v1.0.1 // indirect
1010
github.com/go-language-server/uri v0.2.0
11-
github.com/gogo/protobuf v1.2.1 // indirect
1211
github.com/gruntwork-io/terragrunt v0.21.13
1312
github.com/hashicorp/go-hclog v0.9.0 // indirect
1413
github.com/hashicorp/go-plugin v1.0.1-0.20190610192547-a1bc61569a26
@@ -25,6 +24,8 @@ require (
2524
github.com/sirupsen/logrus v1.2.0
2625
github.com/sourcegraph/go-lsp v0.0.0-20181119182933-0c7d621186c1
2726
github.com/spf13/afero v1.2.2
27+
github.com/spf13/pflag v1.0.3
28+
github.com/spf13/viper v1.6.2
2829
github.com/ulikunitz/xz v0.5.6 // indirect
2930
github.com/urfave/cli v1.22.2 // indirect
3031
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect

go.sum

Lines changed: 49 additions & 0 deletions
Large diffs are not rendered by default.

langserver/did_change.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ func TextDocumentDidChange(ctx context.Context, vs lsp.DidChangeTextDocumentPara
1919

2020
DiagsFiles[fileURL] = tfstructs.GetDiagnostics(tempFile.Name(), fileURL)
2121

22-
TextDocumentPublishDiagnostics(ctx, lsp.PublishDiagnosticsParams{
23-
URI: vs.TextDocument.URI,
24-
Diagnostics: DiagsFiles[fileURL],
25-
})
22+
TextDocumentPublishDiagnostics(ctx, lsp.PublishDiagnosticsParams{
23+
URI: vs.TextDocument.URI,
24+
Diagnostics: DiagsFiles[fileURL],
25+
})
2626
return nil
2727
}

langserver/did_open.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ func TextDocumentDidOpen(ctx context.Context, vs lsp.DidOpenTextDocumentParams)
1515

1616
DiagsFiles[fileURL] = tfstructs.GetDiagnostics(fileURL, fileURL)
1717

18-
TextDocumentPublishDiagnostics(ctx, lsp.PublishDiagnosticsParams{
19-
URI: vs.TextDocument.URI,
20-
Diagnostics: DiagsFiles[fileURL],
21-
})
18+
TextDocumentPublishDiagnostics(ctx, lsp.PublishDiagnosticsParams{
19+
URI: vs.TextDocument.URI,
20+
Diagnostics: DiagsFiles[fileURL],
21+
})
2222
tempFile.Write([]byte(vs.TextDocument.Text))
2323
return nil
2424
}

langserver/initialized.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
)
66

7-
type InitializedParams struct {}
7+
type InitializedParams struct{}
88

99
func Initialized(ctx context.Context, vs InitializedParams) error {
10-
return nil
10+
return nil
1111
}

langserver/main.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ package langserver
22

33
import (
44
"context"
5-
"fmt"
5+
"fmt"
66
"github.com/creachadair/jrpc2"
77
"github.com/creachadair/jrpc2/channel"
88
"github.com/creachadair/jrpc2/handler"
99
"github.com/creachadair/jrpc2/server"
1010
log "github.com/sirupsen/logrus"
11-
oldLog "log"
11+
oldLog "log"
1212
"net"
1313
"os"
1414
)
1515

1616
func RunStdioServer(oldLogInstance *oldLog.Logger) {
17-
isTCP = false
17+
isTCP = false
1818

1919
StdioServer = jrpc2.NewServer(ServiceMap, &jrpc2.ServerOptions{
2020
AllowPush: true,
21-
Logger: oldLogInstance,
21+
Logger: oldLogInstance,
2222
})
2323

2424
StdioServer.Start(channel.Header("")(os.Stdin, os.Stdout))
@@ -34,7 +34,7 @@ func RunStdioServer(oldLogInstance *oldLog.Logger) {
3434
}
3535

3636
func RunTCPServer(address string, port int, oldLogInstance *oldLog.Logger) {
37-
isTCP = true
37+
isTCP = true
3838

3939
lst, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port))
4040
if err != nil {
@@ -51,9 +51,9 @@ func RunTCPServer(address string, port int, oldLogInstance *oldLog.Logger) {
5151
if err := server.Loop(lst, ServiceMap, &server.LoopOptions{
5252
Framing: newChan,
5353
ServerOptions: &jrpc2.ServerOptions{
54-
AllowPush: true,
55-
Logger: oldLogInstance,
56-
},
54+
AllowPush: true,
55+
Logger: oldLogInstance,
56+
},
5757
}); err != nil {
5858
log.Errorf("Loop: unexpected failure: %v", err)
5959
cancelFunc()
@@ -76,7 +76,7 @@ func RunTCPServer(address string, port int, oldLogInstance *oldLog.Logger) {
7676
func InitializeServiceMap() {
7777
ServiceMap = handler.Map{
7878
"initialize": handler.New(Initialize),
79-
"initialized": handler.New(Initialized),
79+
"initialized": handler.New(Initialized),
8080
"textDocument/completion": handler.New(TextDocumentComplete),
8181
"textDocument/didChange": handler.New(TextDocumentDidChange),
8282
"textDocument/didOpen": handler.New(TextDocumentDidOpen),

langserver/publish_diagnostics.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88

99
func TextDocumentPublishDiagnostics(ctx context.Context, vs lsp.PublishDiagnosticsParams) error {
1010

11-
var resultedError error
11+
var resultedError error
1212

13-
if isTCP {
14-
resultedError = jrpc2.ServerPush(ctx, "textDocument/publishDiagnostics", vs)
15-
} else {
16-
resultedError = StdioServer.Push(ctx, "textDocument/publishDiagnostics", vs)
17-
}
13+
if isTCP {
14+
resultedError = jrpc2.ServerPush(ctx, "textDocument/publishDiagnostics", vs)
15+
} else {
16+
resultedError = StdioServer.Push(ctx, "textDocument/publishDiagnostics", vs)
17+
}
1818

1919
return resultedError
2020
}

main.go

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,78 +10,102 @@ import (
1010
"strings"
1111

1212
"github.com/juliosueiras/terraform-lsp/langserver"
13+
"github.com/spf13/pflag"
14+
"github.com/spf13/viper"
1315
)
1416

15-
var tcp = flag.Bool("tcp", false, "Use TCP instead of Stdio(which is default)")
16-
var port = flag.Int("port", 9900, "Port for TCP Server")
17-
var address = flag.String("address", "127.0.0.1", "Address for TCP Server")
17+
var Version string
18+
var GitCommit string
19+
var Date string
1820

19-
var location = flag.String("log-location", "", "Location of the lsp log")
20-
var locationJRPC2 = flag.String("log-jrpc2-location", "", "Location of the lsp log for jrpc2")
21+
func init() {
22+
flag.Bool("tcp", false, "Use TCP instead of Stdio(which is default)")
23+
flag.Int("port", 9900, "Port for TCP Server")
24+
flag.String("address", "127.0.0.1", "Address for TCP Server")
2125

22-
var debug = flag.Bool("debug", false, "Enable debug output")
23-
var debugJRPC2 = flag.Bool("debug-jrpc2", false, "Enable debug output for jrpc2")
26+
flag.String("log-location", "", "Location of the lsp log")
27+
flag.String("log-jrpc2-location", "", "Location of the lsp log for jrpc2")
2428

25-
var enableLogFile = flag.Bool("enable-log-file", false, "Enable log file")
26-
var enableLogFileJRPC2 = flag.Bool("enable-log-jrpc2-file", false, "Enable log file for JRPC2")
29+
flag.Bool("debug", false, "Enable debug output")
30+
flag.Bool("debug-jrpc2", false, "Enable debug output for jrpc2")
2731

28-
var Version string
29-
var GitCommit string
30-
var Date string
32+
flag.Bool("enable-log-file", false, "Enable log file")
33+
flag.Bool("enable-log-jrpc2-file", false, "Enable log file for JRPC2")
34+
35+
flag.Bool("version", false, "Show version")
3136

32-
var version = flag.Bool("version", false, "Show version")
37+
// Load config from file
38+
configViper()
39+
}
3340

3441
func main() {
35-
flag.Parse()
3642

3743
oldLog.SetOutput(ioutil.Discard)
3844
oldLog.SetFlags(0)
3945

40-
if *version {
41-
fmt.Printf("v%s, commit: %s, build on: %s", strings.Trim(Version, "v"), GitCommit, Date)
46+
version := viper.GetBool("version")
47+
if version {
48+
fmt.Printf("v%s, commit: %s, build on: %s\n", strings.Trim(Version, "v"), GitCommit, Date)
4249
return
4350
}
4451

45-
log.Infof("Log Level is Debug: %t", *debug)
52+
debug := viper.GetBool("debug")
53+
log.Infof("Log Level is Debug: %t", debug)
4654

47-
if *debug {
55+
if debug {
4856
log.SetLevel(log.DebugLevel)
4957
} else {
5058
log.SetLevel(log.InfoLevel)
5159
}
5260

53-
if *enableLogFile {
54-
f, err := os.OpenFile(fmt.Sprintf("%stf-lsp.log", *location), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
61+
enableLogFile := viper.GetBool("enable-log-file")
62+
if enableLogFile {
63+
location := viper.GetString("log-location")
64+
f, err := os.OpenFile(fmt.Sprintf("%stf-lsp.log", location), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
5565
if err != nil {
5666
log.Fatalf("error opening file: %v", err)
5767
}
5868
defer f.Close()
5969
log.SetOutput(f)
6070
}
6171

62-
var oldLogInstance *oldLog.Logger
63-
64-
if *debugJRPC2 {
65-
if !*tcp && !*enableLogFileJRPC2 {
66-
log.Fatal("Debug for JRPC2 has to be set for log file location if is set to use stdio")
67-
}
68-
69-
oldLogInstance = oldLog.New(os.Stdout, "", 0)
70-
if *enableLogFileJRPC2 {
71-
f, err := os.OpenFile(fmt.Sprintf("%stf-lsp-jrpc2.log", *locationJRPC2), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
72-
if err != nil {
73-
log.Fatalf("error opening file: %v", err)
74-
}
75-
defer f.Close()
76-
oldLogInstance.SetOutput(f)
77-
}
72+
var oldLogInstance *oldLog.Logger
73+
74+
debugJRPC2 := viper.GetBool("debug-jrpc2")
75+
tcp := viper.GetBool("tcp")
76+
enableLogFileJRPC2 := viper.GetBool("enable-log-jrpc2-file")
77+
78+
if debugJRPC2 {
79+
if !tcp && !enableLogFileJRPC2 {
80+
log.Fatal("Debug for JRPC2 has to be set for log file location if is set to use stdio")
81+
}
82+
83+
oldLogInstance = oldLog.New(os.Stdout, "", 0)
84+
if enableLogFileJRPC2 {
85+
locationJRPC2 := viper.GetString("log-jrpc2-location")
86+
f, err := os.OpenFile(fmt.Sprintf("%stf-lsp-jrpc2.log", locationJRPC2), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
87+
if err != nil {
88+
log.Fatalf("error opening file: %v", err)
89+
}
90+
defer f.Close()
91+
oldLogInstance.SetOutput(f)
92+
}
7893
}
7994

80-
langserver.InitializeServiceMap()
95+
langserver.InitializeServiceMap()
8196

82-
if *tcp {
83-
langserver.RunTCPServer(*address, *port, oldLogInstance)
97+
if tcp {
98+
address := viper.GetString("address")
99+
port := viper.GetInt("port")
100+
langserver.RunTCPServer(address, port, oldLogInstance)
84101
} else {
85102
langserver.RunStdioServer(oldLogInstance)
86103
}
87104
}
105+
106+
func configViper() {
107+
// Accept CLI arguments
108+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
109+
flag.Parse()
110+
viper.BindPFlags(pflag.CommandLine)
111+
}

tfstructs/diags.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package tfstructs
22

33
import (
44
"fmt"
5-
"os"
65
v2 "github.com/hashicorp/hcl/v2"
76
"github.com/hashicorp/terraform/configs"
87
"github.com/zclconf/go-cty/cty"
8+
"os"
99
//"github.com/juliosueiras/terraform-lsp/helper"
1010
terragruntConfig "github.com/gruntwork-io/terragrunt/config"
1111
terragruntOptions "github.com/gruntwork-io/terragrunt/options"
@@ -120,13 +120,13 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
120120
resourceTypes[v.Type][v.Name] = cty.DynamicVal
121121
}
122122

123-
targetDir := filepath.Dir(originalFileName)
123+
targetDir := filepath.Dir(originalFileName)
124124

125-
resultedDir := ""
125+
resultedDir := ""
126126
searchLevel := 4
127127
for dir := targetDir; dir != "" && searchLevel != 0; dir = filepath.Dir(dir) {
128128
if _, err := os.Stat(filepath.Join(dir, ".terraform")); err == nil {
129-
resultedDir = dir
129+
resultedDir = dir
130130
break
131131
}
132132
searchLevel -= 1
@@ -136,7 +136,7 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
136136
"path": cty.ObjectVal(map[string]cty.Value{
137137
"cwd": cty.StringVal(filepath.Dir(originalFileName)),
138138
"module": cty.StringVal(filepath.Dir(originalFileName)),
139-
"root": cty.StringVal(resultedDir),
139+
"root": cty.StringVal(resultedDir),
140140
}),
141141
"var": cty.DynamicVal, // Need to check for undefined vars
142142
"module": cty.DynamicVal,
@@ -198,7 +198,7 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
198198
})
199199
}
200200

201-
for _, local := range cfg.Locals {
201+
for _, local := range cfg.Locals {
202202
diags := GetLocalsForDiags(*local, filepath.Dir(originalFileName), variables)
203203

204204
if diags != nil {
@@ -220,7 +220,7 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
220220
})
221221
}
222222
}
223-
}
223+
}
224224

225225
// cfg, diags := configload.NewLoader(&configload.Config{
226226
// ModulesDir: ".terraform/modules",

tfstructs/main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ func GetModuleVariables(moduleAddr string, config hcl.Body, targetDir string) (m
4040
func GetLocalsForDiags(local configs.Local, targetDir string, variables map[string]cty.Value) hcl.Diagnostics {
4141
scope := lang.Scope{}
4242

43-
//_, diags := scope.EvalExpr(local, cty.DynamicPseudoType)
44-
_, diags := local.Expr.Value(
45-
&hcl.EvalContext{
46-
// Build Full Tree
47-
Variables: variables,
48-
Functions: scope.Functions(),
49-
},
50-
)
43+
//_, diags := scope.EvalExpr(local, cty.DynamicPseudoType)
44+
_, diags := local.Expr.Value(
45+
&hcl.EvalContext{
46+
// Build Full Tree
47+
Variables: variables,
48+
Functions: scope.Functions(),
49+
},
50+
)
5151
//res, _, diags := hcldec.PartialDecode(config, nil, &hcl.EvalContext{
5252
// // Build Full Tree
5353
// Variables: variables,

0 commit comments

Comments
 (0)