diff --git a/template/parse.go b/template/parse.go index 9f37929a3..722b1fb3c 100644 --- a/template/parse.go +++ b/template/parse.go @@ -24,6 +24,7 @@ type rawTemplate struct { Description string Builders []map[string]interface{} + Comments map[string]string Push map[string]interface{} PostProcessors []interface{} `mapstructure:"post-processors"` Provisioners []map[string]interface{} @@ -44,6 +45,15 @@ func (r *rawTemplate) Template() (*Template, error) { result.MinVersion = r.MinVersion result.RawContents = r.RawContents + // Gather the comments + if len(r.Comments) > 0 { + result.Comments = make(map[string]string, len(r.Comments)) + + for k, v := range r.Comments { + result.Comments[k] = v + } + } + // Gather the variables if len(r.Variables) > 0 { result.Variables = make(map[string]*Variable, len(r.Variables)) @@ -304,9 +314,22 @@ func Parse(r io.Reader) (*Template, error) { // Build an error if there are unused root level keys if len(md.Unused) > 0 { sort.Strings(md.Unused) + + unusedMap, ok := raw.(map[string]interface{}) + if !ok { + return nil, fmt.Errorf("Failed to convert unused root level keys to map") + } + for _, unused := range md.Unused { // Ignore keys starting with '_' as comments if unused[0] == '_' { + if rawTpl.Comments == nil { + rawTpl.Comments = make(map[string]string) + } + rawTpl.Comments[unused], ok = unusedMap[unused].(string) + if !ok { + return nil, fmt.Errorf("Failed to cast root level comment key to string") + } continue } diff --git a/template/parse_test.go b/template/parse_test.go index 8881783f6..5bb13ea4b 100644 --- a/template/parse_test.go +++ b/template/parse_test.go @@ -316,6 +316,9 @@ func TestParse(t *testing.T) { Type: "something", }, }, + Comments: map[string]string{ + "_info": "foo", + }, }, false, }, diff --git a/template/template.go b/template/template.go index 3a0a372aa..635e9c202 100644 --- a/template/template.go +++ b/template/template.go @@ -18,6 +18,7 @@ type Template struct { Description string MinVersion string + Comments map[string]string Variables map[string]*Variable SensitiveVariables []*Variable Builders map[string]*Builder