config.go 568 B

12345678910111213141516171819202122232425262728293031
  1. package config
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/spf13/viper"
  6. "os"
  7. )
  8. func NewConfig() *viper.Viper {
  9. envConf := os.Getenv("APP_CONF")
  10. if envConf == "" {
  11. flag.StringVar(&envConf, "conf", "config/local.yml", "config path, eg: -conf config/local.yml")
  12. flag.Parse()
  13. }
  14. if envConf == "" {
  15. envConf = "config/local.yml"
  16. }
  17. fmt.Println("load conf file:", envConf)
  18. return getConfig(envConf)
  19. }
  20. func getConfig(path string) *viper.Viper {
  21. conf := viper.New()
  22. conf.SetConfigFile(path)
  23. err := conf.ReadInConfig()
  24. if err != nil {
  25. panic(err)
  26. }
  27. return conf
  28. }