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.AutomaticEnv()
defaults()
err := viper.ReadInConfig()
if err != nil {
if errors.As(err, &viper.ConfigFileNotFoundError{}) {
@ -36,17 +34,45 @@ func Init() error {
if err != nil {
return err
}
return nil
return writeDefaults()
}
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
}
func defaults() {
viper.SetDefault("core.post-upload-limit", 1024*1024*1000)
viper.SetDefault("core.log.level", "info")
viper.SetDefault("protocol.s5.p2p.maxOutgoingPeerFailures", 10)
func writeDefault(key string, value interface{}) bool {
if !viper.IsSet(key) {
viper.SetDefault(key, value)
return true
}
return false
}