diff --git a/command/build/build.go b/command/build/build.go new file mode 100644 index 000000000..7dc9f779e --- /dev/null +++ b/command/build/build.go @@ -0,0 +1,4 @@ +package build + +type config struct { +} diff --git a/command/build/command.go b/command/build/command.go index 6be166898..5a735bfd5 100644 --- a/command/build/command.go +++ b/command/build/command.go @@ -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 { diff --git a/command/build/help.go b/command/build/help.go index 29f4fc3ec..c9fa52a0b 100644 --- a/command/build/help.go +++ b/command/build/help.go @@ -3,6 +3,10 @@ package build const helpText = ` 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. + 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 ` diff --git a/command/build/string_slice_value.go b/command/build/string_slice_value.go new file mode 100644 index 000000000..c68cd5a1b --- /dev/null +++ b/command/build/string_slice_value.go @@ -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 +}