command/build: Add -only flag

This commit is contained in:
Mitchell Hashimoto 2013-06-04 14:13:02 -07:00
parent ec22477185
commit 87e7f17aa7
4 changed files with 51 additions and 3 deletions

4
command/build/build.go Normal file
View File

@ -0,0 +1,4 @@
package build
type config struct {
}

View File

@ -1,6 +1,7 @@
package build
import (
"flag"
"fmt"
"github.com/mitchellh/packer/packer"
"io/ioutil"
@ -18,8 +19,18 @@ func (Command) Help() string {
}
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 {
env.Ui().Say(c.Help())
cmdFlags.Usage()
return 1
}
@ -50,6 +61,21 @@ func (c Command) Run(env packer.Environment, args []string) int {
buildNames := tpl.BuildNames()
builds := make([]packer.Build, 0, len(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)
build, err := tpl.Build(buildName, components)
if err != nil {

View File

@ -5,4 +5,8 @@ Usage: packer build TEMPLATE
Will execute multiple builds in parallel as defined in the template.
The various artifacts created by the template will be outputted.
Options:
-only=foo,bar,baz Only build the given builds by name
`

View File

@ -0,0 +1,14 @@
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
}