template: store Rawcontents

This commit is contained in:
Mitchell Hashimoto 2015-05-26 09:38:02 -07:00
parent 26c7ac2d90
commit 2fb08be192
3 changed files with 15 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package template package template
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -23,6 +24,8 @@ type rawTemplate struct {
PostProcessors []interface{} `mapstructure:"post-processors"` PostProcessors []interface{} `mapstructure:"post-processors"`
Provisioners []map[string]interface{} Provisioners []map[string]interface{}
Variables map[string]interface{} Variables map[string]interface{}
RawContents []byte
} }
// Template returns the actual Template object built from this raw // Template returns the actual Template object built from this raw
@ -34,6 +37,7 @@ func (r *rawTemplate) Template() (*Template, error) {
// Copy some literals // Copy some literals
result.Description = r.Description result.Description = r.Description
result.MinVersion = r.MinVersion result.MinVersion = r.MinVersion
result.RawContents = r.RawContents
// Gather the variables // Gather the variables
if len(r.Variables) > 0 { 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. // Parse takes the given io.Reader and parses a Template object out of it.
func Parse(r io.Reader) (*Template, error) { 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 // First, decode the object into an interface{}. We do this instead of
// the rawTemplate directly because we'd rather use mapstructure to // the rawTemplate directly because we'd rather use mapstructure to
// decode since it has richer errors. // decode since it has richer errors.
@ -263,6 +271,7 @@ func Parse(r io.Reader) (*Template, error) {
// Create our decoder // Create our decoder
var md mapstructure.Metadata var md mapstructure.Metadata
var rawTpl rawTemplate var rawTpl rawTemplate
rawTpl.RawContents = buf.Bytes()
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Metadata: &md, Metadata: &md,
Result: &rawTpl, Result: &rawTpl,

View File

@ -276,6 +276,9 @@ func TestParse(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if tpl != nil {
tpl.RawContents = nil
}
if !reflect.DeepEqual(tpl, tc.Result) { if !reflect.DeepEqual(tpl, tc.Result) {
t.Fatalf("bad: %s\n\n%#v\n\n%#v", tc.File, tpl, tc.Result) t.Fatalf("bad: %s\n\n%#v\n\n%#v", tc.File, tpl, tc.Result)
} }

View File

@ -19,6 +19,9 @@ type Template struct {
Provisioners []*Provisioner Provisioners []*Provisioner
PostProcessors [][]*PostProcessor PostProcessors [][]*PostProcessor
Push *Push Push *Push
// RawContents is just the raw data for this template
RawContents []byte
} }
// Builder represents a builder configured in the template // Builder represents a builder configured in the template