Fixed issue with overriding default config values via command line
This commit is contained in:
+28
-6
@@ -225,6 +225,33 @@ func evalConfigValues() {
|
||||
}
|
||||
}
|
||||
|
||||
func loadSpecifedConfigFile(configFile string) {
|
||||
userConfig, err := ini.Load(configFile)
|
||||
if err != nil {
|
||||
log.Fatal(3, "Failed to parse %v, %v", configFile, err)
|
||||
}
|
||||
|
||||
for _, section := range userConfig.Sections() {
|
||||
for _, key := range section.Keys() {
|
||||
if key.Value() == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
defaultSec, err := Cfg.GetSection(section.Name())
|
||||
if err != nil {
|
||||
log.Fatal(3, "Unknown config section %s defined in %s", section.Name(), configFile)
|
||||
}
|
||||
defaultKey, err := defaultSec.GetKey(key.Name())
|
||||
if err != nil {
|
||||
log.Fatal(3, "Unknown config key %s defined in section %s, in file", key.Name(), section.Name(), configFile)
|
||||
}
|
||||
defaultKey.SetValue(key.Value())
|
||||
}
|
||||
}
|
||||
|
||||
configFiles = append(configFiles, configFile)
|
||||
}
|
||||
|
||||
func loadConfiguration(args *CommandLineArgs) {
|
||||
var err error
|
||||
|
||||
@@ -249,12 +276,7 @@ func loadConfiguration(args *CommandLineArgs) {
|
||||
|
||||
// load specified config file
|
||||
if args.Config != "" {
|
||||
err = Cfg.Append(args.Config)
|
||||
if err != nil {
|
||||
log.Fatal(3, "Failed to parse %v, %v", args.Config, err)
|
||||
}
|
||||
configFiles = append(configFiles, args.Config)
|
||||
appliedCommandLineProperties = append(appliedCommandLineProperties, "config="+args.Config)
|
||||
loadSpecifedConfigFile(args.Config)
|
||||
}
|
||||
|
||||
// apply environment overrides
|
||||
|
||||
@@ -49,10 +49,13 @@ func TestLoadingSettings(t *testing.T) {
|
||||
|
||||
Convey("Should be able to override defaults via command line", func() {
|
||||
NewConfigContext(&CommandLineArgs{
|
||||
Args: []string{"cfg:default.paths.data=/tmp/data"},
|
||||
Args: []string{
|
||||
"cfg:default.server.domain=test2",
|
||||
},
|
||||
Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
|
||||
})
|
||||
|
||||
So(DataPath, ShouldEqual, "/tmp/data")
|
||||
So(Domain, ShouldEqual, "test2")
|
||||
})
|
||||
|
||||
Convey("Defaults can be overriden in specified config file", func() {
|
||||
|
||||
Reference in New Issue
Block a user