command/build,validate: pass user vars to Prepare

This commit is contained in:
Mitchell Hashimoto 2013-08-09 15:46:59 -07:00
parent bb1b3d8fe0
commit 713791ba9a
5 changed files with 33 additions and 2 deletions

View File

@ -46,6 +46,14 @@ func (c Command) Run(env packer.Environment, args []string) int {
return 1
}
userVars, err := buildOptions.AllUserVars()
if err != nil {
env.Ui().Error(fmt.Sprintf("Error compiling user variables: %s", err))
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])
tpl, err := packer.ParseTemplateFile(args[0])
@ -104,7 +112,7 @@ func (c Command) Run(env packer.Environment, args []string) int {
log.Printf("Preparing build: %s", b.Name())
b.SetDebug(cfgDebug)
b.SetForce(cfgForce)
err := b.Prepare(nil)
err := b.Prepare(userVars)
if err != nil {
env.Ui().Error(err.Error())
return 1

View File

@ -12,4 +12,5 @@ Options:
-force Force a build to continue if artifacts exist, deletes existing artifacts
-except=foo,bar,baz Build all builds other than these
-only=foo,bar,baz Only build the given builds by name
-var 'key=value' Variable for templates, can be used multiple times.
`

View File

@ -40,6 +40,14 @@ func (c Command) Run(env packer.Environment, args []string) int {
return 1
}
userVars, err := buildOptions.AllUserVars()
if err != nil {
env.Ui().Error(fmt.Sprintf("Error compiling user variables: %s", err))
env.Ui().Error("")
env.Ui().Error(c.Help())
return 1
}
// Parse the template into a machine-usable format
log.Printf("Reading template: %s", args[0])
tpl, err := packer.ParseTemplateFile(args[0])
@ -73,7 +81,7 @@ func (c Command) Run(env packer.Environment, args []string) int {
// Check the configuration of all builds
for _, b := range builds {
log.Printf("Preparing build: %s", b.Name())
err := b.Prepare(nil)
err := b.Prepare(userVars)
if err != nil {
errs = append(errs, fmt.Errorf("Errors validating build '%s'. %s", b.Name(), err))
}

View File

@ -15,4 +15,5 @@ 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
-var 'key=value' Variable for templates, can be used multiple times.
`

View File

@ -24,6 +24,19 @@ func (f *BuildOptions) Validate() error {
return nil
}
// AllUserVars returns the user variables, compiled from both the
// file paths and the vars on the command line.
func (f *BuildOptions) AllUserVars() (map[string]string, error) {
all := make(map[string]string)
// Copy in the command-line vars
for k, v := range f.UserVars {
all[k] = v
}
return all, nil
}
// Builds returns the builds out of the given template that pass the
// configured options.
func (f *BuildOptions) Builds(t *packer.Template, cf *packer.ComponentFinder) ([]packer.Build, error) {