Documentation ¶
Overview ¶
Package config provide configuration facilities, handling configuration files in yaml format.
This package has been optimized for reads, so functions write functions (Set and Unset) are really slow when compared to Get functions.
Index ¶
- Variables
- func Bytes() ([]byte, error)
- func Check(checkers []Checker) error
- func CheckWithWarnings(checkers []Checker, warningWriter io.Writer) error
- func Get(key string) (interface{}, error)
- func GetBool(key string) (bool, error)
- func GetDuration(key string) (time.Duration, error)
- func GetFloat(key string) (float64, error)
- func GetInt(key string) (int, error)
- func GetList(key string) ([]string, error)
- func GetString(key string) (string, error)
- func GetUint(key string) (uint, error)
- func NewWarning(msg string) error
- func ReadAndWatchConfigFile(filePath string) error
- func ReadConfigBytes(data []byte) error
- func ReadConfigFile(filePath string) error
- func Set(key string, value interface{})
- func Unset(key string) error
- func WriteConfigFile(filePath string, perm os.FileMode) error
- type Checker
- type Configuration
- func (c *Configuration) Bytes() ([]byte, error)
- func (c *Configuration) Data() map[interface{}]interface{}
- func (c *Configuration) Get(key string) (interface{}, error)
- func (c *Configuration) GetBool(key string) (bool, error)
- func (c *Configuration) GetDuration(key string) (time.Duration, error)
- func (c *Configuration) GetFloat(key string) (float64, error)
- func (c *Configuration) GetInt(key string) (int, error)
- func (c *Configuration) GetList(key string) ([]string, error)
- func (c *Configuration) GetString(key string) (string, error)
- func (c *Configuration) GetUint(key string) (uint, error)
- func (c *Configuration) ReadAndWatchConfigFile(filePath string) error
- func (c *Configuration) ReadConfigBytes(data []byte) error
- func (c *Configuration) ReadConfigFile(filePath string) error
- func (c *Configuration) Set(key string, value interface{})
- func (c *Configuration) Store(data map[interface{}]interface{})
- func (c *Configuration) Unset(key string) error
- func (c *Configuration) WriteConfigFile(filePath string, perm os.FileMode) error
- type ErrKeyNotFound
- type InvalidValue
Constants ¶
This section is empty.
Variables ¶
var ErrMismatchConf = errors.New("Your conf is wrong:")
Functions ¶
func CheckWithWarnings ¶
Check a parsed config file and writes warnings to received writer.
func Get ¶
Get returns the value for the given key, or an error if the key is undefined.
The key is composed of all the key names separated by :, in case of nested keys. For example, suppose we have the following configuration yaml:
databases: mysql: host: localhost port: 3306
The key "databases:mysql:host" would return "localhost", while the key "port" would return an error.
Get will expand the value with environment values, ex.:
mongo: $MONGOURI
If there is an environment variable MONGOURI=localhost/test, the key "mongo" would return "localhost/test". If the variable value is a json object/list, this object will also be expanded.
func GetDuration ¶
GetDuration parses and returns a duration from the config file. It may be an integer or a number specifying the amount of nanoseconds.
Here are some examples of valid durations:
- 1h30m0s
- 1e9 (one second)
- 100e6 (one hundred milliseconds)
- 1 (one nanosecond)
- 1000000000 (one billion nanoseconds, or one second)
func GetFloat ¶
GetFloat works like Get, but does a float type assertion and attempts string conversion before returning the value.
It returns error if the key is undefined or if it is not a float.
func GetInt ¶
GetInt works like Get, but does an int type assertion and attempts string conversion before returning the value.
It returns error if the key is undefined or if it is not a int.
func GetList ¶
GetList works like Get, but returns a slice of strings instead. It must be written down in the config as YAML lists.
Here are two example of YAML lists:
names: - Mary - John - Paul - Petter
If GetList find an item that is not a string (for example 5.08734792), it will convert the item.
func GetString ¶
GetString works like Get, but does an string type assertion before returning the value.
It returns error if the key is undefined or if it is not a string.
func NewWarning ¶
func ReadAndWatchConfigFile ¶
ReadAndWatchConfigFile reads and watchs for changes in the configuration file. Whenever the file change, and its contents are valid YAML, the configuration gets updated. With this function, daemons that use this package may reload configuration without restarting.
func ReadConfigBytes ¶
ReadConfigBytes receives a slice of bytes and builds the internal configuration object.
If the given slice is not a valid yaml file, ReadConfigBytes returns a non-nil error.
func ReadConfigFile ¶
ReadConfigFile reads the content of a file and calls ReadConfigBytes to build the internal configuration object.
It returns error if it can not read the given file or if the file contents is not valid yaml.
func Set ¶
func Set(key string, value interface{})
Set redefines or defines a value for a key. The key has the same format that it has in Get and GetString.
Values defined by this function affects only runtime informatin, nothing defined by Set is persisted in the filesystem or any database.
Types ¶
type Configuration ¶
var DefaultConfig Configuration
func (*Configuration) Bytes ¶
func (c *Configuration) Bytes() ([]byte, error)
func (*Configuration) Data ¶
func (c *Configuration) Data() map[interface{}]interface{}
func (*Configuration) Get ¶
func (c *Configuration) Get(key string) (interface{}, error)
func (*Configuration) GetDuration ¶
func (c *Configuration) GetDuration(key string) (time.Duration, error)
func (*Configuration) ReadAndWatchConfigFile ¶
func (c *Configuration) ReadAndWatchConfigFile(filePath string) error
func (*Configuration) ReadConfigBytes ¶
func (c *Configuration) ReadConfigBytes(data []byte) error
func (*Configuration) ReadConfigFile ¶
func (c *Configuration) ReadConfigFile(filePath string) error
func (*Configuration) Set ¶
func (c *Configuration) Set(key string, value interface{})
func (*Configuration) Store ¶
func (c *Configuration) Store(data map[interface{}]interface{})
func (*Configuration) Unset ¶
func (c *Configuration) Unset(key string) error
func (*Configuration) WriteConfigFile ¶
func (c *Configuration) WriteConfigFile(filePath string, perm os.FileMode) error
type ErrKeyNotFound ¶
type ErrKeyNotFound struct {
Key string
}
func (ErrKeyNotFound) Error ¶
func (e ErrKeyNotFound) Error() string
type InvalidValue ¶
type InvalidValue struct {
// contains filtered or unexported fields
}
func (*InvalidValue) Error ¶
func (e *InvalidValue) Error() string