common/config: config filter function [GH-521]

Fixes #521
This commit is contained in:
Matthew Hooker 2013-10-11 03:50:08 -07:00
parent 94487871ad
commit 5315b19822
7 changed files with 32 additions and 9 deletions

View File

@ -143,7 +143,7 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return errs
}
log.Printf("Config: %+v", b.config)
log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey)
return nil
}

View File

@ -56,7 +56,7 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return errs
}
log.Printf("Config: %+v", b.config)
log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey)
return nil
}

View File

@ -159,7 +159,7 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return errs
}
log.Printf("Config: %+v", b.config)
log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey)
return nil
}

View File

@ -11,7 +11,6 @@ import (
"github.com/mitchellh/packer/packer"
"log"
"os"
"strings"
"time"
)
@ -165,10 +164,7 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return errs
}
configRepr := fmt.Sprintf("Config: %+v", b.config)
scrubbedConfig := strings.Replace(configRepr, b.config.ClientID, "CLIENT_ID", -1)
scrubbedConfig = strings.Replace(scrubbedConfig, b.config.APIKey, "API_KEY", -1)
log.Println(scrubbedConfig)
common.ScrubConfig(b.config, b.config.ClientID, b.config.APIKey)
return nil
}

View File

@ -51,7 +51,7 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return errs
}
log.Printf("Config: %+v", b.config)
log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey)
return nil
}

View File

@ -12,6 +12,16 @@ import (
"strings"
)
// ScrubConfig is a helper that returns a string representation of
// any struct with the given values stripped out.
func ScrubConfig(target interface{}, values ...string) string {
conf := fmt.Sprintf("Config: %+v", target)
for _, value := range values {
conf = strings.Replace(conf, value, "<Filtered>", -1)
}
return conf
}
// 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) *packer.MultiError {

View File

@ -159,3 +159,20 @@ func TestDownloadableURL_FilePaths(t *testing.T) {
}
}
}
func TestScrubConfig(t *testing.T) {
type Inner struct {
Baz string
}
type Local struct {
Foo string
Bar string
Inner
}
c := Local{"foo", "bar", Inner{"bar"}}
expect := "Config: {Foo:foo Bar:<Filtered> Inner:{Baz:<Filtered>}}"
conf := ScrubConfig(c, c.Bar)
if conf != expect {
t.Fatalf("got %s, expected %s", conf, expect)
}
}