2021-09-05 17:07:34 +08:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2021-10-25 15:34:23 +08:00
|
|
|
"sync"
|
2021-09-05 17:07:34 +08:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
linuxPath = "/etc/wheat-cache/"
|
|
|
|
)
|
|
|
|
|
2021-10-25 15:34:23 +08:00
|
|
|
var confLock sync.Once
|
2021-09-05 17:07:34 +08:00
|
|
|
|
2021-10-25 15:34:23 +08:00
|
|
|
func init() {
|
|
|
|
confLock.Do(func() {
|
|
|
|
setDefaultConfValue()
|
|
|
|
err := LoadConf("")
|
|
|
|
switch err.(type) {
|
|
|
|
case nil:
|
|
|
|
case viper.ConfigFileNotFoundError:
|
|
|
|
formatPath := []string{linuxPath}
|
|
|
|
log.Fatalf("the profile could not be read, read path:%v", formatPath)
|
|
|
|
default:
|
|
|
|
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-11-01 23:48:09 +08:00
|
|
|
defaultStorage()
|
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
|
|
|
|
}
|