mirror of
https://github.com/hashicorp/packer.git
synced 2026-02-24 10:21:20 -05:00
* Update validation options for undeclared variables
In an effort to help users move from JSON to HCL2 templates the support for
variable definitions files are being updated to ignore undeclared
variable warnings on build execution. For legacy JSON templates builds no
warnings are displayed when var-files contain undeclared variables.
Since preferred mode HCL2 templates is to be explicit with variable
declarations - they must be declared to be used - validation for
undeclared variables still warns when running `packer validate`. A new
flag has been added to the validate command that can be used to disable
undeclared variable warnings.
* Update validation test for unused variables
Example Run
```
~> go run . validate -no-warn-undeclared-var -var-file
command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl
command/test-fixtures/validate/var-file-tests/basic.pkr.hcl
The configuration is valid.
~> go run . validate -var-file
command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl
command/test-fixtures/validate/var-file-tests/basic.pkr.hcl
Warning: Undefined variable
The variable "unused" was set but was not declared as an input variable.
To declare variable "unused" place this block in one of your .pkr.hcl
files,
such as variables.pkr.hcl
variable "unused" {
type = string
default = null
}
The configuration is valid.
~> go run . build -var-file
command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl
command/test-fixtures/validate/var-file-tests/basic.pkr.hcl
file.chocolate: output will be in this color.
Build 'file.chocolate' finished after 744 microseconds.
==> Wait completed after 798 microseconds
==> Builds finished. The artifacts of successful builds are:
--> file.chocolate: Stored file: chocolate.txt
```
* Rename Strict field to WarnOnUndeclaredVar
The field name Strict is a bit vague since it is only used for
checking against undeclared variables within a var-file definition.
To mitigate against potential overloading of this field it is
being renamed to be more explicit on its usage.
* command/build: Add warn-on-undeclared-var flag
Now that the default behaviour is to not display warnings for undeclared variables
an optional flag has been added to toggle the old behaviour.
```
~> go run . build -warn-on-undeclared-var -var-file command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl command/test-fixtures/validate/var-file-tests/basic.pkr.hcl
Warning: Undefined variable
The variable "unused" was set but was not declared as an input variable.
To declare variable "unused" place this block in one of your .pkr.hcl files,
such as variables.pkr.hcl
variable "unused" {
type = string
default = null
}
file.chocolate: output will be in this color.
Build 'file.chocolate' finished after 762 microseconds.
==> Wait completed after 799 microseconds
==> Builds finished. The artifacts of successful builds are:
--> file.chocolate: Stored file: chocolate.txt
```
|
||
|---|---|---|
| .. | ||
| enumflag | ||
| flag-kv | ||
| flag-slice | ||
| test-fixtures | ||
| build.go | ||
| build_cancellation_test.go | ||
| build_cleanup_script_test.go | ||
| build_parallel_test.go | ||
| build_test.go | ||
| build_timeout_test.go | ||
| build_windows_test.go | ||
| cli.go | ||
| command_test.go | ||
| config_file_unix.go | ||
| config_file_windows.go | ||
| configtype_enumer.go | ||
| console.go | ||
| console_test.go | ||
| exec_test.go | ||
| fix.go | ||
| fix_test.go | ||
| fmt.go | ||
| fmt_test.go | ||
| hcl2_upgrade.go | ||
| hcl2_upgrade_test.go | ||
| init.go | ||
| init_test.go | ||
| inspect.go | ||
| inspect_test.go | ||
| meta.go | ||
| plugin.go | ||
| plugins.go | ||
| plugins_install.go | ||
| plugins_install_test.go | ||
| plugins_installed.go | ||
| plugins_remove.go | ||
| plugins_remove_test.go | ||
| plugins_required.go | ||
| signal.go | ||
| test_utils.go | ||
| utils.go | ||
| utils_test.go | ||
| validate.go | ||
| validate_test.go | ||
| vendored_plugins.go | ||
| version.go | ||
| version_test.go | ||