-
Notifications
You must be signed in to change notification settings - Fork 0
/
reg.go
43 lines (38 loc) · 843 Bytes
/
reg.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
package conf
import (
"github.com/spf13/viper"
"log"
"runtime"
"path/filepath"
"flag"
"os"
)
var(
configFile string
flagSet = flag.NewFlagSet("config", flag.ContinueOnError)
)
/**
*@author LanguageY++2013
*2020/2/15 11:03 PM
**/
func init() {
flagSet.StringVar(&configFile, "config", "", "yaml配置文件路径")
flagSet.Parse(os.Args[1:])
if configFile != "" {
viper.SetConfigFile(configFile)
goto readConfig
}
viper.AddConfigPath("./conf")
viper.AddConfigPath(curPathDir())
viper.SetConfigName("main")
viper.SetConfigType("yaml")
readConfig:
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Fatalf("Fatal error config file: %s \n", err)
}
}
func curPathDir() string {
_, file, _, _ := runtime.Caller(1)
return filepath.Dir(file)
}