app: support merging configs

This commit is contained in:
Mitchell Hashimoto 2013-05-08 20:37:07 -07:00
parent 5e17fbdaca
commit cd5cecfe89
2 changed files with 52 additions and 0 deletions

View File

@ -20,6 +20,33 @@ type config struct {
Commands map[string]string
}
// Merge the configurations. Anything in the "new" configuration takes
// precedence over the "old" configuration.
func mergeConfig(a, b *config) *config {
configs := []*config{a, b}
result := newConfig()
for _, config := range configs {
for k, v := range config.Builders {
result.Builders[k] = v
}
for k, v := range config.Commands {
result.Commands[k] = v
}
}
return result
}
// Creates and initializes a new config struct.
func newConfig() *config {
result := new(config)
result.Builders = make(map[string]string)
result.Commands = make(map[string]string)
return result
}
// Parses a configuration file and returns a proper configuration
// struct.
func parseConfig(data string) (result *config, err error) {

View File

@ -5,6 +5,31 @@ import (
"testing"
)
func TestConfig_MergeConfig(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
aString := `
[commands]
a = "1"
b = "1"
`
bString := `
[commands]
a = "1"
b = "2"
c = "3"
`
a, _ := parseConfig(aString)
b, _ := parseConfig(bString)
result := mergeConfig(a, b)
assert.Equal(result.Commands["a"], "1", "a should be 1")
assert.Equal(result.Commands["b"], "2", "a should be 2")
assert.Equal(result.Commands["c"], "3", "a should be 3")
}
func TestConfig_ParseConfig_Bad(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)