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