Commit b11004b9 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: ParseTemplateFile understands "-" to mean stdin

parent 588623b0
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)
......
......@@ -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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment