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

This commit is contained in:
Mitchell Hashimoto 2013-08-09 15:12:33 -07:00
parent 089df41aac
commit 8507e8098d
2 changed files with 17 additions and 12 deletions

View File

@ -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 {
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 {
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 {
} }
// 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)) if err != nil {
for _, buildName := range buildNames { env.Ui().Error(err.Error())
log.Printf("Creating build from template for: %s", buildName) return 1
build, err := tpl.Build(buildName, components)
if err != nil {
errs = append(errs, fmt.Errorf("Build '%s': %s", buildName, err))
continue
}
builds = append(builds, build)
} }
// Check the configuration of all builds // Check the configuration of all builds

View File

@ -12,5 +12,7 @@ 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
` `