Skip to content

Commit

Permalink
Implement support for JSON variable files (#697)
Browse files Browse the repository at this point in the history
* identify and parse json tfvars

* add json to autoloaded check
  • Loading branch information
dbanck authored Nov 9, 2021
1 parent b2cf4f6 commit 15bb6e1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 11 additions & 2 deletions internal/terraform/ast/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@ func NewVarsFilename(name string) (VarsFilename, bool) {
}

func IsVarsFilename(name string) bool {
return strings.HasSuffix(name, ".tfvars") && !isIgnoredFile(name)
return (strings.HasSuffix(name, ".tfvars") ||
strings.HasSuffix(name, ".tfvars.json")) &&
!isIgnoredFile(name)
}

func (vf VarsFilename) String() string {
return string(vf)
}

func (vf VarsFilename) IsJSON() bool {
return strings.HasSuffix(string(vf), ".json")
}

func (vf VarsFilename) IsAutoloaded() bool {
name := string(vf)
return strings.HasSuffix(name, ".auto.tfvars") || name == "terraform.tfvars"
return strings.HasSuffix(name, ".auto.tfvars") ||
strings.HasSuffix(name, ".auto.tfvars.json") ||
name == "terraform.tfvars" ||
name == "terraform.tfvars.json"
}

type VarsFiles map[VarsFilename]*hcl.File
Expand Down
6 changes: 3 additions & 3 deletions internal/terraform/parser/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package parser
import (
"path/filepath"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/terraform-ls/internal/terraform/ast"
)

Expand Down Expand Up @@ -35,8 +33,10 @@ func ParseVariableFiles(fs FS, modPath string) (ast.VarsFiles, ast.VarsDiags, er
return nil, nil, err
}

f, pDiags := hclsyntax.ParseConfig(src, name, hcl.InitialPos)
filename := ast.VarsFilename(name)

f, pDiags := parseFile(src, filename)

diags[filename] = pDiags
if f != nil {
files[filename] = f
Expand Down

0 comments on commit 15bb6e1

Please sign in to comment.