# README
cfg
import "github.com/ardanlabs/kit/cfg"
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 New(p Provider) (*Config, error)
- 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
Package files
cfg.go cfg_default.go doc.go env_provider.go file_provider.go map_provider.go
func Bool
func Bool(key string) (bool, error)
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
func Duration(key string) (time.Duration, error)
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
func Init(p Provider) error
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
func Int(key string) (int, error)
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
func MustBool(key string) bool
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
func MustDuration(key string) time.Duration
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
func MustInt(key string) int
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
func MustString(key string) string
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
func MustTime(key string) time.Time
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
func MustURL(key string) *url.URL
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 SetBool
func SetBool(key string, value bool)
SetBool adds or modifies the default Config for the specified key and value.
func SetDuration
func SetDuration(key string, value time.Duration)
SetDuration adds or modifies the default Config for the specified key and value.
func SetInt
func SetInt(key string, value int)
SetInt adds or modifies the default Config for the specified key and value.
func SetString
func SetString(key string, value string)
SetString adds or modifies the default Config for the specified key and value.
func SetTime
func SetTime(key string, value time.Time)
SetTime adds or modifies the default Config for the specified key and value.
func SetURL
func SetURL(key string, value *url.URL)
SetURL adds or modifies the default Config for the specified key and value.
func String
func String(key string) (string, error)
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.
func Time
func Time(key string) (time.Time, error)
Time calls the default Config and returns the value of the given key as a Time. It will return an error if the key was not found or the value can't be converted to a Time.
func URL
func URL(key string) (*url.URL, error)
URL calls the default Config and returns the value of the given key as a URL. It will return an error if the key was not found or the value can't be converted to a URL.
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
func New(p Provider) (*Config, error)
New populates a new Config from a Provider. It will return an error if there was any problem reading from the Provider.
func (*Config) Bool
func (c *Config) Bool(key string) (bool, error)
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
func (c *Config) Duration(key string) (time.Duration, error)
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
func (c *Config) Int(key string) (int, error)
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
func (c *Config) Log() string
Log returns a string to help with logging your configuration. It excludes any values whose key contains the string "PASS".
func (*Config) MustBool
func (c *Config) MustBool(key string) bool
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
func (c *Config) MustDuration(key string) time.Duration
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
func (c *Config) MustInt(key string) int
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
func (c *Config) MustString(key string) string
MustString returns the value of the given key as a string. It will panic if the key was not found.
func (*Config) MustTime
func (c *Config) MustTime(key string) time.Time
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
func (c *Config) MustURL(key string) *url.URL
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
func (c *Config) SetBool(key string, value bool)
SetBool adds or modifies the configuration for the specified key and value.
func (*Config) SetDuration
func (c *Config) SetDuration(key string, value time.Duration)
SetDuration adds or modifies the configuration for a given duration at a specific key.
func (*Config) SetInt
func (c *Config) SetInt(key string, value int)
SetInt adds or modifies the configuration for the specified key and value.
func (*Config) SetString
func (c *Config) SetString(key string, value string)
SetString adds or modifies the configuration for the specified key and value.
func (*Config) SetTime
func (c *Config) SetTime(key string, value time.Time)
SetTime adds or modifies the configuration for the specified key and value.
func (*Config) SetURL
func (c *Config) SetURL(key string, value *url.URL)
SetURL adds or modifies the configuration for the specified key and value.
func (*Config) String
func (c *Config) String(key string) (string, error)
String returns the value of the given key as a string. It will return an error if key was not found.
func (*Config) Time
func (c *Config) Time(key string) (time.Time, error)
Time returns the value of the given key as a Time. It will return an error if the key was not found or the value can't be converted to a Time.
func (*Config) URL
func (c *Config) URL(key string) (*url.URL, error)
URL returns the value of the given key as a URL. It will return an error if the key was not found or the value can't be converted to a URL.
type EnvProvider
type EnvProvider struct {
Namespace string
}
EnvProvider provides configuration from the environment. All keys will be made uppercase.
func (EnvProvider) Provide
func (ep EnvProvider) Provide() (map[string]string, error)
Provide implements the Provider interface.
type FileProvider
type FileProvider struct {
Filename string
}
FileProvider describes a file based loader which loads the configuration from a file listed.
func (FileProvider) Provide
func (fp FileProvider) Provide() (map[string]string, error)
Provide implements the Provider interface.
type MapProvider
type MapProvider struct {
Map map[string]string
}
MapProvider provides a simple implementation of the Provider whereby it just returns a stored map.
func (MapProvider) Provide
func (mp MapProvider) Provide() (map[string]string, error)
Provide implements the Provider interface.
type Provider
type Provider interface {
Provide() (map[string]string, error)
}
Provider is implemented by the user to provide the configuration as a map. There are currently two Providers implemented, EnvProvider and MapProvider.
Generated by godoc2md