gcfg.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gcfg provides reading, caching and managing for configuration.
  7. package gcfg
  8. import (
  9. "context"
  10. "github.com/gogf/gf/container/garray"
  11. "github.com/gogf/gf/container/gmap"
  12. "github.com/gogf/gf/internal/intlog"
  13. "github.com/gogf/gf/os/gcmd"
  14. )
  15. // Config is the configuration manager.
  16. type Config struct {
  17. defaultName string // Default configuration file name.
  18. searchPaths *garray.StrArray // Searching path array.
  19. jsonMap *gmap.StrAnyMap // The pared JSON objects for configuration files.
  20. violenceCheck bool // Whether do violence check in value index searching. It affects the performance when set true(false in default).
  21. }
  22. const (
  23. DefaultName = "config" // DefaultName is the default group name for instance usage.
  24. DefaultConfigFile = "config.toml" // DefaultConfigFile is the default configuration file name.
  25. commandEnvKeyForFile = "gf.gcfg.file" // commandEnvKeyForFile is the configuration key for command argument or environment configuring file name.
  26. commandEnvKeyForPath = "gf.gcfg.path" // commandEnvKeyForPath is the configuration key for command argument or environment configuring directory path.
  27. commandEnvKeyForErrorPrint = "gf.gcfg.errorprint" // commandEnvKeyForErrorPrint is used to specify the key controlling error printing to stdout.
  28. )
  29. var (
  30. supportedFileTypes = []string{"toml", "yaml", "yml", "json", "ini", "xml"} // All supported file types suffixes.
  31. resourceTryFiles = []string{"", "/", "config/", "config", "/config", "/config/"} // Prefix array for trying searching in resource manager.
  32. instances = gmap.NewStrAnyMap(true) // Instances map containing configuration instances.
  33. customConfigContentMap = gmap.NewStrStrMap(true) // Customized configuration content.
  34. )
  35. // SetContent sets customized configuration content for specified `file`.
  36. // The `file` is unnecessary param, default is DefaultConfigFile.
  37. func SetContent(content string, file ...string) {
  38. name := DefaultConfigFile
  39. if len(file) > 0 {
  40. name = file[0]
  41. }
  42. // Clear file cache for instances which cached `name`.
  43. instances.LockFunc(func(m map[string]interface{}) {
  44. if customConfigContentMap.Contains(name) {
  45. for _, v := range m {
  46. v.(*Config).jsonMap.Remove(name)
  47. }
  48. }
  49. customConfigContentMap.Set(name, content)
  50. })
  51. }
  52. // GetContent returns customized configuration content for specified `file`.
  53. // The `file` is unnecessary param, default is DefaultConfigFile.
  54. func GetContent(file ...string) string {
  55. name := DefaultConfigFile
  56. if len(file) > 0 {
  57. name = file[0]
  58. }
  59. return customConfigContentMap.Get(name)
  60. }
  61. // RemoveContent removes the global configuration with specified `file`.
  62. // If `name` is not passed, it removes configuration of the default group name.
  63. func RemoveContent(file ...string) {
  64. name := DefaultConfigFile
  65. if len(file) > 0 {
  66. name = file[0]
  67. }
  68. // Clear file cache for instances which cached `name`.
  69. instances.LockFunc(func(m map[string]interface{}) {
  70. if customConfigContentMap.Contains(name) {
  71. for _, v := range m {
  72. v.(*Config).jsonMap.Remove(name)
  73. }
  74. customConfigContentMap.Remove(name)
  75. }
  76. })
  77. intlog.Printf(context.TODO(), `RemoveContent: %s`, name)
  78. }
  79. // ClearContent removes all global configuration contents.
  80. func ClearContent() {
  81. customConfigContentMap.Clear()
  82. // Clear cache for all instances.
  83. instances.LockFunc(func(m map[string]interface{}) {
  84. for _, v := range m {
  85. v.(*Config).jsonMap.Clear()
  86. }
  87. })
  88. intlog.Print(context.TODO(), `RemoveConfig`)
  89. }
  90. // errorPrint checks whether printing error to stdout.
  91. func errorPrint() bool {
  92. return gcmd.GetOptWithEnv(commandEnvKeyForErrorPrint, true).Bool()
  93. }