Documentation
¶
Overview ¶
Package unicon provides tools for managing hierarcial configuration from multiple sources
Index ¶
- func LoadConfig(config Configurable) error
- func SaveConfig(config Configurable) error
- type ArgvConfig
- type Config
- type Configurable
- type EnvConfig
- type FlagSetConfig
- type JSONConfig
- type MemoryConfig
- func (mem *MemoryConfig) All() map[string]interface{}
- func (mem *MemoryConfig) BulkSet(items map[string]interface{})
- func (mem *MemoryConfig) Get(key string) interface{}
- func (mem *MemoryConfig) GetBool(key string) bool
- func (mem *MemoryConfig) GetDuration(key string) time.Duration
- func (mem *MemoryConfig) GetFloat64(key string) float64
- func (mem *MemoryConfig) GetInt(key string) int
- func (mem *MemoryConfig) GetInt64(key string) int64
- func (mem *MemoryConfig) GetString(key string) string
- func (mem *MemoryConfig) GetTime(key string) time.Time
- func (mem *MemoryConfig) Reset(datas ...map[string]interface{})
- func (mem *MemoryConfig) Set(key string, value interface{})
- type PflagConfig
- type ReadableConfig
- func NewArgvConfig(prefix string, namespaces ...string) ReadableConfig
- func NewEnvConfig(prefix string, namespaces ...string) ReadableConfig
- func NewFlagSetConfig(fs *pflag.FlagSet, prefix string, namespaces ...string) ReadableConfig
- func NewPflagConfig(prefix string, namespaces ...string) ReadableConfig
- func NewURLConfig(url string) ReadableConfig
- type URLConfig
- type Unicon
- func (uni *Unicon) All() map[string]interface{}
- func (uni *Unicon) BulkSet(items map[string]interface{})
- func (uni *Unicon) BulkSetDefault(items map[string]interface{})
- func (uni *Unicon) Debug()
- func (uni *Unicon) Get(key string) interface{}
- func (uni *Unicon) GetBool(key string) bool
- func (uni *Unicon) GetDefault(key string) interface{}
- func (uni *Unicon) GetDuration(key string) time.Duration
- func (uni *Unicon) GetFloat64(key string) float64
- func (uni *Unicon) GetInt(key string) int
- func (uni *Unicon) GetInt64(key string) int64
- func (uni *Unicon) GetString(key string) string
- func (uni *Unicon) GetTime(key string) time.Time
- func (uni *Unicon) Load() error
- func (uni *Unicon) Reset(datas ...map[string]interface{})
- func (uni *Unicon) ResetDefaults(datas ...map[string]interface{})
- func (uni *Unicon) Save() error
- func (uni *Unicon) Set(key string, value interface{})
- func (uni *Unicon) SetDefault(key string, value interface{})
- func (uni *Unicon) Sub(ns string) *Unicon
- func (uni *Unicon) Unmarshal(target interface{}) error
- func (uni *Unicon) Use(name string, config ...Configurable) Configurable
- type WritableConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadConfig ¶
func LoadConfig(config Configurable) error
LoadConfig loads a config if it is of type ReadableConfig, otherwise does nothing.
func SaveConfig ¶
func SaveConfig(config Configurable) error
SaveConfig saves if is of type WritableConfig, otherwise does nothing.
Types ¶
type ArgvConfig ¶
type ArgvConfig struct { Configurable Prefix string // contains filtered or unexported fields }
ArgvConfig is the Argv configurable.
func (*ArgvConfig) Load ¶
func (ac *ArgvConfig) Load() (err error)
Load loads all the variables from argv to the underlaying Configurable. If a Prefix is provided for ArgvConfig then keys are imported with the Prefix removed so --test.asd=1 with Prefix 'test.' imports "asd" with value of 1
type Config ¶
type Config interface { WritableConfig // Use config as name, .Use("name") without the second parameter returns // the config previously added to the hierarchy with the name. // Use("name", Configurable) adds or replaces the configurable designated // by "Name" in the hierarchy Use(name string, config ...Configurable) Configurable }
Config is a Configurable that can Use other Configurables thus build a hierarchy
type Configurable ¶
type Configurable interface { Get(string) interface{} GetString(key string) string GetBool(key string) bool GetInt(key string) int GetInt64(key string) int64 GetFloat64(key string) float64 GetTime(key string) time.Time GetDuration(key string) time.Duration // Set a variable, nil to reset key Set(string, interface{}) // Overwrite items with items from map BulkSet(map[string]interface{}) // Reset the config data to passed data, if nothing is given set it to zero // value Reset(...map[string]interface{}) // Return a map of all variables All() map[string]interface{} }
Configurable is the main interface. Also the hierarcial configuration (Config) implements it.
type EnvConfig ¶
type EnvConfig struct { Configurable Prefix string // contains filtered or unexported fields }
EnvConfig can be used to read values from the environment into the underlaying Configurable
func (*EnvConfig) Load ¶
Load loads the data from os.Environ() to the underlaying Configurable. if a Prefix is set then variables are imported with self.Prefix removed from the name so MYAPP_test=1 exported in env and read from ENV by EnvConfig{Prefix:"MYAPP_"} can be found from EnvConfig.Get("test") If namespaces are declared, POSTGRESQL_HOST becomes postgresql.host
type FlagSetConfig ¶
type FlagSetConfig struct { Configurable Prefix string // contains filtered or unexported fields }
FlagSetConfig can be used to read arguments in the posix flag style into the underlaying Configurable
func (*FlagSetConfig) Load ¶
func (fsc *FlagSetConfig) Load() (err error)
Load loads all the variables from argv to the underlaying Configurable. If a Prefix is provided for FlagSetConfig then keys are imported with the Prefix removed so --test.asd=1 with Prefix 'test.' imports "asd" with value of 1
type JSONConfig ¶
type JSONConfig struct { Configurable Path string }
JSONConfig is the json configurable
func NewJSONConfig ¶
func NewJSONConfig(path string, cfg ...Configurable) *JSONConfig
NewJSONConfig returns a new WritableConfig backed by a json file at path. The file does not need to exist, if it does not exist the first Save call will create it.
func (*JSONConfig) Load ¶
func (jc *JSONConfig) Load() (err error)
Load attempts to load the json configuration at JSONConfig.Path and Set them into the underlaying Configurable
func (*JSONConfig) Save ¶
func (jc *JSONConfig) Save() (err error)
Save attempts to save the configuration from the underlaying Configurable to json file at JSONConfig.Path
type MemoryConfig ¶
type MemoryConfig struct {
// contains filtered or unexported fields
}
MemoryConfig is a simple abstraction to map[]interface{} for in process memory backed configuration only implements Configurable use JsonConfig to save/load if needed
func NewMemoryConfig ¶
func NewMemoryConfig() *MemoryConfig
NewMemoryConfig returns a new memory backed Configurable The most basic Configurable simply backed by a map[string]interface{}
func (*MemoryConfig) All ¶
func (mem *MemoryConfig) All() map[string]interface{}
All returns all keys
func (*MemoryConfig) BulkSet ¶
func (mem *MemoryConfig) BulkSet(items map[string]interface{})
BulkSet overwrites the overrides with items in the provided map
func (*MemoryConfig) GetBool ¶
func (mem *MemoryConfig) GetBool(key string) bool
GetBool casts the value as a bool. If value is nil, it returns false
func (*MemoryConfig) GetDuration ¶
func (mem *MemoryConfig) GetDuration(key string) time.Duration
GetDuration casts the value as a time.Duration. If the value is nil, it returns the 0 duration
func (*MemoryConfig) GetFloat64 ¶
func (mem *MemoryConfig) GetFloat64(key string) float64
GetFloat64 casts the value as a float64. If the value is nil, it returns 0.0
func (*MemoryConfig) GetInt ¶
func (mem *MemoryConfig) GetInt(key string) int
GetInt casts the value as an int. If the value is nil, it returns 0
func (*MemoryConfig) GetInt64 ¶
func (mem *MemoryConfig) GetInt64(key string) int64
GetInt64 casts the value as an int64. If the value is nil, it returns 0
func (*MemoryConfig) GetString ¶
func (mem *MemoryConfig) GetString(key string) string
GetString casts the value as a string. If value is nil, it returns ""
func (*MemoryConfig) GetTime ¶
func (mem *MemoryConfig) GetTime(key string) time.Time
GetTime casts the value as a time.Time. If the value is nil, it returns the 0 time
func (*MemoryConfig) Reset ¶
func (mem *MemoryConfig) Reset(datas ...map[string]interface{})
Reset if no arguments are provided Reset() re-creates the underlaying map
func (*MemoryConfig) Set ¶
func (mem *MemoryConfig) Set(key string, value interface{})
Set a key to value
type PflagConfig ¶
type PflagConfig struct { Configurable Prefix string // contains filtered or unexported fields }
PflagConfig can be used to read arguments in the posix flag style into the underlaying Configurable
func (*PflagConfig) Load ¶
func (pc *PflagConfig) Load() (err error)
Load loads all the variables from argv to the underlaying Configurable. If a Prefix is provided for PflagConfig then keys are imported with the Prefix removed so --test.asd=1 with Prefix 'test.' imports "asd" with value of 1
type ReadableConfig ¶
type ReadableConfig interface { Configurable // Load the configuration Load() error }
ReadableConfig is a Configurable that can be loaded
func NewArgvConfig ¶
func NewArgvConfig(prefix string, namespaces ...string) ReadableConfig
NewArgvConfig creates a new ArgvConfig and returns it as a ReadableConfig
func NewEnvConfig ¶
func NewEnvConfig(prefix string, namespaces ...string) ReadableConfig
NewEnvConfig creates a new Env config backed by a memory config
func NewFlagSetConfig ¶
func NewFlagSetConfig(fs *pflag.FlagSet, prefix string, namespaces ...string) ReadableConfig
NewFlagSetConfig creates a new FlagSetConfig and returns it as a ReadableConfig
func NewPflagConfig ¶
func NewPflagConfig(prefix string, namespaces ...string) ReadableConfig
NewPflagConfig creates a new PflagConfig and returns it as a ReadableConfig
func NewURLConfig ¶
func NewURLConfig(url string) ReadableConfig
NewURLConfig returns a new Configurable backed by JSON at url
type URLConfig ¶
type URLConfig struct { Configurable // contains filtered or unexported fields }
URLConfig is the url configurable
type Unicon ¶
type Unicon struct {
// contains filtered or unexported fields
}
Unicon is the Hierarchical Config that can be used to mount other configs that are searched for keys by Get
func NewConfig ¶
func NewConfig(initial Configurable, defaults ...Configurable) *Unicon
NewConfig creates a new config that is by default backed by a MemoryConfig Configurable. Takes optional initial configuration and an optional defaults
func (*Unicon) All ¶
All returns a map of data from all Configurables in use the first found instance of variable found is provided. Config.Use("a", NewMemoryConfig()). Config.Use("b", NewMemoryConfig()). Config.Use("a").Set("a","1"). Config.Set("b").Set("a","2"). then. Config.All()["a"] == "1". Config.Get("a") == "1". Config.Use("b".).Get("a") == "2".
func (*Unicon) BulkSetDefault ¶
BulkSetDefault overwrites the defaults with items in the provided map
func (*Unicon) Debug ¶
func (uni *Unicon) Debug()
Debug prints out simple list of keys as returned by All()
func (*Unicon) GetDefault ¶
GetDefault returns the default for the key, regardless of whether Set() has been called for that key or not.
func (*Unicon) GetDuration ¶
GetDuration casts the value as a time.Duration. If the value is nil, it returns the 0 duration
func (*Unicon) GetFloat64 ¶
GetFloat64 casts the value as a float64. If the value is nil, it returns 0.0
func (*Unicon) GetTime ¶
GetTime casts the value as a time.Time. If the value is nil, it returns the 0 time
func (*Unicon) Reset ¶
Reset resets all configs with the provided data, if no data is provided empties all stores. Never touches the Defaults, to reset Defaults use Unicon.ResetDefaults()
func (*Unicon) ResetDefaults ¶
ResetDefaults resets just the defaults with the provided data. Needed because Reset() doesn't reset the defaults.
func (*Unicon) Save ¶
Save saves all mounted configurations in the hierarchy that implement the WritableConfig interface
func (*Unicon) SetDefault ¶
SetDefault sets the default value, which will be looked up if no other values match the key. The default is preserved across Set() and Reset() can can only be modified by SetDefault() or ResetDefaults()
func (*Unicon) Sub ¶
Sub returns a new Unicon but with the namespace prepended to Gets/Sets/Subs behind the scenes
func (*Unicon) Use ¶
func (uni *Unicon) Use(name string, config ...Configurable) Configurable
Use config as named config and return an already set and loaded config mounts a new configuration in the hierarchy. conf.Use("global", NewUrlConfig("http://host.com/config..json")). conf.Use("local", NewFileConfig("./config.json")) err := conf.Load();. Then get variable from specific config. conf.Use("global").Get("key"). or traverse the hierarchy and search for "key". conf.Get("key"). conf.Use("name") returns a nil value for non existing config named "name".
type WritableConfig ¶
type WritableConfig interface { ReadableConfig // Save configuration Save() error }
WritableConfig is a Configurable that can be Loaded & Saved