continue and realise and unused undefaulted value triggers an error

This commit is contained in:
Adrien Delorme 2020-02-17 18:05:15 +01:00
parent cebfb1c735
commit 3c213e6eaf
5 changed files with 53 additions and 27 deletions

View File

@ -8,7 +8,7 @@ build {
provisioner "shell" { provisioner "shell" {
name = "provisioner that does something" name = "provisioner that does something"
not_squashed = var.foo not_squashed = var.foo
string = "string${var.proxmox_username}" string = "string"
int = "${41 + 1}" int = "${41 + 1}"
int64 = "${42 + 1}" int64 = "${42 + 1}"
bool = "true" bool = "true"

View File

@ -10,10 +10,6 @@ variable "image_id" {
default = "image-id-default" default = "image-id-default"
} }
variable "proxmox_username" {
type = string
}
variable "port" { variable "port" {
type = number type = number
default = 42 default = 42

View File

@ -21,14 +21,23 @@ func TestParser_complete(t *testing.T) {
&PackerConfig{ &PackerConfig{
Basedir: "testdata/complete", Basedir: "testdata/complete",
InputVariables: Variables{ InputVariables: Variables{
"foo": &Variable{}, "foo": &Variable{
"image_id": &Variable{}, Name: "foo",
"port": &Variable{}, },
"availability_zone_names": &Variable{}, "image_id": &Variable{
"proxmox_username": &Variable{}, Name: "image_id",
},
"port": &Variable{
Name: "port",
},
"availability_zone_names": &Variable{
Name: "availability_zone_names",
},
}, },
LocalVariables: Variables{ LocalVariables: Variables{
"feefoo": &Variable{}, "feefoo": &Variable{
Name: "feefoo",
},
}, },
Sources: map[SourceRef]*SourceBlock{ Sources: map[SourceRef]*SourceBlock{
refVBIsoUbuntu1204: {Type: "virtualbox-iso", Name: "ubuntu-1204"}, refVBIsoUbuntu1204: {Type: "virtualbox-iso", Name: "ubuntu-1204"},

View File

@ -31,6 +31,8 @@ type Variable struct {
// declaration, the type of the default variable will be used. This will // declaration, the type of the default variable will be used. This will
// allow to ensure that users set this variable correctly. // allow to ensure that users set this variable correctly.
Type cty.Type Type cty.Type
// Common name of the variable
Name string
// Description of the variable // Description of the variable
Description string Description string
// When Sensitive is set to true Packer will try it best to hide/obfuscate // When Sensitive is set to true Packer will try it best to hide/obfuscate
@ -59,7 +61,7 @@ func (v *Variable) Value() (cty.Value, *hcl.Diagnostic) {
return cty.UnknownVal(v.Type), &hcl.Diagnostic{ return cty.UnknownVal(v.Type), &hcl.Diagnostic{
Severity: hcl.DiagError, Severity: hcl.DiagError,
Summary: "Unset variable", Summary: fmt.Sprintf("Unset variable %q", v.Name),
Detail: "A used variable must be set or have a default value; see " + Detail: "A used variable must be set or have a default value; see " +
"https://packer.io/docs/configuration/from-1.5/syntax.html for details.", "https://packer.io/docs/configuration/from-1.5/syntax.html for details.",
Context: v.block.DefRange.Ptr(), Context: v.block.DefRange.Ptr(),
@ -74,7 +76,9 @@ func (variables Variables) Values() (map[string]cty.Value, hcl.Diagnostics) {
for k, v := range variables { for k, v := range variables {
var diag *hcl.Diagnostic var diag *hcl.Diagnostic
res[k], diag = v.Value() res[k], diag = v.Value()
diags = append(diags, diag) if diag != nil {
diags = append(diags, diag)
}
} }
return res, diags return res, diags
} }
@ -107,8 +111,10 @@ func (variables *Variables) decodeConfigMap(block *hcl.Block, ectx *hcl.EvalCont
continue continue
} }
(*variables)[key] = &Variable{ (*variables)[key] = &Variable{
Name: key,
DefaultValue: value, DefaultValue: value,
Type: value.Type(), Type: value.Type(),
block: block,
} }
} }
@ -142,7 +148,10 @@ func (variables *Variables) decodeConfig(block *hcl.Block, ectx *hcl.EvalContext
return diags return diags
} }
name := block.Labels[0]
res := &Variable{ res := &Variable{
Name: name,
Description: b.Description, Description: b.Description,
Sensitive: b.Sensitive, Sensitive: b.Sensitive,
block: block, block: block,
@ -200,7 +209,7 @@ func (variables *Variables) decodeConfig(block *hcl.Block, ectx *hcl.EvalContext
}) })
} }
(*variables)[block.Labels[0]] = res (*variables)[name] = res
return diags return diags
} }

View File

@ -23,15 +23,17 @@ func TestParse_variables(t *testing.T) {
&PackerConfig{ &PackerConfig{
Basedir: filepath.Join("testdata", "variables"), Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{ InputVariables: Variables{
"image_name": &Variable{}, "image_name": &Variable{Name: "image_name"},
"key": &Variable{}, "key": &Variable{Name: "key"},
"my_secret": &Variable{}, "my_secret": &Variable{Name: "my_secret"},
"image_id": &Variable{}, "image_id": &Variable{Name: "image_id"},
"port": &Variable{}, "port": &Variable{Name: "port"},
"availability_zone_names": &Variable{ "availability_zone_names": &Variable{
Name: "availability_zone_names",
Description: fmt.Sprintln("Describing is awesome ;D"), Description: fmt.Sprintln("Describing is awesome ;D"),
}, },
"super_secret_password": &Variable{ "super_secret_password": &Variable{
Name: "super_secret_password",
Sensitive: true, Sensitive: true,
Description: fmt.Sprintln("Handle with care plz"), Description: fmt.Sprintln("Handle with care plz"),
}, },
@ -51,7 +53,9 @@ func TestParse_variables(t *testing.T) {
&PackerConfig{ &PackerConfig{
Basedir: filepath.Join("testdata", "variables"), Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{ InputVariables: Variables{
"boolean_value": &Variable{}, "boolean_value": &Variable{
Name: "boolean_value",
},
}, },
}, },
true, true, true, true,
@ -64,7 +68,9 @@ func TestParse_variables(t *testing.T) {
&PackerConfig{ &PackerConfig{
Basedir: filepath.Join("testdata", "variables"), Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{ InputVariables: Variables{
"boolean_value": &Variable{}, "boolean_value": &Variable{
Name: "boolean_value",
},
}, },
}, },
true, true, true, true,
@ -77,23 +83,27 @@ func TestParse_variables(t *testing.T) {
&PackerConfig{ &PackerConfig{
Basedir: filepath.Join("testdata", "variables"), Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{ InputVariables: Variables{
"broken_type": &Variable{}, "broken_type": &Variable{
Name: "broken_type",
},
}, },
}, },
true, true, true, true,
[]packer.Build{}, []packer.Build{},
false, false,
}, },
{"invalid default type", {"unknown key",
defaultParser, defaultParser,
parseTestArgs{"testdata/variables/unknown_key.pkr.hcl", nil}, parseTestArgs{"testdata/variables/unknown_key.pkr.hcl", nil},
&PackerConfig{ &PackerConfig{
Basedir: filepath.Join("testdata", "variables"), Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{ InputVariables: Variables{
"broken_type": &Variable{}, "broken_type": &Variable{
Name: "broken_type",
},
}, },
}, },
true, false, true, true,
[]packer.Build{}, []packer.Build{},
false, false,
}, },
@ -103,7 +113,9 @@ func TestParse_variables(t *testing.T) {
&PackerConfig{ &PackerConfig{
Basedir: filepath.Join("testdata", "variables"), Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{ InputVariables: Variables{
"foo": &Variable{}, "foo": &Variable{
Name: "foo",
},
}, },
Sources: map[SourceRef]*SourceBlock{ Sources: map[SourceRef]*SourceBlock{
SourceRef{"null", "null-builder"}: &SourceBlock{ SourceRef{"null", "null-builder"}: &SourceBlock{
@ -117,7 +129,7 @@ func TestParse_variables(t *testing.T) {
}, },
}, },
}, },
false, false, true, true,
[]packer.Build{}, []packer.Build{},
true, true,
}, },