Initial commit
This commit is contained in:
38
internal/config/init.go
Normal file
38
internal/config/init.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"hub.cybercinch.nz/guisea/go-template/internal/app"
|
||||
"hub.cybercinch.nz/guisea/go-template/internal/filesystem"
|
||||
"hub.cybercinch.nz/guisea/go-template/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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user