Store comments in the Template structure

Signed-off-by: Brendan Devenney <brendan@devenney.io>
This commit is contained in:
Brendan Devenney 2019-02-23 00:09:07 +00:00
parent 4da79a8837
commit d8793e3f85
No known key found for this signature in database
GPG Key ID: 8A043A630C39877E
3 changed files with 27 additions and 0 deletions

View File

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

View File

@ -316,6 +316,9 @@ func TestParse(t *testing.T) {
Type: "something",
},
},
Comments: map[string]string{
"_info": "foo",
},
},
false,
},

View File

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