mirror of
https://github.com/hashicorp/packer.git
synced 2026-05-28 04:35:38 -04:00
Preserve User-Defined Variable File Order (#13350)
* modifying the for loop to preserver user inputted variable files preference * adding test cases * pr comments + refactoring the for loop for better readability. * pr comments + refactoring the for loop for better readability. * pr comments | handling default case * adding additional test case * fixing test cases * fixing test cases. creating a subdirectory to add a test case for both hcl and json auto var files * adding test case for json auto var file
This commit is contained in:
parent
51eeadba3d
commit
8e36b92ce3
8 changed files with 114 additions and 22 deletions
|
|
@ -106,6 +106,21 @@ func TestBuild(t *testing.T) {
|
|||
},
|
||||
fileCheck: fileCheck{expected: []string{"chocolate.txt"}},
|
||||
},
|
||||
{
|
||||
name: "var-args: json - auto varfile sets a peanut env var",
|
||||
args: []string{
|
||||
testFixture("var-arg", "var-arg-test-autovar-json"),
|
||||
},
|
||||
fileCheck: fileCheck{expected: []string{"peanut.txt"}},
|
||||
},
|
||||
|
||||
{
|
||||
name: "var-args: hcl - auto varfile and json -auto varfile sets the value in json auto varfile",
|
||||
args: []string{
|
||||
testFixture("var-arg", "var-arg-tests"),
|
||||
},
|
||||
fileCheck: fileCheck{expected: []string{"peanut.txt"}},
|
||||
},
|
||||
|
||||
{
|
||||
name: "var-args: hcl - hcl varfile sets a apple env var",
|
||||
|
|
@ -124,6 +139,24 @@ func TestBuild(t *testing.T) {
|
|||
},
|
||||
fileCheck: fileCheck{expected: []string{"apple.txt"}},
|
||||
},
|
||||
{
|
||||
name: "var-args: banana json var file then hcl var file sets apple env var",
|
||||
args: []string{
|
||||
"-var-file=" + filepath.Join(testFixture("var-arg"), "banana.json"),
|
||||
"-var-file=" + filepath.Join(testFixture("var-arg"), "apple.hcl"),
|
||||
testFixture("var-arg"),
|
||||
},
|
||||
fileCheck: fileCheck{expected: []string{"apple.txt"}},
|
||||
},
|
||||
{
|
||||
name: "var-args: apple hcl var file then banana json var file sets banana env var",
|
||||
args: []string{
|
||||
"-var-file=" + filepath.Join(testFixture("var-arg"), "apple.hcl"),
|
||||
"-var-file=" + filepath.Join(testFixture("var-arg"), "banana.json"),
|
||||
testFixture("var-arg"),
|
||||
},
|
||||
fileCheck: fileCheck{expected: []string{"banana.txt"}},
|
||||
},
|
||||
|
||||
{
|
||||
name: "var-args: hcl - arg sets a tomato env var",
|
||||
|
|
|
|||
3
command/test-fixtures/var-arg/banana.json
Normal file
3
command/test-fixtures/var-arg/banana.json
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"fruit": "banana"
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
variable "fruit" {
|
||||
type = string
|
||||
}
|
||||
|
||||
locals {
|
||||
fruit = var.fruit
|
||||
}
|
||||
|
||||
source "null" "builder" {
|
||||
communicator = "none"
|
||||
}
|
||||
|
||||
build {
|
||||
sources = [
|
||||
"source.null.builder",
|
||||
]
|
||||
|
||||
provisioner "shell-local" {
|
||||
inline = ["echo ${local.fruit} > ${local.fruit}.txt"]
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"fruit": "peanut"
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
variable "fruit" {
|
||||
type = string
|
||||
}
|
||||
|
||||
locals {
|
||||
fruit = var.fruit
|
||||
}
|
||||
|
||||
source "null" "builder" {
|
||||
communicator = "none"
|
||||
}
|
||||
|
||||
build {
|
||||
sources = [
|
||||
"source.null.builder",
|
||||
]
|
||||
|
||||
provisioner "shell-local" {
|
||||
inline = ["echo ${local.fruit} > ${local.fruit}.txt"]
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
fruit = "icecream"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"fruit": "peanut"
|
||||
}
|
||||
|
|
@ -210,39 +210,44 @@ func (p *Parser) Parse(filename string, varFiles []string, argVars map[string]st
|
|||
{
|
||||
hclVarFiles, jsonVarFiles, moreDiags := GetHCL2Files(filename, hcl2AutoVarFileExt, hcl2AutoVarJsonFileExt)
|
||||
diags = append(diags, moreDiags...)
|
||||
for _, file := range varFiles {
|
||||
|
||||
// Combine all variable files into a single list, preserving the intended precedence and order.
|
||||
// The order is: auto-loaded HCL files, auto-loaded JSON files, followed by user-specified varFiles.
|
||||
// This ensures that user-specified files can override values from auto-loaded files,
|
||||
// and that their relative order is preserved exactly as specified by the user.
|
||||
variableFileNames := append(append(hclVarFiles, jsonVarFiles...), varFiles...)
|
||||
|
||||
var variableFiles []*hcl.File
|
||||
|
||||
for _, file := range variableFileNames {
|
||||
var (
|
||||
f *hcl.File
|
||||
moreDiags hcl.Diagnostics
|
||||
)
|
||||
switch filepath.Ext(file) {
|
||||
case ".hcl":
|
||||
hclVarFiles = append(hclVarFiles, file)
|
||||
f, moreDiags = p.ParseHCLFile(file)
|
||||
case ".json":
|
||||
jsonVarFiles = append(jsonVarFiles, file)
|
||||
f, moreDiags = p.ParseJSONFile(file)
|
||||
default:
|
||||
diags = append(moreDiags, &hcl.Diagnostic{
|
||||
Severity: hcl.DiagError,
|
||||
Summary: "Could not guess format of " + file,
|
||||
Detail: "A var file must be suffixed with `.hcl` or `.json`.",
|
||||
})
|
||||
moreDiags = hcl.Diagnostics{
|
||||
&hcl.Diagnostic{
|
||||
Severity: hcl.DiagError,
|
||||
Summary: "Could not guess format of " + file,
|
||||
Detail: "A var file must be suffixed with `.hcl` or `.json`.",
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
var varFiles []*hcl.File
|
||||
for _, filename := range hclVarFiles {
|
||||
f, moreDiags := p.ParseHCLFile(filename)
|
||||
|
||||
diags = append(diags, moreDiags...)
|
||||
if moreDiags.HasErrors() {
|
||||
continue
|
||||
}
|
||||
varFiles = append(varFiles, f)
|
||||
}
|
||||
for _, filename := range jsonVarFiles {
|
||||
f, moreDiags := p.ParseJSONFile(filename)
|
||||
diags = append(diags, moreDiags...)
|
||||
if moreDiags.HasErrors() {
|
||||
continue
|
||||
}
|
||||
varFiles = append(varFiles, f)
|
||||
variableFiles = append(variableFiles, f)
|
||||
|
||||
}
|
||||
|
||||
diags = append(diags, cfg.collectInputVariableValues(os.Environ(), varFiles, argVars)...)
|
||||
diags = append(diags, cfg.collectInputVariableValues(os.Environ(), variableFiles, argVars)...)
|
||||
}
|
||||
|
||||
return cfg, diags
|
||||
|
|
|
|||
Loading…
Reference in a new issue