Skip to content

Commit

Permalink
command/build: Add warn-on-undeclared-var flag
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
Wilken Rivera committed Nov 14, 2022
1 parent dc8c29c commit 7820bd2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,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)
Expand Down
1 change: 1 addition & 0 deletions command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 7820bd2

Please sign in to comment.