packer/command
Wilken Rivera 57cbe4e203
core: Update validation options for undeclared variables (#12104)
* 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
```
2022-11-14 17:06:45 -05:00
..
enumflag move the various flag packages from helper to command, since command is the only package that uses them. 2020-11-16 14:23:35 -08:00
flag-kv move the various flag packages from helper to command, since command is the only package that uses them. 2020-11-16 14:23:35 -08:00
flag-slice move the various flag packages from helper to command, since command is the only package that uses them. 2020-11-16 14:23:35 -08:00
test-fixtures core: Update validation options for undeclared variables (#12104) 2022-11-14 17:06:45 -05:00
build.go core: Update validation options for undeclared variables (#12104) 2022-11-14 17:06:45 -05:00
build_cancellation_test.go pin packer to golang 1.16 (#10702) 2021-03-02 11:43:58 +01:00
build_cleanup_script_test.go Packer SVC acc tests (#11274) 2021-12-01 15:58:33 +01:00
build_parallel_test.go HCL2 Parse packer.required_plugins block + packer init (#10304) 2021-02-02 18:05:04 +01:00
build_test.go core: Update validation options for undeclared variables (#12104) 2022-11-14 17:06:45 -05:00
build_timeout_test.go HCL2 Parse packer.required_plugins block + packer init (#10304) 2021-02-02 18:05:04 +01:00
build_windows_test.go add possibility to use variables everywhere in post-processor blocks (#11094) 2021-06-15 18:29:26 +02:00
cli.go core: Update validation options for undeclared variables (#12104) 2022-11-14 17:06:45 -05:00
command_test.go command: move func to extract stdout/err in tests 2022-09-23 12:07:32 -04:00
config_file_unix.go update all plugins + pin go to go 1.17 (#11237) 2021-09-02 12:15:13 +02:00
config_file_windows.go update all plugins + pin go to go 1.17 (#11237) 2021-09-02 12:15:13 +02:00
configtype_enumer.go packer console for HCL2 (#9359) 2020-06-05 17:23:54 +02:00
console.go (2) Implement datasources (#10440) 2021-01-20 10:37:16 +01:00
console_test.go console: allow to use console when there are untyped vars (#9864) 2020-09-02 17:19:56 +02:00
exec_test.go hcl2_upgrade: add share_image_gallery workaround (#12087) 2022-10-27 18:09:40 -04:00
fix.go packer fix: state that the command does not work for hcl files (#11238) 2021-08-31 16:12:47 +02:00
fix_test.go remove sdk 2020-12-17 13:29:25 -08:00
fmt.go fix TEMPATE to TEMPLATE in fmt cmd 2021-04-19 14:07:22 +02:00
fmt_test.go fix: print unchanged formatted file when using stdin (#11047) 2021-06-03 14:02:42 +02:00
hcl2_upgrade.go hcp: extract all HCP-related code to hcp package 2022-11-14 13:31:35 -05:00
hcl2_upgrade_test.go hcl2_upgrade: add share_image_gallery workaround (#12087) 2022-10-27 18:09:40 -04:00
init.go packer init: better error handling with a special care for gh rate limit errors (#11330) 2021-10-14 13:57:59 +02:00
init_test.go test: use T.Setenv to set env vars in tests 2022-09-20 14:54:20 -04:00
inspect.go (2) Implement datasources (#10440) 2021-01-20 10:37:16 +01:00
inspect_test.go HCL2: allow calling env as input var default value (#10240) 2020-11-11 11:27:32 +01:00
meta.go core: Update validation options for undeclared variables (#12104) 2022-11-14 17:06:45 -05:00
plugin.go feat: add http datasource (#11658) 2022-05-17 19:26:34 +02:00
plugins.go Add packer plugins command and subcommands to interact with plugins (#11553) 2022-02-10 16:53:50 -05:00
plugins_install.go Add tests for the packer plugins subcommand (#11660) 2022-03-15 16:32:49 -04:00
plugins_install_test.go test: use T.Setenv to set env vars in tests 2022-09-20 14:54:20 -04:00
plugins_installed.go Add packer plugins command and subcommands to interact with plugins (#11553) 2022-02-10 16:53:50 -05:00
plugins_remove.go Set default file extension for binary removal on Windows (#11627) 2022-03-04 10:50:26 +01:00
plugins_remove_test.go test: use T.Setenv to set env vars in tests 2022-09-20 14:54:20 -04:00
plugins_required.go Add packer plugins command and subcommands to interact with plugins (#11553) 2022-02-10 16:53:50 -05:00
signal.go remove sdk 2020-12-17 13:29:25 -08:00
test_utils.go command: move func to extract stdout/err in tests 2022-09-23 12:07:32 -04:00
utils.go build using HCL2 (#8423) 2019-12-17 11:25:56 +01:00
utils_test.go simplify fmt test case a little 2021-03-12 11:07:26 +01:00
validate.go core: Update validation options for undeclared variables (#12104) 2022-11-14 17:06:45 -05:00
validate_test.go core: Update validation options for undeclared variables (#12104) 2022-11-14 17:06:45 -05:00
vendored_plugins.go Remove Oracle plugin from the list of vendored plugins (#11983) 2022-09-15 12:31:52 -04:00
version.go packer.io prefix removal, html extension remove for in-code errors 2020-04-07 17:53:22 -04:00
version_test.go command: version tests 2014-10-27 20:58:44 -07:00