From 5dbae2efde76b73d84252f6b9ab824583e67f391 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 19 Jul 2013 15:09:13 -0400 Subject: [PATCH] builder/common: CheckUnusedConfig --- builder/common/config.go | 26 ++++++++++++++++++++++++++ builder/common/config_test.go | 18 ++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/builder/common/config.go b/builder/common/config.go index bf52772ba..88351a35e 100644 --- a/builder/common/config.go +++ b/builder/common/config.go @@ -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 diff --git a/builder/common/config_test.go b/builder/common/config_test.go index 4d0a4d981..1f40c9af3 100644 --- a/builder/common/config_test.go +++ b/builder/common/config_test.go @@ -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