util.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright © 2014 Steve Francia <spf@spf13.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. // Viper is a application configuration system.
  6. // It believes that applications can be configured a variety of ways
  7. // via flags, ENVIRONMENT variables, configuration files retrieved
  8. // from the file system, or a remote key/value store.
  9. package viper
  10. import (
  11. "fmt"
  12. "os"
  13. "path/filepath"
  14. "runtime"
  15. "strings"
  16. "unicode"
  17. "github.com/spf13/cast"
  18. )
  19. // ConfigParseError denotes failing to parse configuration file.
  20. type ConfigParseError struct {
  21. err error
  22. }
  23. // Error returns the formatted configuration error.
  24. func (pe ConfigParseError) Error() string {
  25. return fmt.Sprintf("While parsing config: %s", pe.err.Error())
  26. }
  27. // toCaseInsensitiveValue checks if the value is a map;
  28. // if so, create a copy and lower-case the keys recursively.
  29. func toCaseInsensitiveValue(value interface{}) interface{} {
  30. switch v := value.(type) {
  31. case map[interface{}]interface{}:
  32. value = copyAndInsensitiviseMap(cast.ToStringMap(v))
  33. case map[string]interface{}:
  34. value = copyAndInsensitiviseMap(v)
  35. }
  36. return value
  37. }
  38. // copyAndInsensitiviseMap behaves like insensitiviseMap, but creates a copy of
  39. // any map it makes case insensitive.
  40. func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} {
  41. nm := make(map[string]interface{})
  42. for key, val := range m {
  43. lkey := strings.ToLower(key)
  44. switch v := val.(type) {
  45. case map[interface{}]interface{}:
  46. nm[lkey] = copyAndInsensitiviseMap(cast.ToStringMap(v))
  47. case map[string]interface{}:
  48. nm[lkey] = copyAndInsensitiviseMap(v)
  49. default:
  50. nm[lkey] = v
  51. }
  52. }
  53. return nm
  54. }
  55. func insensitiviseVal(val interface{}) interface{} {
  56. switch val.(type) {
  57. case map[interface{}]interface{}:
  58. // nested map: cast and recursively insensitivise
  59. val = cast.ToStringMap(val)
  60. insensitiviseMap(val.(map[string]interface{}))
  61. case map[string]interface{}:
  62. // nested map: recursively insensitivise
  63. insensitiviseMap(val.(map[string]interface{}))
  64. case []interface{}:
  65. // nested array: recursively insensitivise
  66. insensitiveArray(val.([]interface{}))
  67. }
  68. return val
  69. }
  70. func insensitiviseMap(m map[string]interface{}) {
  71. for key, val := range m {
  72. val = insensitiviseVal(val)
  73. lower := strings.ToLower(key)
  74. if key != lower {
  75. // remove old key (not lower-cased)
  76. delete(m, key)
  77. }
  78. // update map
  79. m[lower] = val
  80. }
  81. }
  82. func insensitiveArray(a []interface{}) {
  83. for i, val := range a {
  84. a[i] = insensitiviseVal(val)
  85. }
  86. }
  87. func absPathify(logger Logger, inPath string) string {
  88. logger.Info("trying to resolve absolute path", "path", inPath)
  89. if inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+string(os.PathSeparator)) {
  90. inPath = userHomeDir() + inPath[5:]
  91. }
  92. inPath = os.ExpandEnv(inPath)
  93. if filepath.IsAbs(inPath) {
  94. return filepath.Clean(inPath)
  95. }
  96. p, err := filepath.Abs(inPath)
  97. if err == nil {
  98. return filepath.Clean(p)
  99. }
  100. logger.Error(fmt.Errorf("could not discover absolute path: %w", err).Error())
  101. return ""
  102. }
  103. func stringInSlice(a string, list []string) bool {
  104. for _, b := range list {
  105. if b == a {
  106. return true
  107. }
  108. }
  109. return false
  110. }
  111. func userHomeDir() string {
  112. if runtime.GOOS == "windows" {
  113. home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
  114. if home == "" {
  115. home = os.Getenv("USERPROFILE")
  116. }
  117. return home
  118. }
  119. return os.Getenv("HOME")
  120. }
  121. func safeMul(a, b uint) uint {
  122. c := a * b
  123. if a > 1 && b > 1 && c/b != a {
  124. return 0
  125. }
  126. return c
  127. }
  128. // parseSizeInBytes converts strings like 1GB or 12 mb into an unsigned integer number of bytes
  129. func parseSizeInBytes(sizeStr string) uint {
  130. sizeStr = strings.TrimSpace(sizeStr)
  131. lastChar := len(sizeStr) - 1
  132. multiplier := uint(1)
  133. if lastChar > 0 {
  134. if sizeStr[lastChar] == 'b' || sizeStr[lastChar] == 'B' {
  135. if lastChar > 1 {
  136. switch unicode.ToLower(rune(sizeStr[lastChar-1])) {
  137. case 'k':
  138. multiplier = 1 << 10
  139. sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
  140. case 'm':
  141. multiplier = 1 << 20
  142. sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
  143. case 'g':
  144. multiplier = 1 << 30
  145. sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
  146. default:
  147. multiplier = 1
  148. sizeStr = strings.TrimSpace(sizeStr[:lastChar])
  149. }
  150. }
  151. }
  152. }
  153. size := cast.ToInt(sizeStr)
  154. if size < 0 {
  155. size = 0
  156. }
  157. return safeMul(uint(size), multiplier)
  158. }
  159. // deepSearch scans deep maps, following the key indexes listed in the
  160. // sequence "path".
  161. // The last value is expected to be another map, and is returned.
  162. //
  163. // In case intermediate keys do not exist, or map to a non-map value,
  164. // a new map is created and inserted, and the search continues from there:
  165. // the initial map "m" may be modified!
  166. func deepSearch(m map[string]interface{}, path []string) map[string]interface{} {
  167. for _, k := range path {
  168. m2, ok := m[k]
  169. if !ok {
  170. // intermediate key does not exist
  171. // => create it and continue from there
  172. m3 := make(map[string]interface{})
  173. m[k] = m3
  174. m = m3
  175. continue
  176. }
  177. m3, ok := m2.(map[string]interface{})
  178. if !ok {
  179. // intermediate key is a value
  180. // => replace with a new map
  181. m3 = make(map[string]interface{})
  182. m[k] = m3
  183. }
  184. // continue search from here
  185. m = m3
  186. }
  187. return m
  188. }