Commit 8507e809 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

command/validate: accept -only/-except and use new common stuff

parent 089df41a
...@@ -3,6 +3,7 @@ package validate ...@@ -3,6 +3,7 @@ package validate
import ( import (
"flag" "flag"
"fmt" "fmt"
cmdcommon "github.com/mitchellh/packer/common/command"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"io/ioutil" "io/ioutil"
"log" "log"
...@@ -17,10 +18,12 @@ func (Command) Help() string { ...@@ -17,10 +18,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 cfgSyntaxOnly bool var cfgSyntaxOnly bool
buildFilters := new(cmdcommon.BuildFilters)
cmdFlags := flag.NewFlagSet("validate", flag.ContinueOnError) cmdFlags := flag.NewFlagSet("validate", flag.ContinueOnError)
cmdFlags.Usage = func() { env.Ui().Say(c.Help()) } cmdFlags.Usage = func() { env.Ui().Say(c.Help()) }
cmdFlags.BoolVar(&cfgSyntaxOnly, "syntax-only", false, "check syntax only") cmdFlags.BoolVar(&cfgSyntaxOnly, "syntax-only", false, "check syntax only")
cmdcommon.BuildFilterFlags(cmdFlags, buildFilters)
if err := cmdFlags.Parse(args); err != nil { if err := cmdFlags.Parse(args); err != nil {
return 1 return 1
} }
...@@ -31,6 +34,13 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -31,6 +34,13 @@ func (c Command) Run(env packer.Environment, args []string) int {
return 1 return 1
} }
if err := buildFilters.Validate(); err != nil {
env.Ui().Error(err.Error())
env.Ui().Error("")
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])
...@@ -63,17 +73,10 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -63,17 +73,10 @@ func (c Command) Run(env packer.Environment, args []string) int {
} }
// Otherwise, get all the builds // Otherwise, get all the builds
buildNames := tpl.BuildNames() builds, err := buildFilters.Builds(tpl, components)
builds := make([]packer.Build, 0, len(buildNames))
for _, buildName := range buildNames {
log.Printf("Creating build from template for: %s", buildName)
build, err := tpl.Build(buildName, components)
if err != nil { if err != nil {
errs = append(errs, fmt.Errorf("Build '%s': %s", buildName, err)) env.Ui().Error(err.Error())
continue return 1
}
builds = append(builds, build)
} }
// Check the configuration of all builds // Check the configuration of all builds
......
...@@ -13,4 +13,6 @@ Usage: packer validate [options] TEMPLATE ...@@ -13,4 +13,6 @@ Usage: packer validate [options] TEMPLATE
Options: Options:
-syntax-only Only check syntax. Do not verify config of the template. -syntax-only Only check syntax. Do not verify config of the template.
-except=foo,bar,baz Validate all builds other than these
-only=foo,bar,baz Validate only these builds
` `
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