mirror of
https://github.com/hashicorp/packer.git
synced 2026-04-27 09:09:52 -04: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
```
133 lines
3.4 KiB
Go
133 lines
3.4 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/posener/complete"
|
|
)
|
|
|
|
type ValidateCommand struct {
|
|
Meta
|
|
}
|
|
|
|
func (c *ValidateCommand) Run(args []string) int {
|
|
ctx, cleanup := handleTermInterrupt(c.Ui)
|
|
defer cleanup()
|
|
|
|
cfg, ret := c.ParseArgs(args)
|
|
if ret != 0 {
|
|
return ret
|
|
}
|
|
|
|
return c.RunContext(ctx, cfg)
|
|
}
|
|
|
|
func (c *ValidateCommand) ParseArgs(args []string) (*ValidateArgs, int) {
|
|
var cfg ValidateArgs
|
|
|
|
flags := c.Meta.FlagSet("validate", FlagSetBuildFilter|FlagSetVars)
|
|
flags.Usage = func() { c.Ui.Say(c.Help()) }
|
|
cfg.AddFlagSets(flags)
|
|
if err := flags.Parse(args); err != nil {
|
|
return &cfg, 1
|
|
}
|
|
|
|
args = flags.Args()
|
|
if len(args) != 1 {
|
|
flags.Usage()
|
|
return &cfg, 1
|
|
}
|
|
cfg.Path = args[0]
|
|
return &cfg, 0
|
|
}
|
|
|
|
func (c *ValidateCommand) RunContext(ctx context.Context, cla *ValidateArgs) int {
|
|
// By default we want to inform users of undeclared variables when validating but not during build time.
|
|
cla.MetaArgs.WarnOnUndeclaredVar = true
|
|
if cla.NoWarnUndeclaredVar {
|
|
cla.MetaArgs.WarnOnUndeclaredVar = false
|
|
}
|
|
|
|
packerStarter, ret := c.GetConfig(&cla.MetaArgs)
|
|
if ret != 0 {
|
|
return 1
|
|
}
|
|
|
|
// If we're only checking syntax, then we're done already
|
|
if cla.SyntaxOnly {
|
|
c.Ui.Say("Syntax-only check passed. Everything looks okay.")
|
|
return 0
|
|
}
|
|
|
|
diags := packerStarter.Initialize(packer.InitializeOptions{
|
|
SkipDatasourcesExecution: true,
|
|
})
|
|
ret = writeDiags(c.Ui, nil, diags)
|
|
if ret != 0 {
|
|
return ret
|
|
}
|
|
|
|
_, _, diags = packerStarter.GetBuilds(packer.GetBuildsOptions{
|
|
Only: cla.Only,
|
|
Except: cla.Except,
|
|
})
|
|
|
|
fixerDiags := packerStarter.FixConfig(packer.FixConfigOptions{
|
|
Mode: packer.Diff,
|
|
})
|
|
diags = append(diags, fixerDiags...)
|
|
|
|
ret = writeDiags(c.Ui, nil, diags)
|
|
if ret == 0 {
|
|
c.Ui.Say("The configuration is valid.")
|
|
}
|
|
|
|
return ret
|
|
}
|
|
|
|
func (*ValidateCommand) Help() string {
|
|
helpText := `
|
|
Usage: packer validate [options] TEMPLATE
|
|
|
|
Checks the template is valid by parsing the template and also
|
|
checking the configuration with the various builders, provisioners, etc.
|
|
|
|
If it is not valid, the errors will be shown and the command will exit
|
|
with a non-zero exit status. If it is valid, it will exit with a zero
|
|
exit status.
|
|
|
|
Options:
|
|
|
|
-syntax-only Only check syntax. Do not verify config of the template.
|
|
-except=foo,bar,baz Validate all builds other than these.
|
|
-only=foo,bar,baz Validate only these builds.
|
|
-machine-readable Produce machine-readable output.
|
|
-var 'key=value' Variable for templates, can be used multiple times.
|
|
-var-file=path JSON or HCL2 file containing user variables, can be used multiple times.
|
|
-no-warn-undeclared-var Disable warnings for user variable files containing undeclared variables.
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
}
|
|
|
|
func (*ValidateCommand) Synopsis() string {
|
|
return "check that a template is valid"
|
|
}
|
|
|
|
func (*ValidateCommand) AutocompleteArgs() complete.Predictor {
|
|
return complete.PredictNothing
|
|
}
|
|
|
|
func (*ValidateCommand) AutocompleteFlags() complete.Flags {
|
|
return complete.Flags{
|
|
"-syntax-only": complete.PredictNothing,
|
|
"-except": complete.PredictNothing,
|
|
"-only": complete.PredictNothing,
|
|
"-var": complete.PredictNothing,
|
|
"-machine-readable": complete.PredictNothing,
|
|
"-var-file": complete.PredictNothing,
|
|
}
|
|
}
|