From 91c5a4613c9a337c370d8bccc9edf3b6b76ebed2 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 26 Aug 2020 15:21:32 +0200 Subject: [PATCH 1/6] HCL2 inspect remove debug values --- command/inspect_test.go | 6 +++--- hcl2template/types.packer_config.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/command/inspect_test.go b/command/inspect_test.go index 2b31cedcf..2e8f09908 100644 --- a/command/inspect_test.go +++ b/command/inspect_test.go @@ -19,7 +19,7 @@ func Test_commands(t *testing.T) { > input-variables: -var.fruit: "banana" [debug: {Type:cty.String,CmdValue:banana,VarfileValue:null,EnvValue:null,DefaultValue:null}] +var.fruit: "banana" > local-variables: @@ -46,7 +46,7 @@ local.fruit: "banana" > input-variables: -var.fruit: "peach" [debug: {Type:cty.String,CmdValue:peach,VarfileValue:null,EnvValue:null,DefaultValue:banana}] +var.fruit: "peach" > local-variables: @@ -58,7 +58,7 @@ var.fruit: "peach" [debug: {Type:cty.String,CmdValue:peach,VarfileValue:null,Env > input-variables: -var.fruit: "peach" [debug: {Type:cty.String,CmdValue:peach,VarfileValue:null,EnvValue:null,DefaultValue:banana}] +var.fruit: "peach" > local-variables: diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index d07e92ef2..2685809d5 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -462,7 +462,7 @@ func (p *PackerConfig) printVariables() string { out.WriteString("> input-variables:\n\n") for _, v := range p.InputVariables { val, _ := v.Value() - fmt.Fprintf(out, "var.%s: %q [debug: %#v]\n", v.Name, PrintableCtyValue(val), v) + fmt.Fprintf(out, "var.%s: %q\n", v.Name, PrintableCtyValue(val)) } out.WriteString("\n> local-variables:\n\n") for _, v := range p.LocalVariables { From eb4069a1b749e4b9fd49c058f41def66a6187542 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 27 Aug 2020 11:54:06 +0200 Subject: [PATCH 2/6] hcl variables: return an unknown value in when no default is set --- hcl2template/types.variables.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/hcl2template/types.variables.go b/hcl2template/types.variables.go index 6a2649083..fb5bc8833 100644 --- a/hcl2template/types.variables.go +++ b/hcl2template/types.variables.go @@ -72,9 +72,7 @@ func (v *Variable) Value() (cty.Value, *hcl.Diagnostic) { } } - value := cty.NullVal(cty.DynamicPseudoType) - - return value, &hcl.Diagnostic{ + return cty.UnknownVal(v.Type), &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: fmt.Sprintf("Unset variable %q", v.Name), Detail: "A used variable must be set or have a default value; see " + @@ -93,7 +91,6 @@ func (variables Variables) Values() (map[string]cty.Value, hcl.Diagnostics) { value, diag := v.Value() if diag != nil { diags = append(diags, diag) - continue } res[k] = value } @@ -208,12 +205,11 @@ func (variables *Variables) decodeVariableBlock(block *hcl.Block, ectx *hcl.Eval res.DefaultValue = defaultValue - // It's possible no type attribute was assigned so lets make - // sure we have a valid type otherwise there will be issues parsing the value. + // It's possible no type attribute was assigned so lets make sure we + // have a valid type otherwise there could be issues parsing the value. if res.Type == cty.NilType { res.Type = res.DefaultValue.Type() } - } if len(attrs) > 0 { keys := []string{} From 652878059cf3a4b27bae3b6e01ee256c2fc9e6dc Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 27 Aug 2020 11:54:36 +0200 Subject: [PATCH 3/6] inspect command: ignore init errors as some value can be unset --- command/inspect.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/command/inspect.go b/command/inspect.go index d304c7926..1542e19fc 100644 --- a/command/inspect.go +++ b/command/inspect.go @@ -44,11 +44,10 @@ func (c *InspectCommand) RunContext(ctx context.Context, cla *InspectArgs) int { if ret != 0 { return ret } - diags := packerStarter.Initialize() - ret = writeDiags(c.Ui, nil, diags) - if ret != 0 { - return ret - } + + // here we ignore init diags to allow unknown variables to be used + _ = packerStarter.Initialize() + return packerStarter.InspectConfig(packer.InspectConfigOptions{ Ui: c.Ui, }) From 161879b98a62414b4efdb95f7be6368fa63f94f6 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 27 Aug 2020 11:55:57 +0200 Subject: [PATCH 4/6] test unknown variables and locals --- command/inspect_test.go | 27 +++++++++++++++++++ .../test-fixtures/var-arg/fruit_builder.json | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/command/inspect_test.go b/command/inspect_test.go index 2e8f09908..52c00009d 100644 --- a/command/inspect_test.go +++ b/command/inspect_test.go @@ -15,6 +15,33 @@ func Test_commands(t *testing.T) { env []string expected string }{ + {[]string{"inspect", filepath.Join(testFixture("var-arg"), "fruit_builder.pkr.hcl")}, nil, `Packer Inspect: HCL2 mode + +> input-variables: + +var.fruit: "" + +> local-variables: + +local.fruit: "" + +> builds: + + > : + + sources: + + null.builder + + provisioners: + + shell-local + + post-processors: + + + +`}, {[]string{"inspect", "-var=fruit=banana", filepath.Join(testFixture("var-arg"), "fruit_builder.pkr.hcl")}, nil, `Packer Inspect: HCL2 mode > input-variables: diff --git a/command/test-fixtures/var-arg/fruit_builder.json b/command/test-fixtures/var-arg/fruit_builder.json index abda09077..9d6831c8b 100644 --- a/command/test-fixtures/var-arg/fruit_builder.json +++ b/command/test-fixtures/var-arg/fruit_builder.json @@ -1,6 +1,6 @@ { "variables": { - "fruit": "" + "fruit": null }, "builders": [ { From a25f05798465e78353683f74518f13c15506024d Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Tue, 1 Sep 2020 13:07:45 +0200 Subject: [PATCH 5/6] add tests for unknown values --- command/inspect_test.go | 26 ++++++++++++++++++- .../hcl/inspect/fruit_string.pkr.hcl | 7 +++++ command/test-fixtures/inspect/unset_var.json | 5 ++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 command/test-fixtures/inspect/unset_var.json diff --git a/command/inspect_test.go b/command/inspect_test.go index 52c00009d..73d93b1c2 100644 --- a/command/inspect_test.go +++ b/command/inspect_test.go @@ -69,11 +69,13 @@ local.fruit: "banana" `}, - {[]string{"inspect", "-var=fruit=peach", filepath.Join(testFixture("hcl"), "inspect", "fruit_string.pkr.hcl")}, nil, `Packer Inspect: HCL2 mode + {[]string{"inspect", "-var=fruit=peach", "-var=unknown_string=also_peach", `-var=unknown_unknown="peach_too"`, filepath.Join(testFixture("hcl"), "inspect", "fruit_string.pkr.hcl")}, nil, `Packer Inspect: HCL2 mode > input-variables: var.fruit: "peach" +var.unknown_string: "also_peach" +var.unknown_unknown: "peach_too" > local-variables: @@ -86,6 +88,8 @@ var.fruit: "peach" > input-variables: var.fruit: "peach" +var.unknown_string: "" +var.unknown_unknown: "" > local-variables: @@ -121,6 +125,26 @@ Use it at will. manifest shell-local +`}, + {[]string{"inspect", filepath.Join(testFixture("inspect"), "unset_var.json")}, nil, `Packer Inspect: JSON mode +Required variables: + + something + +Optional variables and their defaults: + + +Builders: + + + +Provisioners: + + + +Note: If your build names contain user variables or template +functions such as 'timestamp', these are processed at build time, +and therefore only show in their raw form here. `}, } diff --git a/command/test-fixtures/hcl/inspect/fruit_string.pkr.hcl b/command/test-fixtures/hcl/inspect/fruit_string.pkr.hcl index 3140d12e0..fb285b422 100644 --- a/command/test-fixtures/hcl/inspect/fruit_string.pkr.hcl +++ b/command/test-fixtures/hcl/inspect/fruit_string.pkr.hcl @@ -3,3 +3,10 @@ variable "fruit" { type = string default = "banana" } + +variable "unknown_string" { + type = string +} + +variable "unknown_unknown" { +} diff --git a/command/test-fixtures/inspect/unset_var.json b/command/test-fixtures/inspect/unset_var.json new file mode 100644 index 000000000..f2d2471a5 --- /dev/null +++ b/command/test-fixtures/inspect/unset_var.json @@ -0,0 +1,5 @@ +{ + "variables": { + "something": null + } +} From bdf198594ea6079efede5d4fc0f1b5f3c4a75241 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Tue, 1 Sep 2020 15:27:01 +0200 Subject: [PATCH 6/6] hcl2 inspect: sort variables to have a consistent output --- hcl2template/types.packer_config.go | 11 +++++++++-- hcl2template/types.variables.go | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index 2685809d5..c7f0d0a7f 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -2,6 +2,7 @@ package hcl2template import ( "fmt" + "sort" "strings" "github.com/gobwas/glob" @@ -460,12 +461,18 @@ func (p *PackerConfig) EvaluateExpression(line string) (out string, exit bool, d func (p *PackerConfig) printVariables() string { out := &strings.Builder{} out.WriteString("> input-variables:\n\n") - for _, v := range p.InputVariables { + keys := p.InputVariables.Keys() + sort.Strings(keys) + for _, key := range keys { + v := p.InputVariables[key] val, _ := v.Value() fmt.Fprintf(out, "var.%s: %q\n", v.Name, PrintableCtyValue(val)) } out.WriteString("\n> local-variables:\n\n") - for _, v := range p.LocalVariables { + keys = p.LocalVariables.Keys() + sort.Strings(keys) + for _, key := range keys { + v := p.LocalVariables[key] val, _ := v.Value() fmt.Fprintf(out, "local.%s: %q\n", v.Name, PrintableCtyValue(val)) } diff --git a/hcl2template/types.variables.go b/hcl2template/types.variables.go index fb5bc8833..11f8ba7dc 100644 --- a/hcl2template/types.variables.go +++ b/hcl2template/types.variables.go @@ -84,6 +84,14 @@ func (v *Variable) Value() (cty.Value, *hcl.Diagnostic) { type Variables map[string]*Variable +func (variables Variables) Keys() []string { + keys := make([]string, 0, len(variables)) + for key := range variables { + keys = append(keys, key) + } + return keys +} + func (variables Variables) Values() (map[string]cty.Value, hcl.Diagnostics) { res := map[string]cty.Value{} var diags hcl.Diagnostics