39 lines
830 B
Go
39 lines
830 B
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/spf13/viper"
|
|
"hub.cybercinch.nz/guisea/gosqldump/internal/app"
|
|
"hub.cybercinch.nz/guisea/gosqldump/internal/filesystem"
|
|
"hub.cybercinch.nz/guisea/gosqldump/internal/where"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
}
|