...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package conf
16
17 import (
18 "os"
19 "os/user"
20
21 "github.com/spf13/viper"
22 )
23
24
25 type Conf struct {
26 *viper.Viper
27 }
28
29
30 func New() *Conf {
31 return &Conf{Viper: viper.New()}
32 }
33
34
35 func Load(file string) (*Conf, error) {
36 if file != "" {
37 return load(file)
38 }
39 c, err := load("")
40 if _, ok := err.(viper.ConfigFileNotFoundError); ok {
41 return c, nil
42 }
43 return c, nil
44 }
45
46 func load(file string) (*Conf, error) {
47 v := viper.New()
48 if file == "" {
49 v.SetConfigName("serve")
50 v.SetConfigType("toml")
51 v.AddConfigPath(".")
52 if u, err := user.Current(); err == nil {
53 if u.HomeDir != "" {
54 v.AddConfigPath(u.HomeDir + string(os.PathSeparator) + "kivik/")
55 }
56 }
57 v.AddConfigPath("/etc/kivik/")
58 } else {
59 v.SetConfigFile(file)
60 }
61 return &Conf{v}, v.ReadInConfig()
62 }
63
View as plain text