config

package
v0.0.0-...-538f495 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2014 License: BSD-3-Clause, BSD-3-Clause Imports: 10 Imported by: 0

README

#Config

Build Status

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.

Index

Constants

This section is empty.

Variables

This section is empty.

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.

func Get

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

Get returns the value for the given key, or an eror if the key is undefined.

The key is composed by 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"

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 doing type assertion and attempting conversions 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 doing a int type assertion before return 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 doing a string type assertion before return 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 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 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

Jump to

Keyboard shortcuts

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