Skip to content

Commit

Permalink
cli: updated lenient flag to hcl2-strict.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Nov 4, 2021
1 parent d61d985 commit 91061ae
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 25 deletions.
53 changes: 52 additions & 1 deletion command/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,58 @@ job "example" {
_, err = vf.WriteString(fileVars + "\n")
require.NoError(t, err)

j, err := (&JobGetter{}).ApiJobWithArgs(hclf.Name(), cliArgs, []string{vf.Name()})
j, err := (&JobGetter{}).ApiJobWithArgs(hclf.Name(), cliArgs, []string{vf.Name()}, true)
require.NoError(t, err)

require.NotNil(t, j)
require.Equal(t, expected, j.Datacenters)
}

func TestJobGetter_HCL2_Variables_StrictFalse(t *testing.T) {
t.Parallel()

hcl := `
variables {
var1 = "default-val"
var2 = "default-val"
var3 = "default-val"
var4 = "default-val"
}
job "example" {
datacenters = ["${var.var1}", "${var.var2}", "${var.var3}", "${var.var4}"]
}
`

os.Setenv("NOMAD_VAR_var4", "from-envvar")
defer os.Unsetenv("NOMAD_VAR_var4")

// Both the CLI and var file contain variables that are not used with the
// template and therefore would error, if hcl2-strict was true.
cliArgs := []string{`var2=from-cli`,`unsedVar1=from-cli`}
fileVars := `
var3 = "from-varfile"
unsedVar2 = "from-varfile"
`
expected := []string{"default-val", "from-cli", "from-varfile", "from-envvar"}

hclf, err := ioutil.TempFile("", "hcl")
require.NoError(t, err)
defer os.Remove(hclf.Name())
defer hclf.Close()

_, err = hclf.WriteString(hcl)
require.NoError(t, err)

vf, err := ioutil.TempFile("", "var.hcl")
require.NoError(t, err)
defer os.Remove(vf.Name())
defer vf.Close()

_, err = vf.WriteString(fileVars + "\n")
require.NoError(t, err)

j, err := (&JobGetter{}).ApiJobWithArgs(hclf.Name(), cliArgs, []string{vf.Name()}, false)
require.NoError(t, err)

require.NotNil(t, j)
Expand Down
16 changes: 9 additions & 7 deletions command/job_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ Plan Options:
-hcl1
Parses the job file as HCLv1.
-hcl2-strict
Whether an error should be produced from the HCL2 parser where a variable
has been supplied which is not defined within the root variables. Defaults
to true.
-policy-override
Sets the flag to force override any soft mandatory Sentinel policies.
Expand All @@ -88,9 +93,6 @@ Plan Options:
-var-file=path
Path to HCL2 file containing user variables.
-lenient
Do not error out if variables are provided which have not been defined.
-verbose
Increase diff verbosity.
`
Expand All @@ -108,9 +110,9 @@ func (c *JobPlanCommand) AutocompleteFlags() complete.Flags {
"-policy-override": complete.PredictNothing,
"-verbose": complete.PredictNothing,
"-hcl1": complete.PredictNothing,
"-hcl2-strict": complete.PredictNothing,
"-var": complete.PredictAnything,
"-var-file": complete.PredictFiles("*.var"),
"-lenient": complete.PredictNothing,
})
}

Expand All @@ -120,7 +122,7 @@ func (c *JobPlanCommand) AutocompleteArgs() complete.Predictor {

func (c *JobPlanCommand) Name() string { return "job plan" }
func (c *JobPlanCommand) Run(args []string) int {
var diff, policyOverride, verbose, lenient bool
var diff, policyOverride, verbose, hcl2Strict bool
var varArgs, varFiles flaghelper.StringFlag

flagSet := c.Meta.FlagSet(c.Name(), FlagSetClient)
Expand All @@ -129,7 +131,7 @@ func (c *JobPlanCommand) Run(args []string) int {
flagSet.BoolVar(&policyOverride, "policy-override", false, "")
flagSet.BoolVar(&verbose, "verbose", false, "")
flagSet.BoolVar(&c.JobGetter.hcl1, "hcl1", false, "")
flagSet.BoolVar(&lenient, "lenient", false, "")
flagSet.BoolVar(&hcl2Strict, "hcl2-strict", true, "")
flagSet.Var(&varArgs, "var", "")
flagSet.Var(&varFiles, "var-file", "")

Expand All @@ -147,7 +149,7 @@ func (c *JobPlanCommand) Run(args []string) int {

path := args[0]
// Get Job struct from Jobfile
job, err := c.JobGetter.ApiJobWithArgs(args[0], varArgs, varFiles, !lenient)
job, err := c.JobGetter.ApiJobWithArgs(args[0], varArgs, varFiles, hcl2Strict)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting job struct: %s", err))
return 255
Expand Down
16 changes: 9 additions & 7 deletions command/job_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ Run Options:
-hcl1
Parses the job file as HCLv1.
-hcl2-strict
Whether an error should be produced from the HCL2 parser where a variable
has been supplied which is not defined within the root variables. Defaults
to true.
-output
Output the JSON that would be submitted to the HTTP API without submitting
the job.
Expand Down Expand Up @@ -121,9 +126,6 @@ Run Options:
-var-file=path
Path to HCL2 file containing user variables.
-lenient
Do not error out if variables are provided which have not been defined.
-verbose
Display full information.
`
Expand All @@ -147,9 +149,9 @@ func (c *JobRunCommand) AutocompleteFlags() complete.Flags {
"-policy-override": complete.PredictNothing,
"-preserve-counts": complete.PredictNothing,
"-hcl1": complete.PredictNothing,
"-hcl2-strict": complete.PredictNothing,
"-var": complete.PredictAnything,
"-var-file": complete.PredictFiles("*.var"),
"-lenient": complete.PredictNothing,
})
}

Expand All @@ -160,7 +162,7 @@ func (c *JobRunCommand) AutocompleteArgs() complete.Predictor {
func (c *JobRunCommand) Name() string { return "job run" }

func (c *JobRunCommand) Run(args []string) int {
var detach, verbose, output, override, preserveCounts, lenient bool
var detach, verbose, output, override, preserveCounts, hcl2Strict bool
var checkIndexStr, consulToken, consulNamespace, vaultToken, vaultNamespace string
var varArgs, varFiles flaghelper.StringFlag

Expand All @@ -172,7 +174,7 @@ func (c *JobRunCommand) Run(args []string) int {
flagSet.BoolVar(&override, "policy-override", false, "")
flagSet.BoolVar(&preserveCounts, "preserve-counts", false, "")
flagSet.BoolVar(&c.JobGetter.hcl1, "hcl1", false, "")
flagSet.BoolVar(&lenient, "lenient", false, "")
flagSet.BoolVar(&hcl2Strict, "hcl2-strict", true, "")
flagSet.StringVar(&checkIndexStr, "check-index", "", "")
flagSet.StringVar(&consulToken, "consul-token", "", "")
flagSet.StringVar(&consulNamespace, "consul-namespace", "", "")
Expand Down Expand Up @@ -200,7 +202,7 @@ func (c *JobRunCommand) Run(args []string) int {
}

// Get Job struct from Jobfile
job, err := c.JobGetter.ApiJobWithArgs(args[0], varArgs, varFiles, !lenient)
job, err := c.JobGetter.ApiJobWithArgs(args[0], varArgs, varFiles, hcl2Strict)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting job struct: %s", err))
return 1
Expand Down
22 changes: 12 additions & 10 deletions command/job_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ Validate Options:
-hcl1
Parses the job file as HCLv1.
-hcl2-strict
Whether an error should be produced from the HCL2 parser where a variable
has been supplied which is not defined within the root variables. Defaults
to true.
-var 'key=value'
Variable for template, can be used multiple times.
-var-file=path
Path to HCL2 file containing user variables.
-lenient
Do not error out if variables are provided which have not been defined.
`
return strings.TrimSpace(helpText)
}
Expand All @@ -55,10 +57,10 @@ func (c *JobValidateCommand) Synopsis() string {

func (c *JobValidateCommand) AutocompleteFlags() complete.Flags {
return complete.Flags{
"-hcl1": complete.PredictNothing,
"-var": complete.PredictAnything,
"-var-file": complete.PredictFiles("*.var"),
"-lenient": complete.PredictNothing,
"-hcl1": complete.PredictNothing,
"-hcl2-strict": complete.PredictNothing,
"-var": complete.PredictAnything,
"-var-file": complete.PredictFiles("*.var"),
}
}

Expand All @@ -70,12 +72,12 @@ func (c *JobValidateCommand) Name() string { return "job validate" }

func (c *JobValidateCommand) Run(args []string) int {
var varArgs, varFiles flaghelper.StringFlag
var lenient bool
var hcl2Strict bool

flagSet := c.Meta.FlagSet(c.Name(), FlagSetNone)
flagSet.Usage = func() { c.Ui.Output(c.Help()) }
flagSet.BoolVar(&c.JobGetter.hcl1, "hcl1", false, "")
flagSet.BoolVar(&lenient, "lenient", false, "")
flagSet.BoolVar(&hcl2Strict, "hcl2-strict", true, "")
flagSet.Var(&varArgs, "var", "")
flagSet.Var(&varFiles, "var-file", "")

Expand All @@ -92,7 +94,7 @@ func (c *JobValidateCommand) Run(args []string) int {
}

// Get Job struct from Jobfile
job, err := c.JobGetter.ApiJobWithArgs(args[0], varArgs, varFiles, !lenient)
job, err := c.JobGetter.ApiJobWithArgs(args[0], varArgs, varFiles, hcl2Strict)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting job struct: %s", err))
return 1
Expand Down

0 comments on commit 91061ae

Please sign in to comment.