Repository navigation
Replies: 1 comment 1 reply
|
The write methods serialize the effective configuration, including defaults. In Viper v1.21.0, Your upgrade concern follows directly from that: after a default is written and the file is read again, it is a config-file value, which has higher precedence than a newly changed default. I verified this with a default of 30, a write/reload, and then a new default of 60: the effective value remained 30. To preserve the distinction, keep the persisted configuration separate from the runtime configuration: persisted := viper.New()
persisted.SetConfigFile("config.yaml")
if err := persisted.ReadInConfig(); err != nil {
return err
}
runtimeConfig := viper.New()
runtimeConfig.SetDefault("timeout", 60)
if err := runtimeConfig.MergeConfigMap(persisted.AllSettings()); err != nil {
return err
}
// Persist only an explicit user edit.
persisted.Set("name", "changed")
return persisted.WriteConfig()Use Comparing values against defaults before saving is insufficient: a user may explicitly choose a value that happens to equal today's default. Separating the two configurations preserves that intent. |
Uh oh!
There was an error while loading. Please reload this page.
When the config file is written out, the default values are written out too. Thus, the fact that they represent defaults, and hence can be overridden, is lost. This means that a binary where a default is modified (say, by an upgrade), will no longer get its default although no one ever added the value to the config.
So my question is, why are default values written out? If the config is written, I end up in a situation where this doc from
SetDefaultis no longer true:All reactions