33 lines
722 B
Go
33 lines
722 B
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/spf13/viper"
|
|
"sundynix-go/config"
|
|
)
|
|
|
|
func Viper() (cfg *config.Config) {
|
|
viper.SetConfigFile("config.yaml")
|
|
viper.SetConfigType("yaml")
|
|
viper.AddConfigPath("../")
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
// Config file not found; ignore error if desired
|
|
fmt.Println("Config file not found")
|
|
} else {
|
|
// Config file was found but another error was produced
|
|
fmt.Println("Read config file error ")
|
|
}
|
|
}
|
|
|
|
//将所有值或特定值解组到结构、映射中
|
|
//反序列化所有配置项
|
|
if err := viper.Unmarshal(&cfg); err != nil {
|
|
fmt.Printf("unmarshal error %s", err)
|
|
return
|
|
}
|
|
return
|
|
|
|
}
|