-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
51 lines (41 loc) · 852 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
51
package config
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/spf13/viper"
)
var (
Env = "release" //环境 建议dev开发、test测试、release生产
Viper *viper.Viper //配置
BasePath string //根路径
)
// Start 启动
func Start(envKey, path string) {
if Viper != nil {
return
}
var env string
if flagMode := flag.Lookup("e"); flagMode == nil {
input := flag.String("e", "", "环境")
flag.Parse()
env = *input
} else {
env = flagMode.Value.String()
}
if env == "" {
env = os.Getenv(envKey)
}
if env != "" {
Env = env
}
file := path + "/" + Env + ".yaml"
BasePath = filepath.Dir(path)
Viper = viper.New()
Viper.SetConfigFile(file)
if err := Viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("fatal error config file: %s", err))
}
fmt.Println("config start:", file)
}