Commit d6b0ff6a authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

command/build: Add -except flag

parent 760995db
...@@ -20,10 +20,12 @@ func (Command) Help() string { ...@@ -20,10 +20,12 @@ 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 cfgExcept []string
var cfgOnly []string var cfgOnly []string
cmdFlags := flag.NewFlagSet("build", flag.ContinueOnError) cmdFlags := flag.NewFlagSet("build", flag.ContinueOnError)
cmdFlags.Usage = func() { env.Ui().Say(c.Help()) } cmdFlags.Usage = func() { env.Ui().Say(c.Help()) }
cmdFlags.Var((*stringSliceValue)(&cfgExcept), "except", "build all builds except these")
cmdFlags.Var((*stringSliceValue)(&cfgOnly), "only", "only build the given builds by name") cmdFlags.Var((*stringSliceValue)(&cfgOnly), "only", "only build the given builds by name")
if err := cmdFlags.Parse(args); err != nil { if err := cmdFlags.Parse(args); err != nil {
return 1 return 1
...@@ -35,6 +37,12 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -35,6 +37,12 @@ func (c Command) Run(env packer.Environment, args []string) int {
return 1 return 1
} }
if len(cfgOnly) > 0 && len(cfgExcept) > 0 {
env.Ui().Error("Only one of '-except' or '-only' may be specified.\n")
env.Ui().Error(c.Help())
return 1
}
// Read the file into a byte array so that we can parse the template // Read the file into a byte array so that we can parse the template
log.Printf("Reading template: %s", args[0]) log.Printf("Reading template: %s", args[0])
tplData, err := ioutil.ReadFile(args[0]) tplData, err := ioutil.ReadFile(args[0])
...@@ -62,6 +70,21 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -62,6 +70,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(cfgExcept) > 0 {
found := false
for _, only := range cfgExcept {
if buildName == only {
found = true
break
}
}
if found {
log.Printf("Skipping build '%s' because specified by -except.", buildName)
continue
}
}
if len(cfgOnly) > 0 { if len(cfgOnly) > 0 {
found := false found := false
for _, only := range cfgOnly { for _, only := range cfgOnly {
......
...@@ -8,5 +8,6 @@ Usage: packer build TEMPLATE ...@@ -8,5 +8,6 @@ Usage: packer build TEMPLATE
Options: Options:
-except=foo,bar,baz Build all builds other than these
-only=foo,bar,baz Only build the given builds by name -only=foo,bar,baz Only build the given builds by name
` `
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