refactor: add a means of saving the config if any default value is not set

This commit is contained in:
Derrick Hammer 2024-01-15 12:02:15 -05:00
parent 8ab200541e
commit 73ab5b8214
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 33 additions and 7 deletions

View File

@ -26,8 +26,6 @@ func Init() error {
viper.SetEnvPrefix("LUME_WEB_PORTAL") viper.SetEnvPrefix("LUME_WEB_PORTAL")
viper.AutomaticEnv() viper.AutomaticEnv()
defaults()
err := viper.ReadInConfig() err := viper.ReadInConfig()
if err != nil { if err != nil {
if errors.As(err, &viper.ConfigFileNotFoundError{}) { if errors.As(err, &viper.ConfigFileNotFoundError{}) {
@ -36,17 +34,45 @@ func Init() error {
if err != nil { if err != nil {
return err return err
} }
return nil return writeDefaults()
} }
return err return err
} }
return writeDefaults()
}
func writeDefaults() error {
defaults := map[string]interface{}{
"core.post-upload-limit": 1024 * 1024 * 1000,
"core.log.level": "info",
"protocol.s5.p2p.maxOutgoingPeerFailures": 10,
"protocol.s5.p2p.network": "",
}
changes := false
for key, value := range defaults {
if writeDefault(key, value) {
changes = true
}
}
if changes {
err := viper.WriteConfig()
if err != nil {
return err
}
}
return nil return nil
} }
func defaults() { func writeDefault(key string, value interface{}) bool {
viper.SetDefault("core.post-upload-limit", 1024*1024*1000) if !viper.IsSet(key) {
viper.SetDefault("core.log.level", "info") viper.SetDefault(key, value)
return true
}
viper.SetDefault("protocol.s5.p2p.maxOutgoingPeerFailures", 10) return false
} }