packer: add ParseTemplateFile since that is useful

This commit is contained in:
Mitchell Hashimoto 2013-08-09 14:43:34 -07:00
parent 88d274537c
commit 643d9033ad
2 changed files with 37 additions and 0 deletions

View File

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

View File

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