hcl2 inspect: sort variables to have a consistent output

This commit is contained in:
Adrien Delorme 2020-09-01 15:27:01 +02:00
parent a25f057984
commit bdf198594e
2 changed files with 17 additions and 2 deletions

View File

@ -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))
}

View File

@ -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