Skip to content

Commit

Permalink
Fixed windows path issue and added log level
Browse files Browse the repository at this point in the history
  • Loading branch information
juliosueiras committed Feb 17, 2020
1 parent 8cf7039 commit d253e8b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 18 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/go-errors/errors v1.0.1 // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/gruntwork-io/terragrunt v0.21.13
github.com/gruntwork-io/terragrunt v0.21.13 // indirect
github.com/hashicorp/go-hclog v0.9.0 // indirect
github.com/hashicorp/go-plugin v1.0.1-0.20190610192547-a1bc61569a26
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -21,6 +21,7 @@ require (
github.com/mattn/go-zglob v0.0.1 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/reflectwalk v1.0.1 // indirect
github.com/sirupsen/logrus v1.2.0
github.com/sourcegraph/go-lsp v0.0.0-20181119182933-0c7d621186c1
github.com/spf13/afero v1.2.2
github.com/ulikunitz/xz v0.5.6 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALr
github.com/keybase/go-crypto v0.0.0-20161004153544-93f5b35093ba/go.mod h1:ghbZscTyKdM07+Fw3KSi0hcJm+AlEUWj8QLlPtijN/M=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down Expand Up @@ -340,6 +341,7 @@ github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s=
Expand Down
9 changes: 6 additions & 3 deletions helper/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/sourcegraph/go-lsp"
"github.com/spf13/afero"
"github.com/zclconf/go-cty/cty"
"log"
log "github.com/sirupsen/logrus"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -65,6 +65,10 @@ func CheckAndGetConfig(parser *configs.Parser, originalFile afero.File, line int

// credits: https://stackoverflow.com/questions/28008566/how-to-compute-the-offset-from-column-and-line-number-go
func FindOffset(fileText string, line, column int) int {
if column == 0 {
column = 1
}

currentCol := 1
currentLine := 1

Expand All @@ -79,13 +83,12 @@ func FindOffset(fileText string, line, column int) int {
} else {
currentCol++
}

}
return -1
}

func DumpLog(res interface{}) {
log.Println(spew.Sdump(res))
log.Debug(spew.Sdump(res))
}

func ParseVariables(vars hcl.Traversal, configVars map[string]*configs.Variable, completionItems []lsp.CompletionItem) []lsp.CompletionItem {
Expand Down
7 changes: 5 additions & 2 deletions langserver/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/lang"
lsp "github.com/sourcegraph/go-lsp"
"net/url"
"path/filepath"
"reflect"
"regexp"
Expand All @@ -26,13 +27,14 @@ func TextDocumentComplete(ctx context.Context, vs lsp.CompletionParams) (lsp.Com

fileURL := strings.Replace(string(vs.TextDocument.URI), "file://", "", 1)

fileDir := filepath.Dir(fileURL)
decodedFileURL , _ := url.QueryUnescape(fileURL)
fileDir := filepath.Dir(decodedFileURL)
res, _ := filepath.Glob(fileDir + "/*.tf")
var file *configs.File
var resultFiles []*configs.File

for _, v := range res {
if fileURL == v {
if strings.ToLower(decodedFileURL) == strings.ToLower(v) {
continue
}

Expand Down Expand Up @@ -218,6 +220,7 @@ func TextDocumentComplete(ctx context.Context, vs lsp.CompletionParams) (lsp.Com

if expr != nil {
helper.DumpLog("Found Expression")
helper.DumpLog(posHCL)
helper.DumpLog(expr)
//.*for.*in\s+([^:]*)
//te, te2 := hclsyntax.ParseExpression([]byte("aws[0].test"), "test", hcl.Pos{
Expand Down
4 changes: 2 additions & 2 deletions langserver/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package langserver

import (
"context"
"log"
"os"
"github.com/juliosueiras/terraform-lsp/memfs"

log "github.com/sirupsen/logrus"
lsp "github.com/sourcegraph/go-lsp"
)

Expand All @@ -15,7 +15,7 @@ func Exit(ctx context.Context, vs lsp.None) error {
return err
}

log.Println("Exited")
log.Info("Exited")
os.Exit(0)
return nil
}
2 changes: 1 addition & 1 deletion langserver/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
lsp "github.com/sourcegraph/go-lsp"
"github.com/juliosueiras/terraform-lsp/memfs"
"github.com/spf13/afero"
"log"
log "github.com/sirupsen/logrus"
)

func Initialize(ctx context.Context, vs lsp.InitializeParams) (lsp.InitializeResult, error) {
Expand Down
4 changes: 2 additions & 2 deletions langserver/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package langserver

import (
"context"
"log"
"github.com/juliosueiras/terraform-lsp/memfs"

log "github.com/sirupsen/logrus"
lsp "github.com/sourcegraph/go-lsp"
)

Expand All @@ -14,6 +14,6 @@ func Shutdown(ctx context.Context, vs lsp.None) error {
return err
}

log.Println("Shutdown")
log.Info("Shutdown")
return nil
}
21 changes: 18 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
oldLog "log"
log "github.com/sirupsen/logrus"

"io/ioutil"
"github.com/creachadair/jrpc2/channel"
"github.com/juliosueiras/terraform-lsp/langserver"
)

var location = flag.String("log-location", "", "Location of the lsp log")
var debug = flag.Bool("debug", false, "Enable debug output")
var enableLogFile = flag.Bool("enable-log-file", false, "Enable log file")

var Version string
Expand All @@ -23,13 +26,25 @@ var version = flag.Bool("version", false, "Show version")
func main() {
flag.Parse()

oldLog.SetOutput(ioutil.Discard)
oldLog.SetFlags(0)


if *version {
fmt.Printf("v%s, commit: %s, build on: %s", strings.Trim(Version, "v"), GitCommit, Date)
return
}

Server := langserver.CreateServer()

log.Infof("Log Level is Debug: %t", *debug)

if *debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}

if *enableLogFile {
f, err := os.OpenFile(fmt.Sprintf("%stf-lsp.log", *location), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
Expand All @@ -41,12 +56,12 @@ func main() {

// Start the server on a channel comprising stdin/stdout.
Server.Start(channel.Header("")(os.Stdin, os.Stdout))
log.Print("Server started")
log.Info("Server started")

// Wait for the server to exit, and report any errors.
if err := Server.Wait(); err != nil {
log.Printf("Server exited: %v", err)
}

log.Print("Server Finish")
log.Info("Server Finish")
}
31 changes: 27 additions & 4 deletions tfstructs/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
package tfstructs

import (
"unicode/utf8"
"fmt"
"go/build"
"log"
log "github.com/sirupsen/logrus"
oldLog "log"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -141,6 +144,8 @@ func findPlugin(pluginType string, pluginName string, targetDir string) (*discov
return nil, err
}

oldLog.SetOutput(ioutil.Discard)

pluginMetaSet := discovery.FindPlugins(pluginType, dirs).WithName(pluginName)

// if pluginMetaSet doesn't have any pluginMeta, pluginMetaSet.Newest() will call panic.
Expand Down Expand Up @@ -182,13 +187,31 @@ func pluginDirs(targetDir string) ([]string, error) {
// auto installed directory
// This does not take into account overriding the data directory.
autoInstalledDir := ""
for dir := targetDir; dir != "" && strings.HasPrefix(dir, homeDir); dir = filepath.Dir(dir) {
log.Printf("[DEBUG] search .terraform dir in %s", dir)

s, i := utf8.DecodeRuneInString("\\")
if []rune(targetDir)[0] == s {
// https://stackoverflow.com/questions/48798588/how-do-you-remove-the-first-character-of-a-string
targetDir = targetDir[i:]
}

for dir := targetDir; dir != ""; dir = filepath.Dir(dir) {

if dir[1:] == ":\\" {
if _, err := os.Stat(filepath.Join(dir, ".terraform")); err == nil {
autoInstalledDir = filepath.Join(dir, ".terraform", "plugins", arch)
}
break
}

log.Debug("[DEBUG] search .terraform dir in %s", dir)

if _, err := os.Stat(filepath.Join(dir, ".terraform")); err == nil {
autoInstalledDir = filepath.Join(dir, ".terraform", "plugins", arch)
break
}

}

if autoInstalledDir != "" {
dirs = append(dirs, autoInstalledDir)
}
Expand All @@ -208,7 +231,7 @@ func pluginDirs(targetDir string) ([]string, error) {
gopath := build.Default.GOPATH
dirs = append(dirs, filepath.Join(gopath, "bin"))

log.Printf("[DEBUG] plugin dirs: %#v", dirs)
log.Debug("[DEBUG] plugin dirs: %#v", dirs)
return dirs, nil
}

Expand Down

0 comments on commit d253e8b

Please sign in to comment.