env.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package env
  2. import (
  3. "os"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "github.com/imdario/mergo"
  8. "gogs.baozhida.cn/zoie/OAuth-core/config/source"
  9. )
  10. var (
  11. DefaultPrefixes = []string{}
  12. )
  13. type env struct {
  14. prefixes []string
  15. strippedPrefixes []string
  16. opts source.Options
  17. }
  18. func (e *env) Read() (*source.ChangeSet, error) {
  19. var changes map[string]interface{}
  20. for _, env := range os.Environ() {
  21. if len(e.prefixes) > 0 || len(e.strippedPrefixes) > 0 {
  22. notFound := true
  23. if _, ok := matchPrefix(e.prefixes, env); ok {
  24. notFound = false
  25. }
  26. if match, ok := matchPrefix(e.strippedPrefixes, env); ok {
  27. env = strings.TrimPrefix(env, match)
  28. notFound = false
  29. }
  30. if notFound {
  31. continue
  32. }
  33. }
  34. pair := strings.SplitN(env, "=", 2)
  35. value := pair[1]
  36. keys := strings.Split(strings.ToLower(pair[0]), "_")
  37. reverse(keys)
  38. tmp := make(map[string]interface{})
  39. for i, k := range keys {
  40. if i == 0 {
  41. if intValue, err := strconv.Atoi(value); err == nil {
  42. tmp[k] = intValue
  43. } else if boolValue, err := strconv.ParseBool(value); err == nil {
  44. tmp[k] = boolValue
  45. } else {
  46. tmp[k] = value
  47. }
  48. continue
  49. }
  50. tmp = map[string]interface{}{k: tmp}
  51. }
  52. if err := mergo.Map(&changes, tmp); err != nil {
  53. return nil, err
  54. }
  55. }
  56. b, err := e.opts.Encoder.Encode(changes)
  57. if err != nil {
  58. return nil, err
  59. }
  60. cs := &source.ChangeSet{
  61. Format: e.opts.Encoder.String(),
  62. Data: b,
  63. Timestamp: time.Now(),
  64. Source: e.String(),
  65. }
  66. cs.Checksum = cs.Sum()
  67. return cs, nil
  68. }
  69. func matchPrefix(pre []string, s string) (string, bool) {
  70. for _, p := range pre {
  71. if strings.HasPrefix(s, p) {
  72. return p, true
  73. }
  74. }
  75. return "", false
  76. }
  77. func reverse(ss []string) {
  78. for i := len(ss)/2 - 1; i >= 0; i-- {
  79. opp := len(ss) - 1 - i
  80. ss[i], ss[opp] = ss[opp], ss[i]
  81. }
  82. }
  83. func (e *env) Watch() (source.Watcher, error) {
  84. return newWatcher()
  85. }
  86. func (e *env) Write(cs *source.ChangeSet) error {
  87. return nil
  88. }
  89. func (e *env) String() string {
  90. return "env"
  91. }
  92. // NewSource returns a config source for parsing ENV variables.
  93. // Underscores are delimiters for nesting, and all keys are lowercased.
  94. //
  95. // Example:
  96. //
  97. // "DATABASE_SERVER_HOST=localhost" will convert to
  98. //
  99. // {
  100. // "database": {
  101. // "server": {
  102. // "host": "localhost"
  103. // }
  104. // }
  105. // }
  106. func NewSource(opts ...source.Option) source.Source {
  107. options := source.NewOptions(opts...)
  108. var sp []string
  109. var pre []string
  110. if p, ok := options.Context.Value(strippedPrefixKey{}).([]string); ok {
  111. sp = p
  112. }
  113. if p, ok := options.Context.Value(prefixKey{}).([]string); ok {
  114. pre = p
  115. }
  116. if len(sp) > 0 || len(pre) > 0 {
  117. pre = append(pre, DefaultPrefixes...)
  118. }
  119. return &env{prefixes: pre, strippedPrefixes: sp, opts: options}
  120. }