Commit 87e7f17a authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

command/build: Add -only flag

parent ec224771
package build
type config struct {
}
package build package build
import ( import (
"flag"
"fmt" "fmt"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"io/ioutil" "io/ioutil"
...@@ -18,8 +19,18 @@ func (Command) Help() string { ...@@ -18,8 +19,18 @@ func (Command) Help() string {
} }
func (c Command) Run(env packer.Environment, args []string) int { func (c Command) Run(env packer.Environment, args []string) int {
var cfgOnly []string
cmdFlags := flag.NewFlagSet("build", flag.ContinueOnError)
cmdFlags.Usage = func() { env.Ui().Say(c.Help()) }
cmdFlags.Var((*stringSliceValue)(&cfgOnly), "only", "only build the given builds by name")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
args = cmdFlags.Args()
if len(args) != 1 { if len(args) != 1 {
env.Ui().Say(c.Help()) cmdFlags.Usage()
return 1 return 1
} }
...@@ -50,6 +61,21 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -50,6 +61,21 @@ func (c Command) Run(env packer.Environment, args []string) int {
buildNames := tpl.BuildNames() buildNames := tpl.BuildNames()
builds := make([]packer.Build, 0, len(buildNames)) builds := make([]packer.Build, 0, len(buildNames))
for _, buildName := range buildNames { for _, buildName := range buildNames {
if len(cfgOnly) > 0 {
found := false
for _, only := range cfgOnly {
if buildName == only {
found = true
break
}
}
if !found {
log.Printf("Skipping build '%s' because not specified by -only.", buildName)
continue
}
}
log.Printf("Creating build: %s", buildName) log.Printf("Creating build: %s", buildName)
build, err := tpl.Build(buildName, components) build, err := tpl.Build(buildName, components)
if err != nil { if err != nil {
......
...@@ -3,6 +3,10 @@ package build ...@@ -3,6 +3,10 @@ package build
const helpText = ` const helpText = `
Usage: packer build TEMPLATE Usage: packer build TEMPLATE
Will execute multiple builds in parallel as defined in the template. Will execute multiple builds in parallel as defined in the template.
The various artifacts created by the template will be outputted. The various artifacts created by the template will be outputted.
Options:
-only=foo,bar,baz Only build the given builds by name
` `
package build
import "strings"
type stringSliceValue []string
func (s *stringSliceValue) String() string {
return strings.Join(*s, ",")
}
func (s *stringSliceValue) Set(value string) error {
*s = strings.Split(value, ",")
return nil
}
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