2021-01-07 01:17:03 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
2019-05-08 22:04:36 +08:00
|
|
|
// Package gcfg provides reading, caching and managing for configuration.
|
2017-12-30 23:49:55 +08:00
|
|
|
package gcfg
|
2017-12-13 16:45:00 +08:00
|
|
|
|
|
|
|
import (
|
2019-04-03 09:59:15 +08:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-12-04 23:44:54 +08:00
|
|
|
"github.com/gogf/gf/os/gcmd"
|
2020-02-14 21:57:35 +08:00
|
|
|
"github.com/gogf/gf/text/gstr"
|
2019-06-21 22:23:07 +08:00
|
|
|
|
2019-08-19 21:02:44 +08:00
|
|
|
"github.com/gogf/gf/os/gres"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/container/garray"
|
|
|
|
"github.com/gogf/gf/container/gmap"
|
|
|
|
"github.com/gogf/gf/encoding/gjson"
|
|
|
|
"github.com/gogf/gf/os/gfile"
|
|
|
|
"github.com/gogf/gf/os/gfsnotify"
|
|
|
|
"github.com/gogf/gf/os/glog"
|
|
|
|
"github.com/gogf/gf/os/gspath"
|
2017-12-13 16:45:00 +08:00
|
|
|
)
|
|
|
|
|
2017-12-14 17:32:51 +08:00
|
|
|
const (
|
2020-12-14 13:02:08 +08:00
|
|
|
DefaultConfigFile = "config.toml" // The default configuration file name.
|
|
|
|
cmdEnvKey = "gf.gcfg" // Configuration key for command argument or environment.
|
2017-12-14 17:32:51 +08:00
|
|
|
)
|
2017-12-13 16:45:00 +08:00
|
|
|
|
2019-04-02 14:37:46 +08:00
|
|
|
// Configuration struct.
|
2017-12-14 17:32:51 +08:00
|
|
|
type Config struct {
|
2020-03-15 19:56:07 +08:00
|
|
|
name string // Default configuration file name.
|
2019-09-05 11:38:36 +08:00
|
|
|
paths *garray.StrArray // Searching path array.
|
|
|
|
jsons *gmap.StrAnyMap // The pared JSON objects for configuration files.
|
2020-03-15 19:56:07 +08:00
|
|
|
vc bool // Whether do violence check in value index searching. It affects the performance when set true(false in default).
|
2017-12-13 16:45:00 +08:00
|
|
|
}
|
|
|
|
|
2019-09-03 23:18:54 +08:00
|
|
|
var (
|
2020-12-14 13:02:08 +08:00
|
|
|
supportedFileTypes = []string{"toml", "yaml", "json", "ini", "xml"}
|
|
|
|
resourceTryFiles = []string{"", "/", "config/", "config", "/config", "/config/"}
|
2019-09-03 23:18:54 +08:00
|
|
|
)
|
|
|
|
|
2019-03-06 15:21:00 +08:00
|
|
|
// New returns a new configuration management object.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <file> specifies the default configuration file name for reading.
|
2019-06-19 09:06:52 +08:00
|
|
|
func New(file ...string) *Config {
|
2020-12-14 13:02:08 +08:00
|
|
|
name := DefaultConfigFile
|
2019-06-19 09:06:52 +08:00
|
|
|
if len(file) > 0 {
|
|
|
|
name = file[0]
|
2021-01-07 01:17:03 +08:00
|
|
|
} else {
|
|
|
|
// Custom default configuration file name from command line or environment.
|
|
|
|
if customFile := gcmd.GetWithEnv(fmt.Sprintf("%s.file", cmdEnvKey)).String(); customFile != "" {
|
|
|
|
name = customFile
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
c := &Config{
|
2020-03-15 19:56:07 +08:00
|
|
|
name: name,
|
2019-09-05 11:38:36 +08:00
|
|
|
paths: garray.NewStrArray(true),
|
2019-07-23 23:20:27 +08:00
|
|
|
jsons: gmap.NewStrAnyMap(true),
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-04-03 23:39:31 +08:00
|
|
|
// Customized dir path from env/cmd.
|
2021-01-07 01:17:03 +08:00
|
|
|
if customPath := gcmd.GetWithEnv(fmt.Sprintf("%s.path", cmdEnvKey)).String(); customPath != "" {
|
|
|
|
if gfile.Exists(customPath) {
|
|
|
|
_ = c.SetPath(customPath)
|
2019-04-03 23:39:31 +08:00
|
|
|
} else {
|
2019-06-13 20:29:40 +08:00
|
|
|
if errorPrint() {
|
2021-01-07 01:17:03 +08:00
|
|
|
glog.Errorf("Configuration directory path does not exist: %s", customPath)
|
2019-06-13 20:29:40 +08:00
|
|
|
}
|
2019-04-03 23:39:31 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Dir path of working dir.
|
2019-06-05 21:58:27 +08:00
|
|
|
_ = c.SetPath(gfile.Pwd())
|
2019-04-03 23:39:31 +08:00
|
|
|
// Dir path of binary.
|
|
|
|
if selfPath := gfile.SelfDir(); selfPath != "" && gfile.Exists(selfPath) {
|
2019-06-05 21:58:27 +08:00
|
|
|
_ = c.AddPath(selfPath)
|
2019-04-03 23:39:31 +08:00
|
|
|
}
|
|
|
|
// Dir path of main package.
|
|
|
|
if mainPath := gfile.MainPkgPath(); mainPath != "" && gfile.Exists(mainPath) {
|
2019-06-05 21:58:27 +08:00
|
|
|
_ = c.AddPath(mainPath)
|
2019-04-03 23:39:31 +08:00
|
|
|
}
|
2019-04-03 09:59:15 +08:00
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
return c
|
2017-12-13 16:45:00 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 15:21:00 +08:00
|
|
|
// filePath returns the absolute configuration file path for the given filename by <file>.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (c *Config) filePath(file ...string) (path string) {
|
2020-03-15 19:56:07 +08:00
|
|
|
name := c.name
|
2019-06-19 09:06:52 +08:00
|
|
|
if len(file) > 0 {
|
|
|
|
name = file[0]
|
|
|
|
}
|
|
|
|
path = c.FilePath(name)
|
|
|
|
if path == "" {
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
|
|
|
if c.paths.Len() > 0 {
|
|
|
|
buffer.WriteString(fmt.Sprintf("[gcfg] cannot find config file \"%s\" in following paths:", name))
|
|
|
|
c.paths.RLockFunc(func(array []string) {
|
|
|
|
index := 1
|
|
|
|
for _, v := range array {
|
2020-02-14 21:57:35 +08:00
|
|
|
v = gstr.TrimRight(v, `\/`)
|
2019-06-19 09:06:52 +08:00
|
|
|
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, v))
|
|
|
|
index++
|
|
|
|
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, v+gfile.Separator+"config"))
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
buffer.WriteString(fmt.Sprintf("[gcfg] cannot find config file \"%s\" with no path set/add", name))
|
|
|
|
}
|
|
|
|
if errorPrint() {
|
|
|
|
glog.Error(buffer.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path
|
2018-01-04 11:08:23 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 00:23:59 +08:00
|
|
|
// SetPath sets the configuration directory path for file search.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <path> can be absolute or relative path,
|
2019-05-09 17:14:24 +08:00
|
|
|
// but absolute path is strongly recommended.
|
2018-05-03 13:35:08 +08:00
|
|
|
func (c *Config) SetPath(path string) error {
|
2020-08-12 20:38:48 +08:00
|
|
|
var (
|
|
|
|
isDir = false
|
|
|
|
realPath = ""
|
|
|
|
)
|
2019-08-19 21:02:44 +08:00
|
|
|
if file := gres.Get(path); file != nil {
|
|
|
|
realPath = path
|
|
|
|
isDir = file.FileInfo().IsDir()
|
|
|
|
} else {
|
|
|
|
// Absolute path.
|
|
|
|
realPath = gfile.RealPath(path)
|
|
|
|
if realPath == "" {
|
|
|
|
// Relative path.
|
|
|
|
c.paths.RLockFunc(func(array []string) {
|
|
|
|
for _, v := range array {
|
|
|
|
if path, _ := gspath.Search(v, path); path != "" {
|
|
|
|
realPath = path
|
|
|
|
break
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-08-19 21:02:44 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if realPath != "" {
|
|
|
|
isDir = gfile.IsDir(realPath)
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
// Path not exist.
|
|
|
|
if realPath == "" {
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
|
|
|
if c.paths.Len() > 0 {
|
|
|
|
buffer.WriteString(fmt.Sprintf("[gcfg] SetPath failed: cannot find directory \"%s\" in following paths:", path))
|
|
|
|
c.paths.RLockFunc(func(array []string) {
|
|
|
|
for k, v := range array {
|
|
|
|
buffer.WriteString(fmt.Sprintf("\n%d. %s", k+1, v))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
buffer.WriteString(fmt.Sprintf(`[gcfg] SetPath failed: path "%s" does not exist`, path))
|
|
|
|
}
|
|
|
|
err := errors.New(buffer.String())
|
|
|
|
if errorPrint() {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Should be a directory.
|
2019-08-19 21:02:44 +08:00
|
|
|
if !isDir {
|
2019-06-21 22:23:07 +08:00
|
|
|
err := fmt.Errorf(`[gcfg] SetPath failed: path "%s" should be directory type`, path)
|
2019-06-19 09:06:52 +08:00
|
|
|
if errorPrint() {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Repeated path check.
|
|
|
|
if c.paths.Search(realPath) != -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.jsons.Clear()
|
|
|
|
c.paths.Clear()
|
|
|
|
c.paths.Append(realPath)
|
|
|
|
return nil
|
2018-04-17 14:47:45 +08:00
|
|
|
}
|
|
|
|
|
2020-05-22 12:04:58 +08:00
|
|
|
// SetViolenceCheck sets whether to perform hierarchical conflict checking.
|
2019-04-05 00:23:59 +08:00
|
|
|
// This feature needs to be enabled when there is a level symbol in the key name.
|
2020-05-22 12:04:58 +08:00
|
|
|
// It is off in default.
|
|
|
|
//
|
|
|
|
// Note that, turning on this feature is quite expensive, and it is not recommended
|
|
|
|
// to allow separators in the key names. It is best to avoid this on the application side.
|
2018-07-01 00:38:35 +08:00
|
|
|
func (c *Config) SetViolenceCheck(check bool) {
|
2020-03-15 19:56:07 +08:00
|
|
|
c.vc = check
|
2019-06-19 09:06:52 +08:00
|
|
|
c.Clear()
|
2018-07-01 00:38:35 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 00:23:59 +08:00
|
|
|
// AddPath adds a absolute or relative path to the search paths.
|
2018-05-03 13:35:08 +08:00
|
|
|
func (c *Config) AddPath(path string) error {
|
2020-05-22 12:04:58 +08:00
|
|
|
var (
|
|
|
|
isDir = false
|
|
|
|
realPath = ""
|
|
|
|
)
|
|
|
|
// It firstly checks the resource manager,
|
|
|
|
// and then checks the filesystem for the path.
|
2019-08-19 21:02:44 +08:00
|
|
|
if file := gres.Get(path); file != nil {
|
|
|
|
realPath = path
|
|
|
|
isDir = file.FileInfo().IsDir()
|
|
|
|
} else {
|
|
|
|
// Absolute path.
|
|
|
|
realPath = gfile.RealPath(path)
|
|
|
|
if realPath == "" {
|
|
|
|
// Relative path.
|
|
|
|
c.paths.RLockFunc(func(array []string) {
|
|
|
|
for _, v := range array {
|
|
|
|
if path, _ := gspath.Search(v, path); path != "" {
|
|
|
|
realPath = path
|
|
|
|
break
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-08-19 21:02:44 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if realPath != "" {
|
|
|
|
isDir = gfile.IsDir(realPath)
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
if realPath == "" {
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
|
|
|
if c.paths.Len() > 0 {
|
|
|
|
buffer.WriteString(fmt.Sprintf("[gcfg] AddPath failed: cannot find directory \"%s\" in following paths:", path))
|
|
|
|
c.paths.RLockFunc(func(array []string) {
|
|
|
|
for k, v := range array {
|
|
|
|
buffer.WriteString(fmt.Sprintf("\n%d. %s", k+1, v))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
buffer.WriteString(fmt.Sprintf(`[gcfg] AddPath failed: path "%s" does not exist`, path))
|
|
|
|
}
|
|
|
|
err := errors.New(buffer.String())
|
|
|
|
if errorPrint() {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-08-19 21:02:44 +08:00
|
|
|
if !isDir {
|
2019-06-21 22:23:07 +08:00
|
|
|
err := fmt.Errorf(`[gcfg] AddPath failed: path "%s" should be directory type`, path)
|
2019-06-19 09:06:52 +08:00
|
|
|
if errorPrint() {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Repeated path check.
|
|
|
|
if c.paths.Search(realPath) != -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.paths.Append(realPath)
|
|
|
|
//glog.Debug("[gcfg] AddPath:", realPath)
|
|
|
|
return nil
|
2017-12-13 16:45:00 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 00:23:59 +08:00
|
|
|
// GetFilePath returns the absolute path of the specified configuration file.
|
|
|
|
// If <file> is not passed, it returns the configuration file path of the default name.
|
|
|
|
// If the specified configuration file does not exist,
|
|
|
|
// an empty string is returned.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (c *Config) FilePath(file ...string) (path string) {
|
2020-03-15 19:56:07 +08:00
|
|
|
name := c.name
|
2019-06-19 09:06:52 +08:00
|
|
|
if len(file) > 0 {
|
|
|
|
name = file[0]
|
|
|
|
}
|
2019-10-31 23:37:33 +08:00
|
|
|
// Searching resource manager.
|
|
|
|
if !gres.IsEmpty() {
|
|
|
|
for _, v := range resourceTryFiles {
|
|
|
|
if file := gres.Get(v + name); file != nil {
|
|
|
|
path = file.Name()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.paths.RLockFunc(func(array []string) {
|
|
|
|
for _, prefix := range array {
|
|
|
|
for _, v := range resourceTryFiles {
|
|
|
|
if file := gres.Get(prefix + v + name); file != nil {
|
|
|
|
path = file.Name()
|
|
|
|
return
|
|
|
|
}
|
2019-08-19 21:02:44 +08:00
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-10-31 23:37:33 +08:00
|
|
|
})
|
|
|
|
}
|
2021-01-08 00:39:52 +08:00
|
|
|
// Already found?
|
|
|
|
if path != "" {
|
|
|
|
return
|
|
|
|
}
|
2019-10-31 23:37:33 +08:00
|
|
|
// Searching the file system.
|
|
|
|
c.paths.RLockFunc(func(array []string) {
|
|
|
|
for _, prefix := range array {
|
2020-02-14 21:57:35 +08:00
|
|
|
prefix = gstr.TrimRight(prefix, `\/`)
|
2019-08-19 21:02:44 +08:00
|
|
|
if path, _ = gspath.Search(prefix, name); path != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if path, _ = gspath.Search(prefix+gfile.Separator+"config", name); path != "" {
|
|
|
|
return
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return
|
2018-04-21 11:20:31 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 00:23:59 +08:00
|
|
|
// SetFileName sets the default configuration file name.
|
2019-08-07 16:58:01 +08:00
|
|
|
func (c *Config) SetFileName(name string) *Config {
|
2020-03-15 19:56:07 +08:00
|
|
|
c.name = name
|
2019-08-07 16:58:01 +08:00
|
|
|
return c
|
2018-05-23 18:42:21 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 00:23:59 +08:00
|
|
|
// GetFileName returns the default configuration file name.
|
2019-03-21 18:20:20 +08:00
|
|
|
func (c *Config) GetFileName() string {
|
2020-03-15 19:56:07 +08:00
|
|
|
return c.name
|
2019-03-21 18:20:20 +08:00
|
|
|
}
|
|
|
|
|
2019-11-28 23:19:37 +08:00
|
|
|
// Available checks and returns whether configuration of given <file> is available.
|
|
|
|
func (c *Config) Available(file ...string) bool {
|
|
|
|
var name string
|
|
|
|
if len(file) > 0 && file[0] != "" {
|
|
|
|
name = file[0]
|
|
|
|
} else {
|
2020-03-15 19:56:07 +08:00
|
|
|
name = c.name
|
2019-11-28 23:19:37 +08:00
|
|
|
}
|
|
|
|
if c.FilePath(name) != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if GetContent(name) != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-22 21:03:54 +08:00
|
|
|
// getJson returns a *gjson.Json object for the specified <file> content.
|
2019-11-28 23:19:37 +08:00
|
|
|
// It would print error if file reading fails. It return nil if any error occurs.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (c *Config) getJson(file ...string) *gjson.Json {
|
2019-11-28 23:19:37 +08:00
|
|
|
var name string
|
|
|
|
if len(file) > 0 && file[0] != "" {
|
2019-06-19 09:06:52 +08:00
|
|
|
name = file[0]
|
2019-11-28 23:19:37 +08:00
|
|
|
} else {
|
2020-03-15 19:56:07 +08:00
|
|
|
name = c.name
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
r := c.jsons.GetOrSetFuncLock(name, func() interface{} {
|
2020-07-15 00:07:07 +08:00
|
|
|
var (
|
|
|
|
content = ""
|
|
|
|
filePath = ""
|
|
|
|
)
|
2020-09-21 21:30:58 +08:00
|
|
|
// The configured content can be any kind of data type different from its file type.
|
|
|
|
isFromConfigContent := true
|
2019-06-19 09:06:52 +08:00
|
|
|
if content = GetContent(name); content == "" {
|
2020-09-21 21:30:58 +08:00
|
|
|
isFromConfigContent = false
|
2019-06-19 09:06:52 +08:00
|
|
|
filePath = c.filePath(name)
|
|
|
|
if filePath == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-19 21:02:44 +08:00
|
|
|
if file := gres.Get(filePath); file != nil {
|
|
|
|
content = string(file.Content())
|
|
|
|
} else {
|
|
|
|
content = gfile.GetContents(filePath)
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2020-07-15 00:07:07 +08:00
|
|
|
// Note that the underlying configuration json object operations are concurrent safe.
|
2020-09-21 21:30:58 +08:00
|
|
|
var (
|
|
|
|
j *gjson.Json
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
dataType := gfile.ExtName(name)
|
|
|
|
if gjson.IsValidDataType(dataType) && !isFromConfigContent {
|
|
|
|
j, err = gjson.LoadContentType(dataType, content, true)
|
|
|
|
} else {
|
|
|
|
j, err = gjson.LoadContent(content, true)
|
|
|
|
}
|
|
|
|
if err == nil {
|
2020-03-15 19:56:07 +08:00
|
|
|
j.SetViolenceCheck(c.vc)
|
2019-06-19 09:06:52 +08:00
|
|
|
// Add monitor for this configuration file,
|
|
|
|
// any changes of this file will refresh its cache in Config object.
|
2019-08-19 22:54:37 +08:00
|
|
|
if filePath != "" && !gres.Contains(filePath) {
|
2019-06-21 22:23:07 +08:00
|
|
|
_, err = gfsnotify.Add(filePath, func(event *gfsnotify.Event) {
|
2019-06-19 09:06:52 +08:00
|
|
|
c.jsons.Remove(name)
|
|
|
|
})
|
2019-06-21 22:23:07 +08:00
|
|
|
if err != nil && errorPrint() {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
return j
|
|
|
|
} else {
|
|
|
|
if errorPrint() {
|
|
|
|
if filePath != "" {
|
|
|
|
glog.Criticalf(`[gcfg] Load config file "%s" failed: %s`, filePath, err.Error())
|
|
|
|
} else {
|
|
|
|
glog.Criticalf(`[gcfg] Load configuration failed: %s`, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if r != nil {
|
|
|
|
return r.(*gjson.Json)
|
|
|
|
}
|
|
|
|
return nil
|
2018-10-27 16:05:36 +08:00
|
|
|
}
|