utils.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. package configor
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "path"
  10. "reflect"
  11. "strings"
  12. "time"
  13. "github.com/BurntSushi/toml"
  14. "gopkg.in/yaml.v2"
  15. )
  16. // UnmatchedTomlKeysError errors are returned by the Load function when
  17. // ErrorOnUnmatchedKeys is set to true and there are unmatched keys in the input
  18. // toml config file. The string returned by Error() contains the names of the
  19. // missing keys.
  20. type UnmatchedTomlKeysError struct {
  21. Keys []toml.Key
  22. }
  23. func (e *UnmatchedTomlKeysError) Error() string {
  24. return fmt.Sprintf("There are keys in the config file that do not match any field in the given struct: %v", e.Keys)
  25. }
  26. func (configor *Configor) getENVPrefix(config interface{}) string {
  27. if configor.Config.ENVPrefix == "" {
  28. if prefix := os.Getenv("CONFIGOR_ENV_PREFIX"); prefix != "" {
  29. return prefix
  30. }
  31. return "Configor"
  32. }
  33. return configor.Config.ENVPrefix
  34. }
  35. func getConfigurationFileWithENVPrefix(file, env string) (string, time.Time, error) {
  36. var (
  37. envFile string
  38. extname = path.Ext(file)
  39. )
  40. if extname == "" {
  41. envFile = fmt.Sprintf("%v.%v", file, env)
  42. } else {
  43. envFile = fmt.Sprintf("%v.%v%v", strings.TrimSuffix(file, extname), env, extname)
  44. }
  45. if fileInfo, err := os.Stat(envFile); err == nil && fileInfo.Mode().IsRegular() {
  46. return envFile, fileInfo.ModTime(), nil
  47. }
  48. return "", time.Now(), fmt.Errorf("failed to find file %v", file)
  49. }
  50. func (configor *Configor) getConfigurationFiles(watchMode bool, files ...string) ([]string, map[string]time.Time, []error) {
  51. var resultKeys []string
  52. var results = map[string]time.Time{}
  53. var resultsErrors []error = make([]error, 0, len(files))
  54. if !watchMode && (configor.Config.Debug || configor.Config.Verbose) {
  55. fmt.Printf("Current environment: '%v'\n", configor.GetEnvironment())
  56. }
  57. for i := len(files) - 1; i >= 0; i-- {
  58. foundFile := false
  59. file := files[i]
  60. // check configuration
  61. if fileInfo, err := os.Stat(file); err == nil && fileInfo.Mode().IsRegular() {
  62. foundFile = true
  63. resultKeys = append(resultKeys, file)
  64. results[file] = fileInfo.ModTime()
  65. }
  66. // check configuration with env
  67. if file, modTime, err := getConfigurationFileWithENVPrefix(file, configor.GetEnvironment()); err == nil {
  68. foundFile = true
  69. resultKeys = append(resultKeys, file)
  70. results[file] = modTime
  71. }
  72. // check example configuration
  73. if !foundFile {
  74. if example, modTime, err := getConfigurationFileWithENVPrefix(file, "example"); err == nil {
  75. if !watchMode && !configor.Silent {
  76. fmt.Printf("Failed to find configuration %v, using example file %v\n", file, example)
  77. }
  78. resultKeys = append(resultKeys, example)
  79. results[example] = modTime
  80. } else if !configor.Silent {
  81. fmt.Printf("Failed to find configuration %v\n", file)
  82. resultsErrors = append(resultsErrors, errors.New(fmt.Sprintf("Failed to find configuration %v\n", file)))
  83. }
  84. }
  85. }
  86. return resultKeys, results, resultsErrors
  87. }
  88. func processFile(config interface{}, file string, errorOnUnmatchedKeys bool) error {
  89. data, err := ioutil.ReadFile(file)
  90. if err != nil {
  91. return err
  92. }
  93. switch {
  94. case strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".yml"):
  95. if errorOnUnmatchedKeys {
  96. return yaml.UnmarshalStrict(data, config)
  97. }
  98. return yaml.Unmarshal(data, config)
  99. case strings.HasSuffix(file, ".toml"):
  100. return unmarshalToml(data, config, errorOnUnmatchedKeys)
  101. case strings.HasSuffix(file, ".json"):
  102. return unmarshalJSON(data, config, errorOnUnmatchedKeys)
  103. default:
  104. if err := unmarshalToml(data, config, errorOnUnmatchedKeys); err == nil {
  105. return nil
  106. } else if errUnmatchedKeys, ok := err.(*UnmatchedTomlKeysError); ok {
  107. return errUnmatchedKeys
  108. }
  109. if err := unmarshalJSON(data, config, errorOnUnmatchedKeys); err == nil {
  110. return nil
  111. } else if strings.Contains(err.Error(), "json: unknown field") {
  112. return err
  113. }
  114. var yamlError error
  115. if errorOnUnmatchedKeys {
  116. yamlError = yaml.UnmarshalStrict(data, config)
  117. } else {
  118. yamlError = yaml.Unmarshal(data, config)
  119. }
  120. if yamlError == nil {
  121. return nil
  122. } else if yErr, ok := yamlError.(*yaml.TypeError); ok {
  123. return yErr
  124. }
  125. return errors.New("failed to decode config")
  126. }
  127. }
  128. // GetStringTomlKeys returns a string array of the names of the keys that are passed in as args
  129. func GetStringTomlKeys(list []toml.Key) []string {
  130. arr := make([]string, len(list))
  131. for index, key := range list {
  132. arr[index] = key.String()
  133. }
  134. return arr
  135. }
  136. func unmarshalToml(data []byte, config interface{}, errorOnUnmatchedKeys bool) error {
  137. metadata, err := toml.Decode(string(data), config)
  138. if err == nil && len(metadata.Undecoded()) > 0 && errorOnUnmatchedKeys {
  139. return &UnmatchedTomlKeysError{Keys: metadata.Undecoded()}
  140. }
  141. return err
  142. }
  143. // unmarshalJSON unmarshals the given data into the config interface.
  144. // If the errorOnUnmatchedKeys boolean is true, an error will be returned if there
  145. // are keys in the data that do not match fields in the config interface.
  146. func unmarshalJSON(data []byte, config interface{}, errorOnUnmatchedKeys bool) error {
  147. reader := strings.NewReader(string(data))
  148. decoder := json.NewDecoder(reader)
  149. if errorOnUnmatchedKeys {
  150. decoder.DisallowUnknownFields()
  151. }
  152. err := decoder.Decode(config)
  153. if err != nil && err != io.EOF {
  154. return err
  155. }
  156. return nil
  157. }
  158. func getPrefixForStruct(prefixes []string, fieldStruct *reflect.StructField) []string {
  159. if fieldStruct.Anonymous && fieldStruct.Tag.Get("anonymous") == "true" {
  160. return prefixes
  161. }
  162. return append(prefixes, fieldStruct.Name)
  163. }
  164. func (configor *Configor) processDefaults(config interface{}) error {
  165. configValue := reflect.Indirect(reflect.ValueOf(config))
  166. if configValue.Kind() != reflect.Struct {
  167. return errors.New("invalid config, should be struct")
  168. }
  169. configType := configValue.Type()
  170. for i := 0; i < configType.NumField(); i++ {
  171. var (
  172. fieldStruct = configType.Field(i)
  173. field = configValue.Field(i)
  174. )
  175. if !field.CanAddr() || !field.CanInterface() {
  176. continue
  177. }
  178. if isBlank := reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type()).Interface()); isBlank {
  179. // Set default configuration if blank
  180. if value := fieldStruct.Tag.Get("default"); value != "" {
  181. if err := yaml.Unmarshal([]byte(value), field.Addr().Interface()); err != nil {
  182. return err
  183. }
  184. }
  185. }
  186. for field.Kind() == reflect.Ptr {
  187. field = field.Elem()
  188. }
  189. switch field.Kind() {
  190. case reflect.Struct:
  191. if err := configor.processDefaults(field.Addr().Interface()); err != nil {
  192. return err
  193. }
  194. case reflect.Slice:
  195. for i := 0; i < field.Len(); i++ {
  196. if reflect.Indirect(field.Index(i)).Kind() == reflect.Struct {
  197. if err := configor.processDefaults(field.Index(i).Addr().Interface()); err != nil {
  198. return err
  199. }
  200. }
  201. }
  202. }
  203. }
  204. return nil
  205. }
  206. func (configor *Configor) processTags(config interface{}, prefixes ...string) error {
  207. configValue := reflect.Indirect(reflect.ValueOf(config))
  208. if configValue.Kind() != reflect.Struct {
  209. return errors.New("invalid config, should be struct")
  210. }
  211. configType := configValue.Type()
  212. for i := 0; i < configType.NumField(); i++ {
  213. var (
  214. envNames []string
  215. fieldStruct = configType.Field(i)
  216. field = configValue.Field(i)
  217. envName = fieldStruct.Tag.Get("env") // read configuration from shell env
  218. )
  219. if !field.CanAddr() || !field.CanInterface() {
  220. continue
  221. }
  222. if envName == "" {
  223. envNames = append(envNames, strings.Join(append(prefixes, fieldStruct.Name), "_")) // Configor_DB_Name
  224. envNames = append(envNames, strings.ToUpper(strings.Join(append(prefixes, fieldStruct.Name), "_"))) // CONFIGOR_DB_NAME
  225. } else {
  226. envNames = []string{envName}
  227. }
  228. if configor.Config.Verbose {
  229. fmt.Printf("Trying to load struct `%v`'s field `%v` from env %v\n", configType.Name(), fieldStruct.Name, strings.Join(envNames, ", "))
  230. }
  231. // Load From Shell ENV
  232. for _, env := range envNames {
  233. if value := os.Getenv(env); value != "" {
  234. if configor.Config.Debug || configor.Config.Verbose {
  235. fmt.Printf("Loading configuration for struct `%v`'s field `%v` from env %v...\n", configType.Name(), fieldStruct.Name, env)
  236. }
  237. switch reflect.Indirect(field).Kind() {
  238. case reflect.Bool:
  239. switch strings.ToLower(value) {
  240. case "", "0", "f", "false":
  241. field.Set(reflect.ValueOf(false))
  242. default:
  243. field.Set(reflect.ValueOf(true))
  244. }
  245. case reflect.String:
  246. field.Set(reflect.ValueOf(value))
  247. default:
  248. if err := yaml.Unmarshal([]byte(value), field.Addr().Interface()); err != nil {
  249. return err
  250. }
  251. }
  252. break
  253. }
  254. }
  255. if isBlank := reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type()).Interface()); isBlank && fieldStruct.Tag.Get("required") == "true" {
  256. // return error if it is required but blank
  257. return errors.New(fieldStruct.Name + " is required, but blank")
  258. }
  259. for field.Kind() == reflect.Ptr {
  260. field = field.Elem()
  261. }
  262. if field.Kind() == reflect.Struct {
  263. if err := configor.processTags(field.Addr().Interface(), getPrefixForStruct(prefixes, &fieldStruct)...); err != nil {
  264. return err
  265. }
  266. }
  267. if field.Kind() == reflect.Slice {
  268. if arrLen := field.Len(); arrLen > 0 {
  269. for i := 0; i < arrLen; i++ {
  270. if reflect.Indirect(field.Index(i)).Kind() == reflect.Struct {
  271. if err := configor.processTags(field.Index(i).Addr().Interface(), append(getPrefixForStruct(prefixes, &fieldStruct), fmt.Sprint(i))...); err != nil {
  272. return err
  273. }
  274. }
  275. }
  276. } else {
  277. // load slice from env
  278. newVal := reflect.New(field.Type().Elem()).Elem()
  279. if newVal.Kind() == reflect.Struct {
  280. idx := 0
  281. for {
  282. newVal = reflect.New(field.Type().Elem()).Elem()
  283. if err := configor.processTags(newVal.Addr().Interface(), append(getPrefixForStruct(prefixes, &fieldStruct), fmt.Sprint(idx))...); err != nil {
  284. return err
  285. } else if reflect.DeepEqual(newVal.Interface(), reflect.New(field.Type().Elem()).Elem().Interface()) {
  286. break
  287. } else {
  288. idx++
  289. field.Set(reflect.Append(field, newVal))
  290. }
  291. }
  292. }
  293. }
  294. }
  295. }
  296. return nil
  297. }
  298. func (configor *Configor) load(config interface{}, watchMode bool, files ...string) (err error, changed bool) {
  299. defer func() {
  300. if configor.Config.Debug || configor.Config.Verbose {
  301. if err != nil {
  302. fmt.Printf("Failed to load configuration from %v, got %v\n", files, err)
  303. }
  304. fmt.Printf("Configuration:\n %#v\n", config)
  305. }
  306. }()
  307. configFiles, configModTimeMap, resultsErrors := configor.getConfigurationFiles(watchMode, files...)
  308. if len(resultsErrors) > 0 {
  309. return resultsErrors[0], false
  310. }
  311. if watchMode {
  312. if len(configModTimeMap) == len(configor.configModTimes) {
  313. var changed bool
  314. for f, t := range configModTimeMap {
  315. if v, ok := configor.configModTimes[f]; !ok || t.After(v) {
  316. changed = true
  317. }
  318. }
  319. if !changed {
  320. return nil, false
  321. }
  322. }
  323. }
  324. // process defaults
  325. configor.processDefaults(config)
  326. for _, file := range configFiles {
  327. if configor.Config.Debug || configor.Config.Verbose {
  328. fmt.Printf("Loading configurations from file '%v'...\n", file)
  329. }
  330. if err = processFile(config, file, configor.GetErrorOnUnmatchedKeys()); err != nil {
  331. return err, true
  332. }
  333. }
  334. configor.configModTimes = configModTimeMap
  335. if prefix := configor.getENVPrefix(config); prefix == "-" {
  336. err = configor.processTags(config)
  337. } else {
  338. err = configor.processTags(config, prefix)
  339. }
  340. return err, true
  341. }