diff --git a/command/build.go b/command/build.go index 7a035a883..6d2fef67c 100644 --- a/command/build.go +++ b/command/build.go @@ -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 diff --git a/command/build_test.go b/command/build_test.go index 73837e1a2..aa35a385f 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -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),