builder/common: CheckUnusedConfig
This commit is contained in:
parent
d1ecd89635
commit
5dbae2efde
|
@ -1,9 +1,35 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CheckUnusedConfig is a helper that makes sure that the there are no
|
||||
// unused configuration keys, properly ignoring keys that don't matter.
|
||||
func CheckUnusedConfig(md *mapstructure.Metadata) error {
|
||||
errs := make([]error, 0)
|
||||
|
||||
if md.Unused != nil && 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 len(errs) > 0 {
|
||||
return &packer.MultiError{errs}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodeConfig is a helper that handles decoding raw configuration using
|
||||
// mapstructure. It returns the metadata and any errors that may happen.
|
||||
// If you need extra configuration for mapstructure, you should configure
|
||||
|
|
|
@ -1,10 +1,28 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCheckUnusedConfig(t *testing.T) {
|
||||
md := &mapstructure.Metadata{
|
||||
Unused: make([]string, 0),
|
||||
}
|
||||
|
||||
err := CheckUnusedConfig(md)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
md.Unused = []string{"foo", "bar"}
|
||||
err = CheckUnusedConfig(md)
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeConfig(t *testing.T) {
|
||||
type Local struct {
|
||||
Foo string
|
||||
|
|
Loading…
Reference in New Issue