From bdf198594ea6079efede5d4fc0f1b5f3c4a75241 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Tue, 1 Sep 2020 15:27:01 +0200 Subject: [PATCH] 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