Skip to content

Commit

Permalink
Rename Strict field to WarnOnUndeclaredVar
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Wilken Rivera committed Nov 14, 2022
1 parent 5818a0f commit fd7d765
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 18 deletions.
11 changes: 7 additions & 4 deletions command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ type MetaArgs struct {
Vars map[string]string
VarFiles []string
// set to "hcl2" to force hcl2 mode
ConfigType configType
StrictValidation bool
ConfigType configType

// WarnOnUndeclared does not have a common default, as the default varies per sub-command usage.
// Refer to individual command FlagSets for usage.
WarnOnUndeclaredVar bool
}

func (ba *BuildArgs) AddFlagSets(flags *flag.FlagSet) {
Expand Down Expand Up @@ -131,15 +134,15 @@ type FixArgs struct {

func (va *ValidateArgs) AddFlagSets(flags *flag.FlagSet) {
flags.BoolVar(&va.SyntaxOnly, "syntax-only", false, "check syntax only")
flags.BoolVar(&va.WarnOnUndeclared, "warn-on-undeclared", true, "show warnings for pkrvars definition files containing undeclared variables")
flags.BoolVar(&va.NoWarnUndeclaredVar, "no-warn-undeclared-var", false, "Ignore warnings for variable files containing undeclared variables.")

va.MetaArgs.AddFlagSets(flags)
}

// ValidateArgs represents a parsed cli line for a `packer validate`
type ValidateArgs struct {
MetaArgs
SyntaxOnly, WarnOnUndeclared bool
SyntaxOnly, NoWarnUndeclaredVar bool
}

func (va *InspectArgs) AddFlagSets(flags *flag.FlagSet) {
Expand Down
2 changes: 1 addition & 1 deletion command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (m *Meta) GetConfigFromHCL(cla *MetaArgs) (*hcl2template.PackerConfig, int)
Parser: hclparse.NewParser(),
PluginConfig: m.CoreConfig.Components.PluginConfig,
ValidationOptions: hcl2template.ValidationOptions{
Strict: cla.StrictValidation,
WarnOnUndeclaredVar: cla.WarnOnUndeclaredVar,
},
}
cfg, diags := parser.Parse(cla.Path, cla.VarFiles, cla.Vars)
Expand Down
13 changes: 8 additions & 5 deletions command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func (c *ValidateCommand) ParseArgs(args []string) (*ValidateArgs, int) {
}

func (c *ValidateCommand) RunContext(ctx context.Context, cla *ValidateArgs) int {
cla.StrictValidation = cla.WarnOnUndeclared
// 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 {
Expand All @@ -61,7 +65,6 @@ func (c *ValidateCommand) RunContext(ctx context.Context, cla *ValidateArgs) int
diags := packerStarter.Initialize(packer.InitializeOptions{
SkipDatasourcesExecution: true,
})

ret = writeDiags(c.Ui, nil, diags)
if ret != 0 {
return ret
Expand Down Expand Up @@ -100,11 +103,11 @@ Options:
-syntax-only Only check syntax. Do not verify config of the template.
-except=foo,bar,baz Validate all builds other than these.
-machine-readable Produce machine-readable output.
-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.
-warn-on-undeclared=false Disable warnings for JSON or HCL2 files containing undeclared variables. (Default true)
-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)
Expand Down
10 changes: 5 additions & 5 deletions command/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ func TestValidateCommand_VarFilesWarnOnUndeclared(t *testing.T) {
varfile string
exitCode int
}{
{name: "warn-on-undeclared=true with unused variable in var-file definition",
{name: "default warning with unused variable in HCL var-file definition",
path: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "basic.pkr.hcl"),
varfile: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "undeclared.pkrvars.hcl"),
exitCode: 0,
},
{name: "warn-on-undeclared=true with unused variable in var-file definition",
{name: "default warning with unused variable in JSON var-file definition",
path: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "basic.pkr.hcl"),
varfile: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "undeclared.json"),
exitCode: 0,
Expand Down Expand Up @@ -302,12 +302,12 @@ func TestValidateCommand_VarFilesDisableWarnOnUndeclared(t *testing.T) {
varfile string
exitCode int
}{
{name: "warn-on-undeclared=false with unused variable in var-file definition",
{name: "no-warn-undeclared-var with unused variable in HCL var-file definition",
path: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "basic.pkr.hcl"),
varfile: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "undeclared.pkrvars.hcl"),
exitCode: 0,
},
{name: "warn-on-undeclared=false with unused variable in JSON var-file definition",
{name: "no-warn-undeclared-var with unused variable in JSON var-file definition",
path: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "basic.pkr.hcl"),
varfile: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "undeclared.json"),
exitCode: 0,
Expand All @@ -319,7 +319,7 @@ func TestValidateCommand_VarFilesDisableWarnOnUndeclared(t *testing.T) {
Meta: TestMetaFile(t),
}
tc := tc
args := []string{"-warn-on-undeclared=false", "-var-file", tc.varfile, tc.path}
args := []string{"-no-warn-undeclared-var", "-var-file", tc.varfile, tc.path}
if code := c.Run(args); code != tc.exitCode {
fatalCommand(t, c.Meta)
}
Expand Down
2 changes: 1 addition & 1 deletion hcl2template/types.packer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type PackerConfig struct {
}

type ValidationOptions struct {
Strict bool
WarnOnUndeclaredVar bool
}

const (
Expand Down
2 changes: 1 addition & 1 deletion hcl2template/types.variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func (cfg *PackerConfig) collectInputVariableValues(env []string, files []*hcl.F
for name, attr := range attrs {
variable, found := variables[name]
if !found {
if cfg.ValidationOptions.Strict == false {
if !cfg.ValidationOptions.WarnOnUndeclaredVar {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion hcl2template/types.variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ func TestVariables_collectVariableValues(t *testing.T) {
{name: "undefined but set value - pkrvar file - strict mode",
variables: Variables{},
validationOptions: ValidationOptions{
Strict: true,
WarnOnUndeclaredVar: true,
},
args: args{
hclFiles: []string{`undefined_string="value"`},
Expand Down

0 comments on commit fd7d765

Please sign in to comment.