From 2fb08be192fcd3cd73b8c9d55ec213ddd22478f1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 26 May 2015 09:38:02 -0700 Subject: [PATCH] template: store Rawcontents --- template/parse.go | 9 +++++++++ template/parse_test.go | 3 +++ template/template.go | 3 +++ 3 files changed, 15 insertions(+) diff --git a/template/parse.go b/template/parse.go index a46adc594..5566e31cc 100644 --- a/template/parse.go +++ b/template/parse.go @@ -1,6 +1,7 @@ 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, diff --git a/template/parse_test.go b/template/parse_test.go index 2cca68b88..d5d9fcd8e 100644 --- a/template/parse_test.go +++ b/template/parse_test.go @@ -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) } diff --git a/template/template.go b/template/template.go index 52f50089f..1ab3c668e 100644 --- a/template/template.go +++ b/template/template.go @@ -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