load_config.go 577 B

12345678910111213141516171819202122232425
  1. package config
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/mitchellh/mapstructure"
  6. "github.com/spf13/viper"
  7. )
  8. func Initialize(path string, cfgStruct interface{}) error {
  9. dir := fmt.Sprintf("%s/config/%s", os.Getenv("GO_WORK_DIR"), path)
  10. // dir = fmt.Sprintf("D:\\project\\golangNew\\kpt-tmr-group\\config\\%s", path)
  11. viper.SetConfigType("yaml")
  12. viper.SetConfigFile(dir)
  13. if err := viper.ReadInConfig(); err != nil {
  14. return err
  15. }
  16. if err := viper.Unmarshal(&cfgStruct, func(c *mapstructure.DecoderConfig) {
  17. c.TagName = "yaml"
  18. }); err != nil {
  19. return err
  20. }
  21. return nil
  22. }