config

package module
v0.0.0-...-375aaee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2020 License: BSD-3-Clause Imports: 12 Imported by: 100

README

Config

ci

PkgGoDev

Config is a Go package to manage yaml configuration files.

For usage information, read our package documentation: http://godoc.org/github.com/tsuru/config.

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

Constants

This section is empty.

Variables

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

Functions

func Bytes

func Bytes() ([]byte, error)

Bytes serialize the configuration in YAML format.

func Check

func Check(checkers []Checker) error

Check a parsed config file and consider warnings as errors.

func CheckWithWarnings

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

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

func Get

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

func GetBool(key string) (bool, error)

GetBool does a type assertion before returning the requested value

func GetDuration

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

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

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

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

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

func GetUint(key string) (uint, error)

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

func NewWarning

func NewWarning(msg string) error

func ReadAndWatchConfigFile

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

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

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

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

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

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".

Types

type Checker

type Checker func() error

type Configuration

type Configuration struct {
	sync.RWMutex
	// contains filtered or unexported fields
}
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) GetBool

func (c *Configuration) GetBool(key string) (bool, error)

func (*Configuration) GetDuration

func (c *Configuration) GetDuration(key string) (time.Duration, error)

func (*Configuration) GetFloat

func (c *Configuration) GetFloat(key string) (float64, error)

func (*Configuration) GetInt

func (c *Configuration) GetInt(key string) (int, error)

func (*Configuration) GetList

func (c *Configuration) GetList(key string) ([]string, error)

func (*Configuration) GetString

func (c *Configuration) GetString(key string) (string, error)

func (*Configuration) GetUint

func (c *Configuration) GetUint(key string) (uint, 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL