config.go 773 B

12345678910111213141516171819202122232425262728293031
  1. package main
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/common"
  4. "github.com/spf13/viper"
  5. )
  6. import "errors"
  7. // InitConfigure 初始化配置文件实例
  8. func InitConfigure() (*viper.Viper, error) {
  9. v := viper.New()
  10. v.SetConfigName("config") // 设置文件名称(无后缀)
  11. v.SetConfigType("yaml") // 设置后缀名 {"1.6以后的版本可以不设置该后缀"}
  12. v.AddConfigPath(".") // 设置文件所在路径
  13. err := v.ReadInConfig()
  14. if err != nil {
  15. return nil, errors.New("error reading config:" + err.Error())
  16. }
  17. return v, nil
  18. }
  19. // ReadConfig 读取配置文件
  20. func ReadConfig(viper *viper.Viper) (*common.Config, error) {
  21. conf := &common.Config{}
  22. err := viper.Unmarshal(conf)
  23. if err != nil {
  24. return nil, err
  25. }
  26. return conf, nil
  27. }