Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for JSON variable files #697

Merged
merged 2 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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