Documentation ¶
Overview ¶
Package gcfg provides reading, caching and managing for configuration.
Index ¶
- Constants
- type Adapter
- type AdapterFile
- func (c *AdapterFile) AddPath(paths ...string) (err error)
- func (c *AdapterFile) Available(ctx context.Context, fileName ...string) bool
- func (c *AdapterFile) Clear()
- func (c *AdapterFile) ClearContent()
- func (c *AdapterFile) Data(ctx context.Context) (data map[string]interface{}, err error)
- func (c *AdapterFile) Dump()
- func (c *AdapterFile) Get(ctx context.Context, pattern string) (value interface{}, err error)
- func (c *AdapterFile) GetContent(file ...string) string
- func (c *AdapterFile) GetFileName() string
- func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error)
- func (c *AdapterFile) GetPaths() []string
- func (c *AdapterFile) MustGet(ctx context.Context, pattern string) *gvar.Var
- func (c *AdapterFile) RemoveContent(file ...string)
- func (c *AdapterFile) Set(pattern string, value interface{}) error
- func (c *AdapterFile) SetContent(content string, file ...string)
- func (c *AdapterFile) SetFileName(name string)
- func (c *AdapterFile) SetPath(path string) (err error)
- func (c *AdapterFile) SetViolenceCheck(check bool)
- type Config
- func (c *Config) Available(ctx context.Context, resource ...string) (ok bool)
- func (c *Config) Data(ctx context.Context) (data map[string]interface{}, err error)
- func (c *Config) Get(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
- func (c *Config) GetAdapter() Adapter
- func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
- func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
- func (c *Config) MustData(ctx context.Context) map[string]interface{}
- func (c *Config) MustGet(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
- func (c *Config) MustGetWithCmd(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
- func (c *Config) MustGetWithEnv(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
- func (c *Config) SetAdapter(adapter Adapter)
Examples ¶
Constants ¶
const ( DefaultInstanceName = "config" // DefaultName is the default instance name for instance usage. DefaultConfigFileName = "config" // DefaultConfigFile is the default configuration file name. )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter interface { // Available checks and returns the backend configuration service is available. // The optional parameter `resource` specifies certain configuration resource. // // Note that this function does not return error as it just does simply check for // backend configuration service. Available(ctx context.Context, resource ...string) (ok bool) // Get retrieves and returns value by specified `pattern` in current resource. // Pattern like: // "x.y.z" for map item. // "x.0.y" for slice item. Get(ctx context.Context, pattern string) (value interface{}, err error) // Data retrieves and returns all configuration data in current resource as map. // Note that this function may lead lots of memory usage if configuration data is too large, // you can implement this function if necessary. Data(ctx context.Context) (data map[string]interface{}, err error) }
Adapter is the interface for configuration retrieving.
type AdapterFile ¶
type AdapterFile struct {
// contains filtered or unexported fields
}
func NewAdapterFile ¶
func NewAdapterFile(file ...string) (*AdapterFile, error)
NewAdapterFile returns a new configuration management object. The parameter `file` specifies the default configuration file name for reading.
func (*AdapterFile) AddPath ¶
func (c *AdapterFile) AddPath(paths ...string) (err error)
AddPath adds an absolute or relative path to the search paths.
func (*AdapterFile) Available ¶
func (c *AdapterFile) Available(ctx context.Context, fileName ...string) bool
Available checks and returns whether configuration of given `file` is available.
func (*AdapterFile) Clear ¶
func (c *AdapterFile) Clear()
Clear removes all parsed configuration files content cache, which will force reload configuration content from file.
func (*AdapterFile) ClearContent ¶
func (c *AdapterFile) ClearContent()
ClearContent removes all global configuration contents.
func (*AdapterFile) Data ¶
func (c *AdapterFile) Data(ctx context.Context) (data map[string]interface{}, err error)
Data retrieves and returns all configuration data as map type.
func (*AdapterFile) Dump ¶
func (c *AdapterFile) Dump()
Dump prints current Json object with more manually readable.
func (*AdapterFile) Get ¶
func (c *AdapterFile) Get(ctx context.Context, pattern string) (value interface{}, err error)
Get retrieves and returns value by specified `pattern`. It returns all values of current Json object if `pattern` is given empty or string ".". It returns nil if no value found by `pattern`.
We can also access slice item by its index number in `pattern` like: "list.10", "array.0.name", "array.0.1.id".
It returns a default value specified by `def` if value for `pattern` is not found.
func (*AdapterFile) GetContent ¶
func (c *AdapterFile) GetContent(file ...string) string
GetContent returns customized configuration content for specified `file`. The `file` is unnecessary param, default is DefaultConfigFile.
func (*AdapterFile) GetFileName ¶
func (c *AdapterFile) GetFileName() string
GetFileName returns the default configuration file name.
func (*AdapterFile) GetFilePath ¶
func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error)
GetFilePath returns the absolute configuration file path for the given filename by `file`. If `file` is not passed, it returns the configuration file path of the default name. It returns an empty `path` string and an error if the given `file` does not exist.
func (*AdapterFile) GetPaths ¶
func (c *AdapterFile) GetPaths() []string
GetPaths returns the searching path array of current configuration manager.
func (*AdapterFile) RemoveContent ¶
func (c *AdapterFile) RemoveContent(file ...string)
RemoveContent removes the global configuration with specified `file`. If `name` is not passed, it removes configuration of the default group name.
func (*AdapterFile) Set ¶
func (c *AdapterFile) Set(pattern string, value interface{}) error
Set sets value with specified `pattern`. It supports hierarchical data access by char separator, which is '.' in default. It is commonly used for updates certain configuration value in runtime. Note that, it is not recommended using `Set` configuration at runtime as the configuration would be automatically refreshed if underlying configuration file changed.
func (*AdapterFile) SetContent ¶
func (c *AdapterFile) SetContent(content string, file ...string)
SetContent sets customized configuration content for specified `file`. The `file` is unnecessary param, default is DefaultConfigFile.
func (*AdapterFile) SetFileName ¶
func (c *AdapterFile) SetFileName(name string)
SetFileName sets the default configuration file name.
func (*AdapterFile) SetPath ¶
func (c *AdapterFile) SetPath(path string) (err error)
SetPath sets the configuration directory path for file search. The parameter `path` can be absolute or relative path, but absolute path is strongly recommended.
func (*AdapterFile) SetViolenceCheck ¶
func (c *AdapterFile) SetViolenceCheck(check bool)
SetViolenceCheck sets whether to perform hierarchical conflict checking. This feature needs to be enabled when there is a level symbol in the key name. It is off in default.
Note that, turning on this feature is quite expensive, and it is not recommended allowing separators in the key names. It is best to avoid this on the application side.
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config is the configuration management object.
func Instance ¶
Instance returns an instance of Config with default settings. The parameter `name` is the name for the instance. But very note that, if the file "name.toml" exists in the configuration directory, it then sets it as the default configuration file. The toml file type is the default configuration file type.
func NewWithAdapter ¶
NewWithAdapter creates and returns a Config object with given adapter.
func (*Config) Available ¶
Available checks and returns the configuration service is available. The optional parameter `pattern` specifies certain configuration resource.
It returns true if configuration file is present in default AdapterFile, or else false. Note that this function does not return error as it just does simply check for backend configuration service.
func (*Config) Get ¶
Get retrieves and returns value by specified `pattern`. It returns all values of current Json object if `pattern` is given empty or string ".". It returns nil if no value found by `pattern`.
It returns a default value specified by `def` if value for `pattern` is not found.
func (*Config) GetAdapter ¶
GetAdapter returns the adapter of current Config object.
func (*Config) GetWithCmd ¶
func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
GetWithCmd returns the configuration value specified by pattern `pattern`. If the configuration value does not exist, then it retrieves and returns the command line option specified by `key`. It returns the default value `def` if none of them exists.
Fetching Rules: Command line arguments are in lowercase format, eg: gf.package.variable.
Example ¶
package main import ( "fmt" "os" "github.com/wowqhb/gf/v2/frame/g" "github.com/wowqhb/gf/v2/os/gcmd" "github.com/wowqhb/gf/v2/os/gctx" ) func main() { var ( key = `cmd.test` ctx = gctx.New() ) v, err := g.Cfg().GetWithCmd(ctx, key) if err != nil { panic(err) } fmt.Printf("cmd:%s\n", v) // Re-Initialize custom command arguments. os.Args = append(os.Args, fmt.Sprintf(`--%s=yes`, key)) gcmd.Init(os.Args...) // Retrieve the configuration and command option again. v, err = g.Cfg().GetWithCmd(ctx, key) if err != nil { panic(err) } fmt.Printf("cmd:%s", v) }
Output: cmd: cmd:yes
func (*Config) GetWithEnv ¶
func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
GetWithEnv returns the configuration value specified by pattern `pattern`. If the configuration value does not exist, then it retrieves and returns the environment value specified by `key`. It returns the default value `def` if none of them exists.
Fetching Rules: Environment arguments are in uppercase format, eg: GF_PACKAGE_VARIABLE.
Example ¶
package main import ( "fmt" "github.com/wowqhb/gf/v2/frame/g" "github.com/wowqhb/gf/v2/os/gctx" "github.com/wowqhb/gf/v2/os/genv" ) func main() { var ( key = `ENV_TEST` ctx = gctx.New() ) v, err := g.Cfg().GetWithEnv(ctx, key) if err != nil { panic(err) } fmt.Printf("env:%s\n", v) if err = genv.Set(key, "gf"); err != nil { panic(err) } v, err = g.Cfg().GetWithEnv(ctx, key) if err != nil { panic(err) } fmt.Printf("env:%s", v) }
Output: env: env:gf
func (*Config) MustGetWithCmd ¶
MustGetWithCmd acts as function GetWithCmd, but it panics if error occurs.
func (*Config) MustGetWithEnv ¶
MustGetWithEnv acts as function GetWithEnv, but it panics if error occurs.
func (*Config) SetAdapter ¶
SetAdapter sets the adapter of current Config object.