Documentation
¶
Overview ¶
Package config presents a unified, hierarchical interface for retreiving configuration options from any registered providers. The package includes builtin providers for retrieving config settings from the environment and ini formatted config files.
Index ¶
- Constants
- Variables
- func RegisterConfigProvider(provider ConfigProvider)
- func Shutdown()
- func UnregisterConfigProvider(provider ConfigProvider)
- type ConfigEntry
- func GetAllVals(key, defaultVal string) ([]string, *ConfigEntry)
- func GetBoolVal(key string, offset int, defaultVal bool) (bool, *ConfigEntry)
- func GetEntries(key string) []*ConfigEntry
- func GetFloat32Val(key string, offset int, defaultVal float32) (float32, *ConfigEntry)
- func GetFloat64Val(key string, offset int, defaultVal float64) (float64, *ConfigEntry)
- func GetInt16Val(key string, offset int, defaultVal int16) (int16, *ConfigEntry)
- func GetInt32Val(key string, offset int, defaultVal int32) (int32, *ConfigEntry)
- func GetInt64Val(key string, offset int, defaultVal int64) (int64, *ConfigEntry)
- func GetInt8Val(key string, offset int, defaultVal int8) (int8, *ConfigEntry)
- func GetIntVal(key string, offset, defaultVal int) (int, *ConfigEntry)
- func GetUint16Val(key string, offset int, defaultVal uint16) (uint16, *ConfigEntry)
- func GetUint32Val(key string, offset int, defaultVal uint32) (uint32, *ConfigEntry)
- func GetUint64Val(key string, offset int, defaultVal uint64) (uint64, *ConfigEntry)
- func GetUint8Val(key string, offset int, defaultVal uint8) (uint8, *ConfigEntry)
- func GetUintVal(key string, offset int, defaultVal uint) (uint, *ConfigEntry)
- func GetVal(key string, offset int, defaultVal string) (string, *ConfigEntry)
- type ConfigProvider
- type EnvProvider
- type IniProvider
Constants ¶
const ( PERF_CFG_PROVIDER_REGISTERED = iota PERF_CFG_PROVIDER_UNREGISTERED PERF_CFG_COUNT )
Perf counters.
const ( PERF_CFG_ENV_PRIORITY = iota PERF_CFG_ENV_QUERIES PERF_CFG_ENV_COUNT )
Perf counters.
const ( PERF_CFG_INI_PRIORITY = iota PERF_CFG_INI_QUERIES PERF_CFG_INI_COUNT )
Perf counters.
const ENV_MOD_NAME = "EnvProvider"
Environment provider module name.
const ERR_KEY_NOT_FOUND = "Key not found: %v, default: %v"
Common error message format.
const INI_MOD_NAME = "IniProvider"
Ini provider module name
Variables ¶
var IniDir = filepath.Dir(fs.ExeFile())
The base directory from which all supplied file paths will be built. Defaults to the directory of the primary executable.
Functions ¶
func RegisterConfigProvider ¶
func RegisterConfigProvider(provider ConfigProvider)
RegisterConfigProvider registers a new config provider with the config service, also inserting it into its appropriate place in the priority map. Providers registered with the same priority level answer in reverse order from which they were added.
func Shutdown ¶
func Shutdown()
Shutdown clears all provider registrations and calls Shutdown() on each of them in turn. Shutdown order cannot be guaranteed.
func UnregisterConfigProvider ¶
func UnregisterConfigProvider(provider ConfigProvider)
UnregisterConfigProvider removes a registered config provider from the provider map and priority list. It will no longer answer GetVal calls from the master config service, but can still be used directly unless manually shutdown.
Types ¶
type ConfigEntry ¶
type ConfigEntry struct {
// contains filtered or unexported fields
}
ConfigEntry objects represent a given key and its associated values.
func GetAllVals ¶
func GetAllVals(key, defaultVal string) ([]string, *ConfigEntry)
GetAllVals searches through all registered config providers and returns all values from the first config entry found. If no matching config entries are found, it returns the supplied default value.
func GetBoolVal ¶
func GetBoolVal(key string, offset int, defaultVal bool) (bool, *ConfigEntry)
GetBoolVal searches through all registered config providers and returns a bool value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to a bool, the supplied default value is returned.
func GetEntries ¶
func GetEntries(key string) []*ConfigEntry
GetEntries returns all matching entries from the first config provider with entries that match the supplied key.
func GetFloat32Val ¶
func GetFloat32Val(key string, offset int, defaultVal float32) (float32, *ConfigEntry)
GetFloat32Val searches through all registered config providers and returns a float32 value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to a float32, the supplied default value is returned.
func GetFloat64Val ¶
func GetFloat64Val(key string, offset int, defaultVal float64) (float64, *ConfigEntry)
GetFloat64Val searches through all registered config providers and returns a float64 value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to a float64, the supplied default value is returned.
func GetInt16Val ¶
func GetInt16Val(key string, offset int, defaultVal int16) (int16, *ConfigEntry)
GetInt16Val searches through all registered config providers and returns an int16 value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to an int16, the supplied default value is returned.
func GetInt32Val ¶
func GetInt32Val(key string, offset int, defaultVal int32) (int32, *ConfigEntry)
GetInt32Val searches through all registered config providers and returns an int32 value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to an int32, the supplied default value is returned.
func GetInt64Val ¶
func GetInt64Val(key string, offset int, defaultVal int64) (int64, *ConfigEntry)
GetInt64Val searches through all registered config providers and returns an int64 value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to an int64, the supplied default value is returned.
func GetInt8Val ¶
func GetInt8Val(key string, offset int, defaultVal int8) (int8, *ConfigEntry)
GetInt8Val searches through all registered config providers and returns an int8 value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to an int8, the supplied default value is returned.
func GetIntVal ¶
func GetIntVal(key string, offset, defaultVal int) (int, *ConfigEntry)
GetIntVal searches through all registered config providers and returns an int value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to an int, the supplied default value is returned.
func GetUint16Val ¶
func GetUint16Val(key string, offset int, defaultVal uint16) (uint16, *ConfigEntry)
GetUint16Val searches through all registered config providers and returns a uint16 value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to a uint16, the supplied default value is returned.
func GetUint32Val ¶
func GetUint32Val(key string, offset int, defaultVal uint32) (uint32, *ConfigEntry)
GetUint32Val searches through all registered config providers and returns a uint32 value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to a uint32, the supplied default value is returned.
func GetUint64Val ¶
func GetUint64Val(key string, offset int, defaultVal uint64) (uint64, *ConfigEntry)
GetUint64Val searches through all registered config providers and returns a uint64 value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to a uint64, the supplied default value is returned.
func GetUint8Val ¶
func GetUint8Val(key string, offset int, defaultVal uint8) (uint8, *ConfigEntry)
GetUint8Val searches through all registered config providers and returns a uint8 value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to a uint8, the supplied default value is returned.
func GetUintVal ¶
func GetUintVal(key string, offset int, defaultVal uint) (uint, *ConfigEntry)
GetUintVal searches through all registered config providers and returns a uint value from the given offset of the first valid entry found. If no matching entries are found, or the value cannot be correctly parsed to a uint, the supplied default value is returned.
func GetVal ¶
func GetVal(key string, offset int, defaultVal string) (string, *ConfigEntry)
GetVal searches through all registered config providers and returns a string value from the given offset of the first valid entry found. If no matching entries are found, the supplied default value is returned.
func (*ConfigEntry) GetAllVals ¶
func (this *ConfigEntry) GetAllVals() []string
GetAllVals returns all values associated with this config entry.
func (*ConfigEntry) GetVal ¶
func (this *ConfigEntry) GetVal(offset int) string
GetVal attempts to return the value at a given offset with the list of this entries values. If offset is out of range, returns an empty string.
func (*ConfigEntry) Len ¶
func (this *ConfigEntry) Len() int
Len returns the number of values associated with this config entry.
func (*ConfigEntry) Name ¶
func (this *ConfigEntry) Name() string
Name returns the name, or key, of this config entry.
func (*ConfigEntry) Parser ¶
func (this *ConfigEntry) Parser() ConfigProvider
Parser returns the parent ConfigProvider instance that this config entry is a child of.
func (*ConfigEntry) String ¶
func (this *ConfigEntry) String() string
String returns a nicely formatted string representing the ConfigEntry object.
type ConfigProvider ¶
type ConfigProvider interface { GetEntriesByKey(name string) []*ConfigEntry GetFirstEntryByKey(name string) *ConfigEntry Name() string Priority() int Shutdown() }
ConfigProvider defines the interface that should be implemnted by config providers.
type EnvProvider ¶
type EnvProvider struct {
// contains filtered or unexported fields
}
EnvProvider represents a ConfigProvider implementation that queries the system environment for config entries.
func InitEnvProvider ¶
func InitEnvProvider(pri int) *EnvProvider
InitEnvProvider initializes a new EnvProvider config provider, registers it with the config services, and returns a pointer to the object for direct use, if required.
func (*EnvProvider) GetEntriesByKey ¶
func (this *EnvProvider) GetEntriesByKey(name string) []*ConfigEntry
GetEntriesByKey returns the requested environment variable, if present, formatted as a ConfigEntry object. If an environment variable matching requested name doesn't exist, nil is returned. Only one entry will ever be returned from this call, despite its signature, since duplicate environment variables cannot exist.
func (*EnvProvider) GetFirstEntryByKey ¶
func (this *EnvProvider) GetFirstEntryByKey(name string) *ConfigEntry
GetFirstEntryByKey returns the requested environment variable, if present, formatted as a ConfigEntry object. If an environment variable matching the requested name doesn't exist, nil is returned.
func (*EnvProvider) Name ¶
func (this *EnvProvider) Name() string
Name returns "EnvProvider", the name of this config module.
func (*EnvProvider) Priority ¶
func (this *EnvProvider) Priority() int
Priority returns the assigned priority for this EnvProvider object.
type IniProvider ¶
type IniProvider struct {
// contains filtered or unexported fields
}
IniProvider represents a ConfigProvider implementation which can query a given ini-formatted config file for config entries.
func InitIniProvider ¶
func InitIniProvider(path string, pri int) *IniProvider
InitIniProvider initializes a new IniProvider config provider for the give path, registers it with the conig service, and returns a pointer to the object for direct use, if required.
func (*IniProvider) GetEntriesByKey ¶
func (this *IniProvider) GetEntriesByKey(name string) []*ConfigEntry
GetEntriesByKey returns all entries within the ini file which match the queried key name. ConfigEntry names follow the format <Section>.<Key> .
func (*IniProvider) GetFirstEntryByKey ¶
func (this *IniProvider) GetFirstEntryByKey(name string) *ConfigEntry
GetFirstEntryByKey returns the first entry within the ini file which matches the queried key name. ConfigEntry names follow the format <Section>.<Key> .
func (*IniProvider) Name ¶
func (this *IniProvider) Name() string
Name returns "IniProvider", the name of this config module.
func (*IniProvider) Priority ¶
func (this *IniProvider) Priority() int
Priority returns the assigned priority for this EnvParser object.