Commit 2fb08be1 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

template: store Rawcontents

parent 26c7ac2d
package template
import (
"bytes"
"encoding/json"
"fmt"
"io"
......@@ -23,6 +24,8 @@ type rawTemplate struct {
PostProcessors []interface{} `mapstructure:"post-processors"`
Provisioners []map[string]interface{}
Variables map[string]interface{}
RawContents []byte
}
// Template returns the actual Template object built from this raw
......@@ -34,6 +37,7 @@ func (r *rawTemplate) Template() (*Template, error) {
// Copy some literals
result.Description = r.Description
result.MinVersion = r.MinVersion
result.RawContents = r.RawContents
// Gather the variables
if len(r.Variables) > 0 {
......@@ -252,6 +256,10 @@ func (r *rawTemplate) parsePostProcessor(
// Parse takes the given io.Reader and parses a Template object out of it.
func Parse(r io.Reader) (*Template, error) {
// Create a buffer to copy what we read
var buf bytes.Buffer
r = io.TeeReader(r, &buf)
// First, decode the object into an interface{}. We do this instead of
// the rawTemplate directly because we'd rather use mapstructure to
// decode since it has richer errors.
......@@ -263,6 +271,7 @@ func Parse(r io.Reader) (*Template, error) {
// Create our decoder
var md mapstructure.Metadata
var rawTpl rawTemplate
rawTpl.RawContents = buf.Bytes()
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Metadata: &md,
Result: &rawTpl,
......
......@@ -276,6 +276,9 @@ func TestParse(t *testing.T) {
t.Fatalf("err: %s", err)
}
if tpl != nil {
tpl.RawContents = nil
}
if !reflect.DeepEqual(tpl, tc.Result) {
t.Fatalf("bad: %s\n\n%#v\n\n%#v", tc.File, tpl, tc.Result)
}
......
......@@ -19,6 +19,9 @@ type Template struct {
Provisioners []*Provisioner
PostProcessors [][]*PostProcessor
Push *Push
// RawContents is just the raw data for this template
RawContents []byte
}
// Builder represents a builder configured in the template
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment