Skip to content

Commit 8c65e16

Browse files
authored
Merge pull request #6 from strukturag/config-updater
Add ConfigUpdater interface
2 parents 31b7f25 + 70a70a9 commit 8c65e16

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

config.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ type Config interface {
2727
GetStringDefault(section, option, dflt string) string
2828
}
2929

30+
// ConfigUpdater provides access to the applications's configuration and allows
31+
// to update it.
32+
//
33+
// Update method takes a string mapping like [section][option]=value. Sections
34+
// are automatically created as needed and existing values are overwritten.
35+
type ConfigUpdater interface {
36+
Config
37+
Update(map[string]map[string]string) error
38+
}
39+
3040
type config struct {
3141
*conf.ConfigFile
3242
path string
@@ -115,6 +125,16 @@ func (config *config) OverrideOption(section, name, value string) {
115125
config.Overrides.AddOption(section, name, value)
116126
}
117127

128+
func (config *config) Update(updates map[string]map[string]string) error {
129+
for section, options := range updates {
130+
for option, value := range options {
131+
config.ConfigFile.AddOption(section, option, value)
132+
}
133+
}
134+
135+
return nil
136+
}
137+
118138
func (config *config) load() (err error) {
119139
if config.HasPath() {
120140
config.ConfigFile, err = conf.ReadConfigFile(config.Path())

container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Metadata interface {
3535
//
3636
// Typically subinterfaces should be used when possible.
3737
type Container interface {
38-
Config
38+
ConfigUpdater
3939
Logger
4040
Metadata
4141
}

0 commit comments

Comments
 (0)