简单地读取不同环境的配置, 目前仅限于YAML
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
880 B

  1. package grconfig
  2. import (
  3. "bytes"
  4. "errors"
  5. "gopkg.in/yaml.v2"
  6. "log"
  7. "strings"
  8. )
  9. type ConfigerChannelYAML struct {
  10. tmp interface{}
  11. filename string
  12. adapter string
  13. }
  14. func (conf *ConfigerChannelYAML) String(item string) string {
  15. chunks := strings.SplitN(item, ".", 2)
  16. target := recursiveParse(chunks[1], conf.tmp)
  17. if str, ok := target.(string); ok {
  18. return str
  19. }
  20. return ""
  21. }
  22. func (conf *ConfigerChannelYAML) Item(item string, t interface{}) (err error) {
  23. chunks := strings.SplitN(item, ".", 2)
  24. target := recursiveParse(chunks[1], conf.tmp)
  25. if target == nil {
  26. err = errors.New("item doesn't exists")
  27. return
  28. }
  29. out, _ := yaml.Marshal(target)
  30. err = yaml.Unmarshal(out, t)
  31. return
  32. }
  33. func parseYAML(buf bytes.Buffer) interface{} {
  34. var v interface{}
  35. if err := yaml.Unmarshal(buf.Bytes(), &v); err != nil {
  36. log.Println(err)
  37. panic(err)
  38. }
  39. return v
  40. }