gcfg.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/v2/container/gvar"
  11. "github.com/gogf/gf/v2/errors/gcode"
  12. "github.com/gogf/gf/v2/errors/gerror"
  13. "github.com/gogf/gf/v2/internal/command"
  14. "github.com/gogf/gf/v2/internal/intlog"
  15. "github.com/gogf/gf/v2/internal/utils"
  16. "github.com/gogf/gf/v2/os/genv"
  17. )
  18. // Config is the configuration management object.
  19. type Config struct {
  20. adapter Adapter
  21. }
  22. const (
  23. DefaultInstanceName = "config" // DefaultName is the default instance name for instance usage.
  24. DefaultConfigFileName = "config" // DefaultConfigFile is the default configuration file name.
  25. )
  26. // New creates and returns a Config object with default adapter of AdapterFile.
  27. func New() (*Config, error) {
  28. adapterFile, err := NewAdapterFile()
  29. if err != nil {
  30. return nil, err
  31. }
  32. return &Config{
  33. adapter: adapterFile,
  34. }, nil
  35. }
  36. // NewWithAdapter creates and returns a Config object with given adapter.
  37. func NewWithAdapter(adapter Adapter) *Config {
  38. return &Config{
  39. adapter: adapter,
  40. }
  41. }
  42. // Instance returns an instance of Config with default settings.
  43. // The parameter `name` is the name for the instance. But very note that, if the file "name.toml"
  44. // exists in the configuration directory, it then sets it as the default configuration file. The
  45. // toml file type is the default configuration file type.
  46. func Instance(name ...string) *Config {
  47. var instanceName = DefaultInstanceName
  48. if len(name) > 0 && name[0] != "" {
  49. instanceName = name[0]
  50. }
  51. return localInstances.GetOrSetFuncLock(instanceName, func() interface{} {
  52. adapterFile, err := NewAdapterFile()
  53. if err != nil {
  54. intlog.Errorf(context.Background(), `%+v`, err)
  55. return nil
  56. }
  57. if instanceName != DefaultInstanceName {
  58. adapterFile.SetFileName(instanceName)
  59. }
  60. return NewWithAdapter(adapterFile)
  61. }).(*Config)
  62. }
  63. // SetAdapter sets the adapter of current Config object.
  64. func (c *Config) SetAdapter(adapter Adapter) {
  65. c.adapter = adapter
  66. }
  67. // GetAdapter returns the adapter of current Config object.
  68. func (c *Config) GetAdapter() Adapter {
  69. return c.adapter
  70. }
  71. // Available checks and returns the configuration service is available.
  72. // The optional parameter `pattern` specifies certain configuration resource.
  73. //
  74. // It returns true if configuration file is present in default AdapterFile, or else false.
  75. // Note that this function does not return error as it just does simply check for backend configuration service.
  76. func (c *Config) Available(ctx context.Context, resource ...string) (ok bool) {
  77. return c.adapter.Available(ctx, resource...)
  78. }
  79. // Get retrieves and returns value by specified `pattern`.
  80. // It returns all values of current Json object if `pattern` is given empty or string ".".
  81. // It returns nil if no value found by `pattern`.
  82. //
  83. // It returns a default value specified by `def` if value for `pattern` is not found.
  84. func (c *Config) Get(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error) {
  85. var (
  86. err error
  87. value interface{}
  88. )
  89. value, err = c.adapter.Get(ctx, pattern)
  90. if err != nil {
  91. return nil, err
  92. }
  93. if value == nil {
  94. if len(def) > 0 {
  95. return gvar.New(def[0]), nil
  96. }
  97. return nil, nil
  98. }
  99. return gvar.New(value), nil
  100. }
  101. // GetWithEnv returns the configuration value specified by pattern `pattern`.
  102. // If the configuration value does not exist, then it retrieves and returns the environment value specified by `key`.
  103. // It returns the default value `def` if none of them exists.
  104. //
  105. // Fetching Rules: Environment arguments are in uppercase format, eg: GF_PACKAGE_VARIABLE.
  106. func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error) {
  107. value, err := c.Get(ctx, pattern)
  108. if err != nil && gerror.Code(err) != gcode.CodeNotFound {
  109. return nil, err
  110. }
  111. if value == nil {
  112. if v := genv.Get(utils.FormatEnvKey(pattern)); v != nil {
  113. return v, nil
  114. }
  115. if len(def) > 0 {
  116. return gvar.New(def[0]), nil
  117. }
  118. return nil, nil
  119. }
  120. return value, nil
  121. }
  122. // GetWithCmd returns the configuration value specified by pattern `pattern`.
  123. // If the configuration value does not exist, then it retrieves and returns the command line option specified by `key`.
  124. // It returns the default value `def` if none of them exists.
  125. //
  126. // Fetching Rules: Command line arguments are in lowercase format, eg: gf.package.variable.
  127. func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error) {
  128. value, err := c.Get(ctx, pattern)
  129. if err != nil && gerror.Code(err) != gcode.CodeNotFound {
  130. return nil, err
  131. }
  132. if value == nil {
  133. if v := command.GetOpt(utils.FormatCmdKey(pattern)); v != "" {
  134. return gvar.New(v), nil
  135. }
  136. if len(def) > 0 {
  137. return gvar.New(def[0]), nil
  138. }
  139. return nil, nil
  140. }
  141. return value, nil
  142. }
  143. // Data retrieves and returns all configuration data as map type.
  144. func (c *Config) Data(ctx context.Context) (data map[string]interface{}, err error) {
  145. return c.adapter.Data(ctx)
  146. }
  147. // MustGet acts as function Get, but it panics if error occurs.
  148. func (c *Config) MustGet(ctx context.Context, pattern string, def ...interface{}) *gvar.Var {
  149. v, err := c.Get(ctx, pattern, def...)
  150. if err != nil {
  151. panic(err)
  152. }
  153. if v == nil {
  154. return nil
  155. }
  156. return v
  157. }
  158. // MustGetWithEnv acts as function GetWithEnv, but it panics if error occurs.
  159. func (c *Config) MustGetWithEnv(ctx context.Context, pattern string, def ...interface{}) *gvar.Var {
  160. v, err := c.GetWithEnv(ctx, pattern, def...)
  161. if err != nil {
  162. panic(err)
  163. }
  164. return v
  165. }
  166. // MustGetWithCmd acts as function GetWithCmd, but it panics if error occurs.
  167. func (c *Config) MustGetWithCmd(ctx context.Context, pattern string, def ...interface{}) *gvar.Var {
  168. v, err := c.GetWithCmd(ctx, pattern, def...)
  169. if err != nil {
  170. panic(err)
  171. }
  172. return v
  173. }
  174. // MustData acts as function Data, but it panics if error occurs.
  175. func (c *Config) MustData(ctx context.Context) map[string]interface{} {
  176. v, err := c.Data(ctx)
  177. if err != nil {
  178. panic(err)
  179. }
  180. return v
  181. }