Fix `packer build` reading from STDIN

Signed-off-by: Jesse Szwedko <jesse.szwedko@getbraintree.com>
This commit is contained in:
Zach Zolton 2015-06-30 17:56:14 +00:00 committed by Jesse Szwedko
parent 8fd673ff9f
commit 548f2ced1c
2 changed files with 37 additions and 1 deletions

View File

@ -37,7 +37,13 @@ func (c BuildCommand) Run(args []string) int {
}
// Parse the template
tpl, err := template.ParseFile(args[0])
var tpl *template.Template
var err error
if args[0] == "-" {
tpl, err = template.Parse(os.Stdin)
} else {
tpl, err = template.ParseFile(args[0])
}
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to parse template: %s", err))
return 1

View File

@ -37,6 +37,36 @@ func TestBuildOnlyFileCommaFlags(t *testing.T) {
}
}
func TestBuildStdin(t *testing.T) {
c := &BuildCommand{
Meta: testMetaFile(t),
}
f, err := os.Open(filepath.Join(testFixture("build-only"), "template.json"))
if err != nil {
t.Fatal(err)
}
defer f.Close()
stdin := os.Stdin
os.Stdin = f
defer func() { os.Stdin = stdin }()
defer cleanup()
if code := c.Run([]string{"-"}); code != 0 {
fatalCommand(t, c.Meta)
}
if !fileExists("chocolate.txt") {
t.Error("Expected to find chocolate.txt")
}
if !fileExists("vanilla.txt") {
t.Error("Expected to find vanilla.txt")
}
if !fileExists("cherry.txt") {
t.Error("Expected to find cherry.txt")
}
}
func TestBuildOnlyFileMultipleFlags(t *testing.T) {
c := &BuildCommand{
Meta: testMetaFile(t),