package conf import ( "log" "github.com/spf13/viper" ) const ( linuxPath = "/etc/wheat-cache/" devPath = "./conf" devPathBin = "../conf" ) func init() { setDefaultConfValue() err := LoadConf("") switch err.(type) { case nil: case viper.ConfigFileNotFoundError: formatPath := []string{linuxPath, devPath, devPath} log.Fatalf("the profile could not be read, read path:%v", formatPath) default: log.Fatalf("the resolution of the profile failed, err: %v", err) } } func setDefaultConfValue() { // 设置一些默认值 viper.Set("version", "base-01") } func LoadConf(path string) error { if path != "" { viper.AddConfigPath(path) } viper.SetConfigName("wheat-cache") // 添加默认读取地址 // linux viper.AddConfigPath(linuxPath) // 开发环境 viper.AddConfigPath(devPath) viper.AddConfigPath(devPathBin) viper.SetConfigType("yaml") err := viper.ReadInConfig() if err != nil { return err } return nil }