简单地读取不同环境的配置, 目前仅限于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.

51 line
938 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. if len(chunks) == 1 {
  25. chunks = append(chunks, "")
  26. }
  27. target := recursiveParse(chunks[1], conf.tmp)
  28. if target == nil {
  29. err = errors.New("item doesn't exists")
  30. return
  31. }
  32. out, _ := yaml.Marshal(target)
  33. err = yaml.Unmarshal(out, t)
  34. return
  35. }
  36. func parseYAML(buf bytes.Buffer) interface{} {
  37. var v interface{}
  38. if err := yaml.Unmarshal(buf.Bytes(), &v); err != nil {
  39. log.Println(err)
  40. panic(err)
  41. }
  42. return v
  43. }