Commit 7e5c64ca authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

provisioner/salt-masterless: use common lib for config decoding

/cc @rgarcia - Since you opened the pull request, common config
decoding stuff has been pulled out into the common Package. This
simplifies a lot of code.
parent 57e89132
...@@ -5,11 +5,10 @@ package saltmasterless ...@@ -5,11 +5,10 @@ package saltmasterless
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/builder/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
) )
...@@ -17,7 +16,7 @@ var Ui packer.Ui ...@@ -17,7 +16,7 @@ var Ui packer.Ui
const DefaultTempConfigDir = "/tmp/salt" const DefaultTempConfigDir = "/tmp/salt"
type config struct { type Config struct {
// If true, run the salt-bootstrap script // If true, run the salt-bootstrap script
SkipBootstrap bool `mapstructure:"skip_bootstrap"` SkipBootstrap bool `mapstructure:"skip_bootstrap"`
BootstrapArgs string `mapstructure:"bootstrap_args"` BootstrapArgs string `mapstructure:"bootstrap_args"`
...@@ -30,52 +29,29 @@ type config struct { ...@@ -30,52 +29,29 @@ type config struct {
} }
type Provisioner struct { type Provisioner struct {
config config config Config
} }
func (p *Provisioner) Prepare(raws ...interface{}) error { func (p *Provisioner) Prepare(raws ...interface{}) error {
var md mapstructure.Metadata md, err := common.DecodeConfig(&p.config, raws...)
decoderConfig := &mapstructure.DecoderConfig{
Metadata: &md,
Result: &p.config,
}
decoder, err := mapstructure.NewDecoder(decoderConfig)
if err != nil { if err != nil {
return err return err
} }
for _, raw := range raws { if p.config.TempConfigDir == "" {
err := decoder.Decode(raw) p.config.TempConfigDir = DefaultTempConfigDir
if err != nil {
return err
}
} }
// Accumulate any errors // Accumulate any errors
errs := make([]error, 0) errs := common.CheckUnusedConfig(md)
// Unused keys are errors
if len(md.Unused) > 0 {
sort.Strings(md.Unused)
for _, unused := range md.Unused {
if unused != "type" && !strings.HasPrefix(unused, "packer_") {
errs = append(
errs, fmt.Errorf("Unknown configuration key: %s", unused))
}
}
}
if p.config.LocalStateTree == "" { if p.config.LocalStateTree == "" {
errs = append(errs, errors.New("Please specify a local_state_tree")) errs = packer.MultiErrorAppend(errs,
} errors.New("Please specify a local_state_tree"))
if p.config.TempConfigDir == "" {
p.config.TempConfigDir = DefaultTempConfigDir
} }
if len(errs) > 0 { if errs != nil && len(errs.Errors) > 0 {
return &packer.MultiError{errs} return errs
} }
return nil return nil
......
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