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

feat: add tfvars.json autodetected variable support #2869

Merged
merged 1 commit into from
Feb 7, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions cmd/infracost/testdata/generate/tfvar_json/expected.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 0.1

projects:
- path: terraform
name: terraform-dev
terraform_var_files:
- ../variables/env/dev/tfvars.json
skip_autodetect: true
- path: terraform
name: terraform-prod
terraform_var_files:
- ../variables/env/prod/tfvars.json
skip_autodetect: true

9 changes: 9 additions & 0 deletions cmd/infracost/testdata/generate/tfvar_json/tree.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.
├── terraform
│ └── main.tf
└── variables
└── env
├── prod
│ └── tfvars.json
└── dev
└── tfvars.json
8 changes: 7 additions & 1 deletion internal/hcl/project_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ func (p *ProjectLocator) walkPaths(fullPath string, level int) {
parseFunc = hclParser.ParseJSONFile
}

if strings.HasSuffix(name, ".tfvars") || strings.HasSuffix(name, ".tfvars.json") {
if p.isTerraformVarFile(name) {
v, ok := p.discoveredVarFiles[fullPath]
if !ok {
v = []RootPathVarFile{{
Expand Down Expand Up @@ -1271,6 +1271,12 @@ func (p *ProjectLocator) walkPaths(fullPath string, level int) {
}
}

func (p *ProjectLocator) isTerraformVarFile(name string) bool {
return strings.HasSuffix(name, ".tfvars") ||
strings.HasSuffix(name, ".tfvars.json") ||
(strings.HasPrefix(name, "tfvars") && strings.HasSuffix(name, ".json"))
}

type terraformDirInfo struct {
hasProviderBlock bool
hasTerraformBackendBlock bool
Expand Down