Files
gosqldump/internal/config/init.go

39 lines
830 B
Go
Raw Normal View History

2024-08-30 11:25:28 +12:00
package config
import (
"strings"
"github.com/spf13/viper"
2024-08-30 12:21:09 +12:00
"hub.cybercinch.nz/guisea/gosqldump/internal/app"
"hub.cybercinch.nz/guisea/gosqldump/internal/filesystem"
"hub.cybercinch.nz/guisea/gosqldump/internal/where"
2024-08-30 11:25:28 +12:00
)
// ConfigFormat is the format of the config file
// Available options are: json, yaml, toml
const ConfigFormat = "yaml"
var EnvKeyReplacer = strings.NewReplacer(".", "_")
func Init() error {
viper.SetConfigName(app.Name)
viper.SetConfigType(ConfigFormat)
viper.SetFs(filesystem.Api())
viper.AddConfigPath(where.Config())
viper.SetTypeByDefaultValue(true)
viper.SetEnvPrefix(app.Name)
viper.SetEnvKeyReplacer(EnvKeyReplacer)
setDefaults()
err := viper.ReadInConfig()
switch err.(type) {
case viper.ConfigFileNotFoundError:
// Use defaults then
return nil
default:
return err
}
}