Documentation ¶
Overview ¶
Package autoconfig allows packages to be configured autonomously and reconfigured automatically.
Each package has its own configuration section in a global config file, neither main() nor any other part of your application has the knowledge of the package configuration. Config can be dynamically updated when the application receives a signal.
Supported file format are INI (using https://github.com/go-ini/ini) and YAML (using https://gopkg.in/yaml.v2).
Usage - YAML ¶
Init :
autoconfig.Load(yaml.New(cfgfile)) autoconfig.ReloadOn(syscall.SIGHUP)
Sample config file :
section_name: group: value: foobar
Package config :
package mypackage type GroupConfig struct { Value `yaml:"value"` } type PkgConf struct { Group GroupConfig `yaml:"group"` } func (c *PkgConf) Changed() { // Do something } var ( pkfCong = PkgConf{ Group: GroupConfig{ Value: "default value", }, } _ = autoconfig.Register("section_name", &pkgConf) )
Instance config :
package mypackage var ( // Set defaults _ = autoconfig.Register("section_name", &PkgConf{ Group: GroupConfig{ Value: "default value", }, }) ) type PkgClass struct {} func New() *PkgClass { n := &PkgClass{} // This will trigger a n.Reconfigure() call with the current config autoconfig.Reconfigure("section_name", n) return n } func (c *PkgClass) Reconfigure(c interface{}) { if cfg, ok := c.(*PkgConf); ok { // Do something } }
autoconfig will cleanly Lock/Unlock your structs provided they implement sync.Locker
Usage - INI ¶
Init :
autoconfig.Load(ini.New(cfgfile)) autoconfig.ReloadOn(syscall.SIGHUP)
Sample config file :
[section_name] value=foobar
Package config :
package mypackage type PkgConf struct { Value string `ini:"value"` } var ( pkfCong = PkgConf{ Value: "default value", } _ = autoconfig.Register("section_name", &pkgConf) )
Other file formats ¶
Any config file format can be used, provided a loader class implementing the `Loader` interface is provided :
type Loader interface { Load(map[string]interface{}) error }
Caveats ¶
* Only a single config file is supported,
* Values types are supported only if the underlying format supports them (e.g. INI does not support slices).
Index ¶
- Variables
- func Get(name string) (interface{}, bool)
- func Load(l Loader) error
- func MustGet(name string) interface{}
- func Reconfigure(name string, r Reconfigurable) bool
- func Register(name string, s interface{}) bool
- func Reload() error
- func ReloadOn(signals ...os.Signal)
- type Config
- func (c *Config) Get(name string) (interface{}, bool)
- func (c *Config) Load() error
- func (c *Config) MustGet(name string) interface{}
- func (c *Config) Reconfigure(name string, r Reconfigurable) bool
- func (c *Config) Register(name string, s interface{}) bool
- func (c *Config) Reload() error
- func (c *Config) ReloadOn(signals ...os.Signal)
- type Loader
- type Reconfigurable
- type UpdatableConfig
Constants ¶
This section is empty.
Variables ¶
var (
ErrNoLoader = errors.New("No loader was defined")
)
Functions ¶
func MustGet ¶
func MustGet(name string) interface{}
MustGet returns the configuration for the specified section from the default configuration. If the section does not exist, something will panic.
func Reconfigure ¶
func Reconfigure(name string, r Reconfigurable) bool
Reconfigure registers an instance to the default config. The config section must have been registered before using Register
func New() *PkgClass { c := &PkgClass{} config.Reconfigure("section_name", c) return c }
func Register ¶
Register registers a config structure for a config file section. The values passed will be used as defaults in the future. Defaults will be remembered : if a variable is defined, and then unset, it will be reset to the default value. If s implements UpdateableConfig, s.Changed() will be called when the config is reloaded and has changed.
var ( _ = config.Register("section_name", &PkgConfig{Value: "default"}) )
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config defines a config
func (*Config) MustGet ¶
MustGet returns the configuration for the specified section. If the section does not exist, something will panic.
func (*Config) Reconfigure ¶
func (c *Config) Reconfigure(name string, r Reconfigurable) bool
Reconfigure registers an instance. The config section must have been registered before using Register r.Reconfigure() will be called when config is reloaded and has changed. If config has been previously loaded, r.Reconfigure() will be called immediatly.
func (*Config) Register ¶
Register registers a config structure for a config file section. The values passed will be used as defaults in the future. Defaults will be remembered : if a variable is defined, and then unset, it will be reset to the default value. If s implements UpdateableConfig, s.Changed() will be called when the config is reloaded and has changed. If config has been previously loaded, s.Changed() will be called immediatly.
type Reconfigurable ¶
type Reconfigurable interface {
Reconfigure(interface{})
}
Reconfigurable defines the interface updateable instances need to implement. Each time the config is reloaded and the corresponding config section has changed, the Reconfigure function will be called for all instances.
type UpdatableConfig ¶
type UpdatableConfig interface {
Changed()
}
UpdatableConfig defines the interface updateable config need to implement. Each time the config is reloaded and the corresponding config section has changed, the Changed function will be called.
Directories ¶
Path | Synopsis |
---|---|
Package ini defines a loader for ini config files autoconfig.Load(ini.New(filename))
|
Package ini defines a loader for ini config files autoconfig.Load(ini.New(filename)) |
Package yaml defines a loader for yaml config files autoconfig.Load(yaml.New(filename))
|
Package yaml defines a loader for yaml config files autoconfig.Load(yaml.New(filename)) |