Skip to content

Commit

Permalink
Added path.cwd and path.module with correct path, Added error check f…
Browse files Browse the repository at this point in the history
…or tfvars
  • Loading branch information
juliosueiras committed Feb 18, 2020
1 parent 058c36f commit aee4615
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions tfstructs/diags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tfstructs
import (
"fmt"
"github.com/hashicorp/terraform/configs"
v2 "github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
//"github.com/juliosueiras/terraform-lsp/helper"
"github.com/juliosueiras/terraform-lsp/memfs"
Expand All @@ -25,7 +26,18 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
originalFile = fileName
}

_, hclDiags := parser.LoadHCLFile(fileName)
var hclDiags v2.Diagnostics
isTFVars := (filepath.Ext(originalFile) == ".tfvars")

var diagName string

if isTFVars {
_, hclDiags = parser.LoadValuesFile(fileName)
diagName = "TFVars"
} else {
_, hclDiags = parser.LoadHCLFile(fileName)
diagName = "HCL"
}

for _, diag := range hclDiags {
result = append(result, lsp.Diagnostic{
Expand All @@ -41,10 +53,14 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {
Character: diag.Subject.End.Column - 1,
},
},
Source: "HCL",
Source: diagName,
})
}

if isTFVars {
return result
}

cfg, tfDiags := parser.LoadConfigFile(fileName)
parser.ForceFileSource(originalFileName, []byte(""))
extra, _ := parser.LoadConfigDir(filepath.Dir(originalFileName))
Expand All @@ -71,14 +87,17 @@ func GetDiagnostics(fileName string, originalFile string) []lsp.Diagnostic {

variables := map[string]cty.Value{
"path": cty.ObjectVal(map[string]cty.Value{
"cwd": cty.StringVal(""),
"module": cty.StringVal(""),
"cwd": cty.StringVal(filepath.Dir(originalFile)),
"module": cty.StringVal(filepath.Dir(originalFile)),
}),
"var": cty.DynamicVal, // Need to check for undefined vars
"module": cty.DynamicVal,
"local": cty.DynamicVal,
"each": cty.DynamicVal,
"count": cty.DynamicVal,
"terraform": cty.ObjectVal(map[string]cty.Value{
"workspace": cty.StringVal(""),
}),
}

for k, v := range resourceTypes {
Expand Down

0 comments on commit aee4615

Please sign in to comment.