From d0200b7be80ded7644273d9f6c777aad71ff0593 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 9 Aug 2013 15:12:33 -0700 Subject: [PATCH] command/validate: accept -only/-except and use new common stuff --- command/validate/command.go | 25 ++++++++++++++----------- command/validate/help.go | 4 +++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/command/validate/command.go b/command/validate/command.go index 0cc7f6ee7..d21be8324 100644 --- a/command/validate/command.go +++ b/command/validate/command.go @@ -3,6 +3,7 @@ package validate import ( "flag" "fmt" + cmdcommon "github.com/mitchellh/packer/common/command" "github.com/mitchellh/packer/packer" "io/ioutil" "log" @@ -17,10 +18,12 @@ func (Command) Help() string { func (c Command) Run(env packer.Environment, args []string) int { var cfgSyntaxOnly bool + buildFilters := new(cmdcommon.BuildFilters) cmdFlags := flag.NewFlagSet("validate", flag.ContinueOnError) cmdFlags.Usage = func() { env.Ui().Say(c.Help()) } cmdFlags.BoolVar(&cfgSyntaxOnly, "syntax-only", false, "check syntax only") + cmdcommon.BuildFilterFlags(cmdFlags, buildFilters) if err := cmdFlags.Parse(args); err != nil { return 1 } @@ -31,6 +34,13 @@ func (c Command) Run(env packer.Environment, args []string) int { 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 log.Printf("Reading template: %s", 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 - buildNames := tpl.BuildNames() - 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 { - errs = append(errs, fmt.Errorf("Build '%s': %s", buildName, err)) - continue - } - - builds = append(builds, build) + builds, err := buildFilters.Builds(tpl, components) + if err != nil { + env.Ui().Error(err.Error()) + return 1 } // Check the configuration of all builds diff --git a/command/validate/help.go b/command/validate/help.go index 34848a817..c9c55c4d8 100644 --- a/command/validate/help.go +++ b/command/validate/help.go @@ -12,5 +12,7 @@ Usage: packer validate [options] TEMPLATE 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 `