-
Notifications
You must be signed in to change notification settings - Fork 9
/
viper.go
63 lines (48 loc) · 931 Bytes
/
viper.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
51
52
53
54
55
56
57
58
59
60
61
62
63
package config
import (
"os"
"path/filepath"
"github.com/spf13/viper"
)
type Viper struct {
*viper.Viper
path string
}
var defaultConf *Viper
func ReadConfig(file, typ string, data interface{}) (err error) {
return defaultConf.ReadConfig(file, typ, data)
}
func Path() string {
return defaultConf.Path()
}
func Dir() (string, error) {
return filepath.Abs(Path())
}
func Config() *Viper {
return defaultConf
}
func Init(path string) {
defaultConf = New(path)
}
func New(path string) *Viper {
if _, err := os.Stat(path); err != nil {
panic(err)
}
config := viper.New()
config.AddConfigPath(path)
return &Viper{
Viper: config,
path: path,
}
}
func (v *Viper) ReadConfig(file, typ string, data interface{}) (err error) {
v.SetConfigName(file)
v.SetConfigType(typ)
if err = v.ReadInConfig(); err != nil {
return
}
return v.Unmarshal(&data)
}
func (v *Viper) Path() string {
return v.path
}