2021-09-05 17:07:34 +08:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
linuxPath = "/etc/wheat-cache/"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
setDefaultConfValue()
|
|
|
|
err := LoadConf("")
|
|
|
|
switch err.(type) {
|
|
|
|
case nil:
|
|
|
|
case viper.ConfigFileNotFoundError:
|
2021-10-19 16:34:32 +08:00
|
|
|
formatPath := []string{linuxPath}
|
2021-09-05 21:27:08 +08:00
|
|
|
log.Fatalf("the profile could not be read, read path:%v", formatPath)
|
2021-09-05 17:07:34 +08:00
|
|
|
default:
|
2021-09-05 21:27:08 +08:00
|
|
|
log.Fatalf("the resolution of the profile failed, err: %v", err)
|
2021-09-05 17:07:34 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setDefaultConfValue() {
|
|
|
|
// 设置一些默认值
|
2021-09-19 10:37:30 +08:00
|
|
|
viper.SetDefault("version", "base-01")
|
2021-09-05 17:07:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConf(path string) error {
|
|
|
|
if path != "" {
|
|
|
|
viper.AddConfigPath(path)
|
|
|
|
}
|
|
|
|
viper.SetConfigName("wheat-cache")
|
|
|
|
|
|
|
|
// 添加默认读取地址
|
|
|
|
// linux
|
|
|
|
viper.AddConfigPath(linuxPath)
|
|
|
|
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|