From 713791ba9a717605b4b91fa86c7638c6159ac657 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 9 Aug 2013 15:46:59 -0700 Subject: [PATCH] command/build,validate: pass user vars to Prepare --- command/build/command.go | 10 +++++++++- command/build/help.go | 1 + command/validate/command.go | 10 +++++++++- command/validate/help.go | 1 + common/command/template.go | 13 +++++++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/command/build/command.go b/command/build/command.go index dc1ee9da7..b52f906a1 100644 --- a/command/build/command.go +++ b/command/build/command.go @@ -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 diff --git a/command/build/help.go b/command/build/help.go index f94b3fd4f..8cd97c14e 100644 --- a/command/build/help.go +++ b/command/build/help.go @@ -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. ` diff --git a/command/validate/command.go b/command/validate/command.go index ee0788ca2..c95b026d4 100644 --- a/command/validate/command.go +++ b/command/validate/command.go @@ -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)) } diff --git a/command/validate/help.go b/command/validate/help.go index c9c55c4d8..2289fa68c 100644 --- a/command/validate/help.go +++ b/command/validate/help.go @@ -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. ` diff --git a/common/command/template.go b/common/command/template.go index a323523b0..3f7a8b9c1 100644 --- a/common/command/template.go +++ b/common/command/template.go @@ -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) {