Commit 56c36c12 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

command/build,validate: pass user vars to Prepare

parent 915c8ceb
...@@ -46,6 +46,14 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -46,6 +46,14 @@ func (c Command) Run(env packer.Environment, args []string) int {
return 1 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 // 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])
tpl, err := packer.ParseTemplateFile(args[0]) tpl, err := packer.ParseTemplateFile(args[0])
...@@ -104,7 +112,7 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -104,7 +112,7 @@ func (c Command) Run(env packer.Environment, args []string) int {
log.Printf("Preparing build: %s", b.Name()) log.Printf("Preparing build: %s", b.Name())
b.SetDebug(cfgDebug) b.SetDebug(cfgDebug)
b.SetForce(cfgForce) b.SetForce(cfgForce)
err := b.Prepare(nil) err := b.Prepare(userVars)
if err != nil { if err != nil {
env.Ui().Error(err.Error()) env.Ui().Error(err.Error())
return 1 return 1
......
...@@ -12,4 +12,5 @@ Options: ...@@ -12,4 +12,5 @@ Options:
-force Force a build to continue if artifacts exist, deletes existing artifacts -force Force a build to continue if artifacts exist, deletes existing artifacts
-except=foo,bar,baz Build all builds other than these -except=foo,bar,baz Build all builds other than these
-only=foo,bar,baz Only build the given builds by name -only=foo,bar,baz Only build the given builds by name
-var 'key=value' Variable for templates, can be used multiple times.
` `
...@@ -40,6 +40,14 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -40,6 +40,14 @@ func (c Command) Run(env packer.Environment, args []string) int {
return 1 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 // Parse the template into a machine-usable format
log.Printf("Reading template: %s", args[0]) log.Printf("Reading template: %s", args[0])
tpl, err := packer.ParseTemplateFile(args[0]) tpl, err := packer.ParseTemplateFile(args[0])
...@@ -73,7 +81,7 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -73,7 +81,7 @@ func (c Command) Run(env packer.Environment, args []string) int {
// Check the configuration of all builds // Check the configuration of all builds
for _, b := range builds { for _, b := range builds {
log.Printf("Preparing build: %s", b.Name()) log.Printf("Preparing build: %s", b.Name())
err := b.Prepare(nil) err := b.Prepare(userVars)
if err != nil { if err != nil {
errs = append(errs, fmt.Errorf("Errors validating build '%s'. %s", b.Name(), err)) errs = append(errs, fmt.Errorf("Errors validating build '%s'. %s", b.Name(), err))
} }
......
...@@ -15,4 +15,5 @@ Options: ...@@ -15,4 +15,5 @@ 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 -except=foo,bar,baz Validate all builds other than these
-only=foo,bar,baz Validate only these builds -only=foo,bar,baz Validate only these builds
-var 'key=value' Variable for templates, can be used multiple times.
` `
...@@ -24,6 +24,19 @@ func (f *BuildOptions) Validate() error { ...@@ -24,6 +24,19 @@ func (f *BuildOptions) Validate() error {
return nil 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 // Builds returns the builds out of the given template that pass the
// configured options. // configured options.
func (f *BuildOptions) Builds(t *packer.Template, cf *packer.ComponentFinder) ([]packer.Build, error) { func (f *BuildOptions) Builds(t *packer.Template, cf *packer.ComponentFinder) ([]packer.Build, error) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment