-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.go
38 lines (30 loc) · 797 Bytes
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package config
import (
"strings"
"github.com/metafates/go-template/app"
"github.com/metafates/go-template/filesystem"
"github.com/metafates/go-template/where"
"github.com/spf13/viper"
)
// ConfigFormat is the format of the config file
// Available options are: json, yaml, toml
const ConfigFormat = "toml"
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
}
}