config: github.com/globocom/config Index | Files

package config

import "github.com/globocom/config"

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

Package Files

checker.go config.go

Variables

var ErrMismatchConf = errors.New("Your conf is wrong:")

func Bytes Uses

func Bytes() ([]byte, error)

Bytes serialize the configuration in YAML format.

func Check Uses

func Check(checkers []Checker) error

Check a parsed config file and consider warnings as errors.

func CheckWithWarnings Uses

func CheckWithWarnings(checkers []Checker, warningWriter io.Writer) error

Check a parsed config file and writes warnings to received writer.

func Get Uses

func Get(key string) (interface{}, error)

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 GetBool Uses

func GetBool(key string) (bool, error)

GetBool does a type assertion before returning the requested value

func GetDuration Uses

func GetDuration(key string) (time.Duration, error)

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 Uses

func GetFloat(key string) (float64, error)

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 Uses

func GetInt(key string) (int, error)

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 Uses

func GetList(key string) ([]string, error)

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 Uses

func GetString(key string) (string, error)

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 GetUint Uses

func GetUint(key string) (uint, error)

GetUint parses and returns an unsigned integer from the config file.

func NewWarning Uses

func NewWarning(msg string) error

func ReadAndWatchConfigFile Uses

func ReadAndWatchConfigFile(filePath string) error

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 Uses

func ReadConfigBytes(data []byte) error

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 Uses

func ReadConfigFile(filePath string) error

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 Uses

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.

func Unset Uses

func Unset(key string) error

Unset removes a key from the configuration map. It returns an error if the key is not defined.

Calling this function does not remove a key from a configuration file, only from the in-memory configuration object.

func WriteConfigFile Uses

func WriteConfigFile(filePath string, perm os.FileMode) error

WriteConfigFile writes the configuration to the disc, using the given path. The configuration is serialized in YAML format.

This function will create the file if it does not exist, setting permissions to "perm".

type Checker Uses

type Checker func() error

type ErrKeyNotFound Uses

type ErrKeyNotFound struct {
    Key string
}

func (ErrKeyNotFound) Error Uses

func (e ErrKeyNotFound) Error() string

type InvalidValue Uses

type InvalidValue struct {
    // contains filtered or unexported fields
}

func (*InvalidValue) Error Uses

func (e *InvalidValue) Error() string

Package config imports 12 packages (graph) and is imported by 59 packages. Updated 2019-03-20. Refresh now. Tools for package owners.