util.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/afero"
  18. "github.com/spf13/cast"
  19. jww "github.com/spf13/jwalterweatherman"
  20. )
  21. // ConfigParseError denotes failing to parse configuration file.
  22. type ConfigParseError struct {
  23. err error
  24. }
  25. // Error returns the formatted configuration error.
  26. func (pe ConfigParseError) Error() string {
  27. return fmt.Sprintf("While parsing config: %s", pe.err.Error())
  28. }
  29. // toCaseInsensitiveValue checks if the value is a map;
  30. // if so, create a copy and lower-case the keys recursively.
  31. func toCaseInsensitiveValue(value interface{}) interface{} {
  32. switch v := value.(type) {
  33. case map[interface{}]interface{}:
  34. value = copyAndInsensitiviseMap(cast.ToStringMap(v))
  35. case map[string]interface{}:
  36. value = copyAndInsensitiviseMap(v)
  37. }
  38. return value
  39. }
  40. // copyAndInsensitiviseMap behaves like insensitiviseMap, but creates a copy of
  41. // any map it makes case insensitive.
  42. func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} {
  43. nm := make(map[string]interface{})
  44. for key, val := range m {
  45. lkey := strings.ToLower(key)
  46. switch v := val.(type) {
  47. case map[interface{}]interface{}:
  48. nm[lkey] = copyAndInsensitiviseMap(cast.ToStringMap(v))
  49. case map[string]interface{}:
  50. nm[lkey] = copyAndInsensitiviseMap(v)
  51. default:
  52. nm[lkey] = v
  53. }
  54. }
  55. return nm
  56. }
  57. func insensitiviseMap(m map[string]interface{}) {
  58. for key, val := range m {
  59. switch val.(type) {
  60. case map[interface{}]interface{}:
  61. // nested map: cast and recursively insensitivise
  62. val = cast.ToStringMap(val)
  63. insensitiviseMap(val.(map[string]interface{}))
  64. case map[string]interface{}:
  65. // nested map: recursively insensitivise
  66. insensitiviseMap(val.(map[string]interface{}))
  67. }
  68. lower := strings.ToLower(key)
  69. if key != lower {
  70. // remove old key (not lower-cased)
  71. delete(m, key)
  72. }
  73. // update map
  74. m[lower] = val
  75. }
  76. }
  77. func absPathify(inPath string) string {
  78. jww.INFO.Println("Trying to resolve absolute path to", inPath)
  79. if strings.HasPrefix(inPath, "$HOME") {
  80. inPath = userHomeDir() + inPath[5:]
  81. }
  82. if strings.HasPrefix(inPath, "$") {
  83. end := strings.Index(inPath, string(os.PathSeparator))
  84. inPath = os.Getenv(inPath[1:end]) + inPath[end:]
  85. }
  86. if filepath.IsAbs(inPath) {
  87. return filepath.Clean(inPath)
  88. }
  89. p, err := filepath.Abs(inPath)
  90. if err == nil {
  91. return filepath.Clean(p)
  92. }
  93. jww.ERROR.Println("Couldn't discover absolute path")
  94. jww.ERROR.Println(err)
  95. return ""
  96. }
  97. // Check if file Exists
  98. func exists(fs afero.Fs, path string) (bool, error) {
  99. stat, err := fs.Stat(path)
  100. if err == nil {
  101. return !stat.IsDir(), nil
  102. }
  103. if os.IsNotExist(err) {
  104. return false, nil
  105. }
  106. return false, err
  107. }
  108. func stringInSlice(a string, list []string) bool {
  109. for _, b := range list {
  110. if b == a {
  111. return true
  112. }
  113. }
  114. return false
  115. }
  116. func userHomeDir() string {
  117. if runtime.GOOS == "windows" {
  118. home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
  119. if home == "" {
  120. home = os.Getenv("USERPROFILE")
  121. }
  122. return home
  123. }
  124. return os.Getenv("HOME")
  125. }
  126. func safeMul(a, b uint) uint {
  127. c := a * b
  128. if a > 1 && b > 1 && c/b != a {
  129. return 0
  130. }
  131. return c
  132. }
  133. // parseSizeInBytes converts strings like 1GB or 12 mb into an unsigned integer number of bytes
  134. func parseSizeInBytes(sizeStr string) uint {
  135. sizeStr = strings.TrimSpace(sizeStr)
  136. lastChar := len(sizeStr) - 1
  137. multiplier := uint(1)
  138. if lastChar > 0 {
  139. if sizeStr[lastChar] == 'b' || sizeStr[lastChar] == 'B' {
  140. if lastChar > 1 {
  141. switch unicode.ToLower(rune(sizeStr[lastChar-1])) {
  142. case 'k':
  143. multiplier = 1 << 10
  144. sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
  145. case 'm':
  146. multiplier = 1 << 20
  147. sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
  148. case 'g':
  149. multiplier = 1 << 30
  150. sizeStr = strings.TrimSpace(sizeStr[:lastChar-1])
  151. default:
  152. multiplier = 1
  153. sizeStr = strings.TrimSpace(sizeStr[:lastChar])
  154. }
  155. }
  156. }
  157. }
  158. size := cast.ToInt(sizeStr)
  159. if size < 0 {
  160. size = 0
  161. }
  162. return safeMul(uint(size), multiplier)
  163. }
  164. // deepSearch scans deep maps, following the key indexes listed in the
  165. // sequence "path".
  166. // The last value is expected to be another map, and is returned.
  167. //
  168. // In case intermediate keys do not exist, or map to a non-map value,
  169. // a new map is created and inserted, and the search continues from there:
  170. // the initial map "m" may be modified!
  171. func deepSearch(m map[string]interface{}, path []string) map[string]interface{} {
  172. for _, k := range path {
  173. m2, ok := m[k]
  174. if !ok {
  175. // intermediate key does not exist
  176. // => create it and continue from there
  177. m3 := make(map[string]interface{})
  178. m[k] = m3
  179. m = m3
  180. continue
  181. }
  182. m3, ok := m2.(map[string]interface{})
  183. if !ok {
  184. // intermediate key is a value
  185. // => replace with a new map
  186. m3 = make(map[string]interface{})
  187. m[k] = m3
  188. }
  189. // continue search from here
  190. m = m3
  191. }
  192. return m
  193. }