diff --git a/packer/template.go b/packer/template.go index 0bef1e6c3..9d9a08484 100644 --- a/packer/template.go +++ b/packer/template.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "github.com/mitchellh/mapstructure" + "io/ioutil" "sort" ) @@ -255,6 +256,17 @@ func ParseTemplate(data []byte) (t *Template, err error) { return } +// ParseTemplateFile takes the given template file and parses it into +// a single template. +func ParseTemplateFile(path string) (*Template, error) { + data, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + + return ParseTemplate(data) +} + func parsePostProvisioner(i int, rawV interface{}) (result []map[string]interface{}, errors []error) { switch v := rawV.(type) { case string: diff --git a/packer/template_test.go b/packer/template_test.go index 2e9cff079..e23517101 100644 --- a/packer/template_test.go +++ b/packer/template_test.go @@ -2,6 +2,7 @@ package packer import ( "cgl.tideland.biz/asserts" + "io/ioutil" "reflect" "sort" "testing" @@ -18,6 +19,30 @@ func testComponentFinder() *ComponentFinder { } } +func TestParseTemplateFile_basic(t *testing.T) { + data := ` + { + "builders": [{"type": "something"}] + } + ` + + tf, err := ioutil.TempFile("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + tf.Write([]byte(data)) + tf.Close() + + result, err := ParseTemplateFile(tf.Name()) + if err != nil { + t.Fatalf("err: %s", err) + } + + if len(result.Builders) != 1 { + t.Fatalf("bad: %#v", result.Builders) + } +} + func TestParseTemplate_Basic(t *testing.T) { assert := asserts.NewTestingAsserts(t, true)