From ae73139b0f626f2de36810e470cb40156005331f Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Fri, 11 Nov 2022 11:44:26 -0500 Subject: [PATCH] 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 ``` --- command/build.go | 3 ++- command/cli.go | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/command/build.go b/command/build.go index 787d776bd97..fa801dff2db 100644 --- a/command/build.go +++ b/command/build.go @@ -393,7 +393,8 @@ Options: -parallel-builds=1 Number of builds to run in parallel. 1 disables parallelization. 0 means no limit (Default: 0) -timestamp-ui Enable prefixing of each ui output with an RFC3339 timestamp. -var 'key=value' Variable for templates, can be used multiple times. - -var-file=path JSON or HCL2 file containing user variables. + -var-file=path JSON or HCL2 file containing user variables, can be used multiple times. + -warn-on-undeclared-var Display warnings for user variable files containing undeclared variables. ` return strings.TrimSpace(helpText) diff --git a/command/cli.go b/command/cli.go index 8d8c6b02c7f..826835ef2b3 100644 --- a/command/cli.go +++ b/command/cli.go @@ -86,6 +86,7 @@ func (ba *BuildArgs) AddFlagSets(flags *flag.FlagSet) { flagOnError := enumflag.New(&ba.OnError, "cleanup", "abort", "ask", "run-cleanup-provisioner") flags.Var(flagOnError, "on-error", "") + flags.BoolVar(&ba.MetaArgs.WarnOnUndeclaredVar, "warn-on-undeclared-var", false, "Show warnings for variable files containing undeclared variables.") ba.MetaArgs.AddFlagSets(flags) }