Commit ec556044 authored by Rafael Garcia's avatar Rafael Garcia

provisioner/file: state of the art config decoding

parent 25af1c8b
...@@ -3,11 +3,9 @@ package file ...@@ -3,11 +3,9 @@ package file
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"os" "os"
"sort"
"strings"
) )
type config struct { type config struct {
...@@ -23,49 +21,26 @@ type Provisioner struct { ...@@ -23,49 +21,26 @@ type Provisioner struct {
} }
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 {
err := decoder.Decode(raw)
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 _, err := os.Stat(p.config.Source); err != nil { if _, err := os.Stat(p.config.Source); err != nil {
errs = append(errs, errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Bad source '%s': %s", p.config.Source, err)) fmt.Errorf("Bad source '%s': %s", p.config.Source, err))
} }
if p.config.Destination == "" { if p.config.Destination == "" {
errs = append(errs, errors.New("Destination must be specified.")) errs = packer.MultiErrorAppend(errs,
errors.New("Destination must be specified."))
} }
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