forked from CatchZeng/dingtalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configs.go
41 lines (35 loc) · 976 Bytes
/
configs.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
package configs
import (
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"log"
"path"
)
// InitConfig reads in configs file and ENV variables if set.
func InitConfig() {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
log.Panic(err)
}
// Search configs in home directory with name ".dingtalk" (without extension).
configPath := path.Join(home, ".dingtalk")
viper.AddConfigPath(configPath)
viper.SetConfigName("config")
viper.AutomaticEnv() // read in environment variables that match
// If a configs file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
log.Printf("failed to read config file: %v", err)
return
}
log.Println("using configs file:", viper.ConfigFileUsed())
}
// GetConfig get configs with key
func GetConfig(key string) (string, error) {
// If a configs file is found, read it in.
err := viper.ReadInConfig()
if err == nil {
return viper.GetString(key), nil
}
return "", err
}