Skip to content

Commit

Permalink
allow to describe variables
Browse files Browse the repository at this point in the history
  • Loading branch information
azr committed Jan 10, 2020
1 parent ae07b17 commit 841897e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
3 changes: 3 additions & 0 deletions hcl2template/testdata/variables/basic.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ variable "port" {
variable "availability_zone_names" {
type = list(string)
default = ["us-west-1a"]
description = <<EOD
Describing is awesome ;D
EOD
}

locals {
Expand Down
19 changes: 15 additions & 4 deletions hcl2template/types.variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package hcl2template
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/ext/typeexpr"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/zclconf/go-cty/cty"
)

type Variable struct {
Default cty.Value
Type cty.Type
Default cty.Value
Type cty.Type
Description string

block *hcl.Block
}
Expand Down Expand Up @@ -69,15 +71,24 @@ func (variables *Variables) decodeConfig(block *hcl.Block, ectx *hcl.EvalContext
(*variables) = Variables{}
}

attrs, diags := block.Body.JustAttributes()
var b struct {
Description string `hcl:"description,optional"`
Rest hcl.Body `hcl:",remain"`
}
diags := gohcl.DecodeBody(block.Body, nil, &b)

if diags.HasErrors() {
return diags
}

res := Variable{
block: block,
Description: b.Description,
block: block,
}

attrs, moreDiags := b.Rest.JustAttributes()
diags = append(diags, moreDiags...)

if def, ok := attrs["default"]; ok {
defaultValue, moreDiags := def.Expr.Value(ectx)
diags = append(diags, moreDiags...)
Expand Down
14 changes: 8 additions & 6 deletions hcl2template/types.variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ func TestParse_variables(t *testing.T) {
parseTestArgs{"testdata/variables/basic.pkr.hcl"},
&PackerConfig{
InputVariables: Variables{
"image_name": Variable{},
"key": Variable{},
"my_secret": Variable{},
"image_id": Variable{},
"port": Variable{},
"availability_zone_names": Variable{},
"image_name": Variable{},
"key": Variable{},
"my_secret": Variable{},
"image_id": Variable{},
"port": Variable{},
"availability_zone_names": Variable{
Description: "Describing is awesome ;D\n",
},
},
LocalVariables: Variables{
"owner": Variable{},
Expand Down

0 comments on commit 841897e

Please sign in to comment.