command/validate: accept -only/-except and use new common stuff
This commit is contained in:
parent
089df41aac
commit
8507e8098d
|
@ -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)
|
||||
builds, err := buildFilters.Builds(tpl, components)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("Build '%s': %s", buildName, err))
|
||||
continue
|
||||
}
|
||||
|
||||
builds = append(builds, build)
|
||||
env.Ui().Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
// Check the configuration of all builds
|
||||
|
|
|
@ -13,4 +13,6 @@ Usage: packer validate [options] TEMPLATE
|
|||
Options:
|
||||
|
||||
-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
|
||||
`
|
||||
|
|
Loading…
Reference in New Issue