-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
50 lines (40 loc) · 867 Bytes
/
config.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
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"path/filepath"
"strings"
"github.com/spf13/viper"
)
const (
configName = "fake.toml"
)
type Config struct {
Fake Fake `toml:"fake" json:"fake"`
}
type Fake struct {
Name string `toml:"name" json:"name"`
To string `toml:"to" json:"to"`
Tps int64 `toml:"tps" json:"tps"`
}
func defaultConfig() *Config {
return &Config{
Fake: Fake{
Name: "Fake",
},
}
}
func UnmarshalConfig(configRoot string) (*Config, error) {
viper.SetConfigFile(filepath.Join(configRoot, configName))
viper.SetConfigType("toml")
viper.AutomaticEnv()
viper.SetEnvPrefix("FAKE")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
if err := viper.ReadInConfig(); err != nil {
return nil, err
}
config := defaultConfig()
if err := viper.Unmarshal(config); err != nil {
return nil, err
}
return config, nil
}