Documentation ¶
Overview ¶
Package cfg provides configuration options that are loaded from the environment. Configuration is then stored in memory and can be retrieved by its proper type.
To initialize the configuration system from your environment, call Init:
cfg.Init(cfg.EnvProvider{Namespace: "configKey"})
To retrieve values from configuration:
proc, err := cfg.String("proc_id") port, err := cfg.Int("port") ms, err := cfg.Time("stamp")
Use the Must set of function to retrieve a single value but these calls will panic if the key does not exist:
proc := cfg.MustString("proc_id") port := cfg.MustInt("port") ms := cfg.MustTime("stamp")
Index ¶
- func Bool(key string) (bool, error)
- func Duration(key string) (time.Duration, error)
- func Init(p Provider) error
- func Int(key string) (int, error)
- func Log() string
- func MustBool(key string) bool
- func MustDuration(key string) time.Duration
- func MustInt(key string) int
- func MustString(key string) string
- func MustTime(key string) time.Time
- func MustURL(key string) *url.URL
- func SetBool(key string, value bool)
- func SetDuration(key string, value time.Duration)
- func SetInt(key string, value int)
- func SetString(key string, value string)
- func SetTime(key string, value time.Time)
- func SetURL(key string, value *url.URL)
- func String(key string) (string, error)
- func Time(key string) (time.Time, error)
- func URL(key string) (*url.URL, error)
- type Config
- func (c *Config) Bool(key string) (bool, error)
- func (c *Config) Duration(key string) (time.Duration, error)
- func (c *Config) Int(key string) (int, error)
- func (c *Config) Log() string
- func (c *Config) MustBool(key string) bool
- func (c *Config) MustDuration(key string) time.Duration
- func (c *Config) MustInt(key string) int
- func (c *Config) MustString(key string) string
- func (c *Config) MustTime(key string) time.Time
- func (c *Config) MustURL(key string) *url.URL
- func (c *Config) SetBool(key string, value bool)
- func (c *Config) SetDuration(key string, value time.Duration)
- func (c *Config) SetInt(key string, value int)
- func (c *Config) SetString(key string, value string)
- func (c *Config) SetTime(key string, value time.Time)
- func (c *Config) SetURL(key string, value *url.URL)
- func (c *Config) String(key string) (string, error)
- func (c *Config) Time(key string) (time.Time, error)
- func (c *Config) URL(key string) (*url.URL, error)
- type EnvProvider
- type FileProvider
- type MapProvider
- type Provider
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bool ¶
Bool calls the default Config and returns the bool value of a given key as a bool. It will return an error if the key was not found or the value can't be converted to a bool.
func Duration ¶
Duration calls the default Config and returns the value of the given key as a duration. It will return an error if the key was not found or the value can't be converted to a Duration.
func Init ¶
Init populates the package's default Config and should be called only once. A Provider must be supplied which will return a map of key/value pairs to be loaded.
func Int ¶
Int calls the Default config and returns the value of the given key as an int. It will return an error if the key was not found or the value can't be converted to an int.
func Log ¶
func Log() string
Log returns a string to help with logging the package's default Config. It excludes any values whose key contains the string "PASS".
func MustBool ¶
MustBool calls the default Config and returns the bool value of a given key as a bool. It will panic if the key was not found or the value can't be converted to a bool.
func MustDuration ¶
MustDuration calls the default Config and returns the value of the given key as a MustDuration. It will panic if the key was not found or the value can't be converted to a MustDuration.
func MustInt ¶
MustInt calls the default Config and returns the value of the given key as an int. It will panic if the key was not found or the value can't be converted to an int.
func MustString ¶
MustString calls the default Config and returns the value of the given key as a string, else it will panic if the key was not found.
func MustTime ¶
MustTime calls the default Config ang returns the value of the given key as a Time. It will panic if the key was not found or the value can't be converted to a Time.
func MustURL ¶
MustURL calls the default Config and returns the value of the given key as a URL. It will panic if the key was not found or the value can't be converted to a URL.
func SetDuration ¶
SetDuration adds or modifies the default Config for the specified key and value.
func String ¶
String calls the default Config and returns the value of the given key as a string. It will return an error if key was not found.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config is a goroutine safe configuration store, with a map of values set from a config Provider.
func New ¶
New populates a new Config from a Provider. It will return an error if there was any problem reading from the Provider.
Example ¶
ExampleNew shows how to create and use a new config which can be passed around.
package main import ( "fmt" "log" "time" "github.com/ardanlabs/kit/cfg" ) func main() { c, err := cfg.New(cfg.MapProvider{ Map: map[string]string{ "IP": "80.23.233.10", "PORT": "8044", "INIT_STAMP": time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).UTC().Format(time.UnixDate), "FLAG": "off", }, }) if err != nil { log.Fatal(err) } // To get the ip. fmt.Println(c.MustString("IP")) // To get the port number. fmt.Println(c.MustInt("PORT")) // To get the timestamp. fmt.Println(c.MustTime("INIT_STAMP")) // To get the flag. fmt.Println(c.MustBool("FLAG")) }
Output: 80.23.233.10 8044 2009-11-10 23:00:00 +0000 UTC false
func (*Config) Bool ¶
Bool returns the bool value of a given key as a bool. It will return an error if the key was not found or the value can't be converted to a bool.
func (*Config) Duration ¶
Duration returns the value of the given key as a Duration. It will return an error if the key was not found or the value can't be converted to a Duration.
func (*Config) Int ¶
Int returns the value of the given key as an int. It will return an error if the key was not found or the value can't be converted to an int.
func (*Config) Log ¶
Log returns a string to help with logging your configuration. It excludes any values whose key contains the string "PASS".
func (*Config) MustBool ¶
MustBool returns the bool value of a given key as a bool. It will panic if the key was not found or the value can't be converted to a bool.
func (*Config) MustDuration ¶
MustDuration returns the value of the given key as a Duration. It will panic if the key was not found or the value can't be converted into a Duration.
func (*Config) MustInt ¶
MustInt returns the value of the given key as an int. It will panic if the key was not found or the value can't be converted to an int.
func (*Config) MustString ¶
MustString returns the value of the given key as a string. It will panic if the key was not found.
func (*Config) MustTime ¶
MustTime returns the value of the given key as a Time. It will panic if the key was not found or the value can't be converted to a Time.
func (*Config) MustURL ¶
MustURL returns the value of the given key as a URL. It will panic if the key was not found or the value can't be converted to a URL.
func (*Config) SetBool ¶
SetBool adds or modifies the configuration for the specified key and value.
func (*Config) SetDuration ¶
SetDuration adds or modifies the configuration for a given duration at a specific key.
func (*Config) SetString ¶
SetString adds or modifies the configuration for the specified key and value.
func (*Config) SetTime ¶
SetTime adds or modifies the configuration for the specified key and value.
func (*Config) String ¶
String returns the value of the given key as a string. It will return an error if key was not found.
type EnvProvider ¶
type EnvProvider struct {
Namespace string
}
EnvProvider provides configuration from the environment. All keys will be made uppercase.
type FileProvider ¶
type FileProvider struct {
Filename string
}
FileProvider describes a file based loader which loads the configuration from a file listed.
type MapProvider ¶
MapProvider provides a simple implementation of the Provider whereby it just returns a stored map.