load_config.go 565 B

12345678910111213141516171819202122232425262728
  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. workDir := os.Getenv("GO_WORK_DIR_TMR_GROUP")
  10. if workDir == "" {
  11. workDir = "."
  12. }
  13. dir := fmt.Sprintf("%s/config/%s", workDir, path)
  14. viper.SetConfigType("yaml")
  15. viper.SetConfigFile(dir)
  16. if err := viper.ReadInConfig(); err != nil {
  17. return err
  18. }
  19. if err := viper.Unmarshal(&cfgStruct, func(c *mapstructure.DecoderConfig) {
  20. c.TagName = "yaml"
  21. }); err != nil {
  22. return err
  23. }
  24. return nil
  25. }