-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
45 lines (35 loc) · 1016 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
package app
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const configFlagName = "config"
var cfgFile string
func init() {
pflag.StringVarP(&cfgFile, "config", "c", cfgFile, "从指定的文件读取配置,"+
"支持 JSON、TOML、YAML、HCL 或 Java properties 格式。")
}
// addConfigFlag 添加配置文件
func addConfigFlag(basename string, fs *pflag.FlagSet) {
fs.AddFlag(pflag.Lookup(configFlagName))
viper.AutomaticEnv()
viper.SetEnvPrefix(strings.Replace(strings.ToUpper(basename), "-", "_", -1))
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
cobra.OnInitialize(func() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
viper.AddConfigPath(".")
viper.AddConfigPath("./configs")
viper.SetConfigName(basename)
}
if err := viper.ReadInConfig(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: 读取配置文件失败(%s): %v\n", cfgFile, err)
os.Exit(1)
}
})
}