diff --git a/packer/template.go b/packer/template.go index 1e751d5c5..f475feba7 100644 --- a/packer/template.go +++ b/packer/template.go @@ -1,10 +1,13 @@ package packer import ( + "bytes" "fmt" "github.com/mitchellh/mapstructure" jsonutil "github.com/mitchellh/packer/common/json" + "io" "io/ioutil" + "os" "sort" ) @@ -235,9 +238,23 @@ func ParseTemplate(data []byte) (t *Template, err error) { // 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 + var data []byte + + if path == "-" { + // Read from stdin... + buf := new(bytes.Buffer) + _, err := io.Copy(buf, os.Stdin) + if err != nil { + return nil, err + } + + data = buf.Bytes() + } else { + var err error + data, err = ioutil.ReadFile(path) + if err != nil { + return nil, err + } } return ParseTemplate(data) diff --git a/packer/template_test.go b/packer/template_test.go index ad5d95040..c07e24a53 100644 --- a/packer/template_test.go +++ b/packer/template_test.go @@ -3,6 +3,7 @@ package packer import ( "cgl.tideland.biz/asserts" "io/ioutil" + "os" "reflect" "sort" "testing" @@ -43,6 +44,39 @@ func TestParseTemplateFile_basic(t *testing.T) { } } +func TestParseTemplateFile_stdin(t *testing.T) { + data := ` + { + "builders": [{"type": "something"}] + } + ` + + tf, err := ioutil.TempFile("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + defer tf.Close() + tf.Write([]byte(data)) + + // Sync and seek to the beginning so that we can re-read the contents + tf.Sync() + tf.Seek(0, 0) + + // Set stdin to something we control + oldStdin := os.Stdin + defer func() { os.Stdin = oldStdin }() + os.Stdin = tf + + result, err := ParseTemplateFile("-") + 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)