gf/os/gcfg/gcfg.go

104 lines
3.7 KiB
Go
Raw Normal View History

// 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,
// 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.
package gcfg
import (
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/container/gmap"
2021-05-17 00:07:06 +08:00
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/os/gcmd"
)
2021-05-17 00:07:06 +08:00
// Config is the configuration manager.
type Config struct {
2021-01-27 00:55:45 +08:00
defaultName string // Default configuration file name.
searchPaths *garray.StrArray // Searching path array.
jsonMap *gmap.StrAnyMap // The pared JSON objects for configuration files.
violenceCheck bool // Whether do violence check in value index searching. It affects the performance when set true(false in default).
}
2021-05-17 00:07:06 +08:00
const (
DefaultName = "config" // DefaultName is the default group name for instance usage.
DefaultConfigFile = "config.toml" // DefaultConfigFile is the default configuration file name.
cmdEnvKey = "gf.gcfg" // cmdEnvKey is the configuration key for command argument or environment.
errorPrintKey = "gf.gcfg.errorprint" // errorPrintKey is used to specify the key controlling error printing to stdout.
)
var (
2021-05-17 00:07:06 +08:00
supportedFileTypes = []string{"toml", "yaml", "yml", "json", "ini", "xml"} // All supported file types suffixes.
resourceTryFiles = []string{"", "/", "config/", "config", "/config", "/config/"} // Prefix array for trying searching in resource manager.
instances = gmap.NewStrAnyMap(true) // Instances map containing configuration instances.
customConfigContentMap = gmap.NewStrStrMap(true) // Customized configuration content.
)
2021-05-17 00:07:06 +08:00
// SetContent sets customized configuration content for specified `file`.
// The `file` is unnecessary param, default is DefaultConfigFile.
func SetContent(content string, file ...string) {
name := DefaultConfigFile
2019-06-19 09:06:52 +08:00
if len(file) > 0 {
name = file[0]
}
2021-05-17 00:07:06 +08:00
// Clear file cache for instances which cached `name`.
instances.LockFunc(func(m map[string]interface{}) {
if customConfigContentMap.Contains(name) {
for _, v := range m {
v.(*Config).jsonMap.Remove(name)
}
}
2021-05-17 00:07:06 +08:00
customConfigContentMap.Set(name, content)
})
}
2021-05-17 00:07:06 +08:00
// GetContent returns customized configuration content for specified `file`.
// The `file` is unnecessary param, default is DefaultConfigFile.
func GetContent(file ...string) string {
name := DefaultConfigFile
if len(file) > 0 {
name = file[0]
}
2021-05-17 00:07:06 +08:00
return customConfigContentMap.Get(name)
}
2021-05-17 00:07:06 +08:00
// RemoveContent removes the global configuration with specified `file`.
// If `name` is not passed, it removes configuration of the default group name.
func RemoveContent(file ...string) {
name := DefaultConfigFile
2019-06-19 09:06:52 +08:00
if len(file) > 0 {
name = file[0]
}
2021-05-17 00:07:06 +08:00
// Clear file cache for instances which cached `name`.
instances.LockFunc(func(m map[string]interface{}) {
if customConfigContentMap.Contains(name) {
for _, v := range m {
v.(*Config).jsonMap.Remove(name)
2021-03-03 14:29:01 +08:00
}
2021-05-17 00:07:06 +08:00
customConfigContentMap.Remove(name)
2021-01-27 00:55:45 +08:00
}
2021-03-03 14:29:01 +08:00
})
2021-05-17 00:07:06 +08:00
intlog.Printf(`RemoveContent: %s`, name)
}
2021-05-17 00:07:06 +08:00
// ClearContent removes all global configuration contents.
func ClearContent() {
customConfigContentMap.Clear()
// Clear cache for all instances.
instances.LockFunc(func(m map[string]interface{}) {
for _, v := range m {
v.(*Config).jsonMap.Clear()
2019-06-19 09:06:52 +08:00
}
})
2021-05-17 00:07:06 +08:00
intlog.Print(`RemoveConfig`)
}
// errorPrint checks whether printing error to stdout.
func errorPrint() bool {
return gcmd.GetOptWithEnv(errorPrintKey, true).Bool()
}