Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "os" "testing/fstest" "github.com/affanshahid/configo" ) func main() { dir := fstest.MapFS{ "default.yml": { Data: []byte(` root: prop1: foo prop2: 100 prop3: false prop4: nestedProp1: 4 `), }, "production.yml": { Data: []byte(` root: prop2: 200 `), }, } os.Setenv("APP_ENV", "production") err := configo.Initialize(dir, configo.WithDeploymentFromEnv("APP_ENV")) if err != nil { panic(err) } fmt.Println(configo.MustGetString("root.prop1")) fmt.Println(configo.MustGetInt("root.prop2")) fmt.Println(configo.MustGetBool("root.prop3")) fmt.Println(configo.MustGetInt("root.prop4.nestedProp1")) }
Output: foo 200 false 4
Index ¶
- func Get(path string) (interface{}, error)
- func GetBool(path string) (bool, error)
- func GetDuration(path string) (time.Duration, error)
- func GetFloat64(path string) (float64, error)
- func GetInt(path string) (int, error)
- func GetInt32(path string) (int32, error)
- func GetInt64(path string) (int64, error)
- func GetIntSlice(path string) ([]int, error)
- func GetString(path string) (string, error)
- func GetStringMap(path string) (map[string]interface{}, error)
- func GetStringSlice(path string) ([]string, error)
- func GetTime(path string) (time.Time, error)
- func GetUint(path string) (uint, error)
- func GetUint32(path string) (uint32, error)
- func GetUint64(path string) (uint64, error)
- func Initialize(dir fs.FS, opts ...ConfigOption) (err error)
- func MustGet(path string) interface{}
- func MustGetBool(path string) bool
- func MustGetDuration(path string) time.Duration
- func MustGetFloat64(path string) float64
- func MustGetInt(path string) int
- func MustGetInt32(path string) int32
- func MustGetInt64(path string) int64
- func MustGetIntSlice(path string) []int
- func MustGetString(path string) string
- func MustGetStringMap(path string) map[string]interface{}
- func MustGetStringSlice(path string) []string
- func MustGetTime(path string) time.Time
- func MustGetUint(path string) uint
- func MustGetUint32(path string) uint32
- func MustGetUint64(path string) uint64
- type Config
- func (c *Config) Get(path string) (interface{}, error)
- func (c *Config) GetBool(path string) (bool, error)
- func (c *Config) GetDuration(path string) (time.Duration, error)
- func (c *Config) GetFloat64(path string) (float64, error)
- func (c *Config) GetInt(path string) (int, error)
- func (c *Config) GetInt32(path string) (int32, error)
- func (c *Config) GetInt64(path string) (int64, error)
- func (c *Config) GetIntSlice(path string) ([]int, error)
- func (c *Config) GetString(path string) (string, error)
- func (c *Config) GetStringMap(path string) (map[string]interface{}, error)
- func (c *Config) GetStringSlice(path string) ([]string, error)
- func (c *Config) GetTime(path string) (time.Time, error)
- func (c *Config) GetUint(path string) (uint, error)
- func (c *Config) GetUint32(path string) (uint32, error)
- func (c *Config) GetUint64(path string) (uint64, error)
- func (c *Config) Initialize() error
- func (c *Config) MustGet(path string) interface{}
- func (c *Config) MustGetBool(path string) bool
- func (c *Config) MustGetDuration(path string) time.Duration
- func (c *Config) MustGetFloat64(path string) float64
- func (c *Config) MustGetInt(path string) int
- func (c *Config) MustGetInt32(path string) int32
- func (c *Config) MustGetInt64(path string) int64
- func (c *Config) MustGetIntSlice(path string) []int
- func (c *Config) MustGetString(path string) string
- func (c *Config) MustGetStringMap(path string) map[string]interface{}
- func (c *Config) MustGetStringSlice(path string) []string
- func (c *Config) MustGetTime(path string) time.Time
- func (c *Config) MustGetUint(path string) uint
- func (c *Config) MustGetUint32(path string) uint32
- func (c *Config) MustGetUint64(path string) uint64
- type ConfigOption
- type Provider
- type ProviderFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetDuration ¶
GetDuration returns the value at the given path as a duration from the globalConfig
func GetFloat64 ¶
GetFloat64 returns the value at the given path as a float64 from the globalConfig
func GetIntSlice ¶
GetIntSlice returns the value at the given path as a slice of int values from the globalConfig
func GetStringMap ¶
GetStringMap returns the value at the given path as a map with string keys from the globalConfig and values as interfaces
func GetStringSlice ¶
GetStringSlice returns the value at the given path as a slice of string values from the globalConfig
func Initialize ¶
func Initialize(dir fs.FS, opts ...ConfigOption) (err error)
Initialize loads the global configuration
func MustGet ¶
func MustGet(path string) interface{}
MustGet is the same as `Get` except it panics in case of an error
func MustGetBool ¶
MustGetBool is the same as `GetBool` except it panics in case of an error
func MustGetDuration ¶
MustGetDuration is the same as `GetDuration` except it panics in case of an error
func MustGetFloat64 ¶
MustGetFloat64 is the same as `GetFloat64` except it panics in case of an error
func MustGetInt ¶
MustGetInt is the same as `GetInt` except it panics in case of an error
func MustGetInt32 ¶
MustGetInt32 is the same as `GetInt32` except it panics in case of an error
func MustGetInt64 ¶
MustGetInt64 is the same as `GetInt64` except it panics in case of an error
func MustGetIntSlice ¶
MustGetIntSlice is the same as `GetIntSlice` except it panics in case of an error
func MustGetString ¶
MustGetString is the same as `GetString` except it panics in case of an error
func MustGetStringMap ¶
MustGetStringMap is the same as `GetStringMap` except it panics in case of an error
func MustGetStringSlice ¶
MustGetStringSlice is the same as `GetStringSlice` except it panics in case of an error
func MustGetTime ¶
MustGetTime is the same as `GetTime` except it panics in case of an error
func MustGetUint ¶
MustGetUint is the same as `GetUint` except it panics in case of an error
func MustGetUint32 ¶
MustGetUint32 is the same as `GetUint32` except it panics in case of an error
func MustGetUint64 ¶
MustGetUint64 is the same as `GetUint64` except it panics in case of an error
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config is a hierarchical loader and access point for configurations. It allows loading configurations from mutiple files while being cognizant of the einvironment.
Files are loading in the following order:
default.EXT default-{instance}.EXT {deployment}.EXT {deployment}-{instance}.EXT {short_hostname}.EXT {short_hostname}-{instance}.EXT {short_hostname}-{deployment}.EXT {short_hostname}-{deployment}-{instance}.EXT {full_hostname}.EXT {full_hostname}-{instance}.EXT {full_hostname}-{deployment}.EXT {full_hostname}-{deployment}-{instance}.EXT local.EXT local-{instance}.EXT local-{deployment}.EXT local-{deployment}-{instance}.EXT
EXT can be: `yaml`, `yml`, `json`, `json5`, `hjson`, `toml`
deployment defines your current environment i.e dev, test, prod etc (defaults to "dev")
instance can be the node ID in a multi-node deployment (defaults to "")
shortHostname is the hostname till the first `.` (derived from `os.Hostname()` by default)
fullHostname is the full host name (defaults to `os.Hostname()`)
Each file overrides configurations from the file above. There is a special file called `env.EXT` which allows overriding configurations using environment variables
Example ¶
package main import ( "fmt" "os" "testing/fstest" "github.com/affanshahid/configo" ) func main() { dir := fstest.MapFS{ "default.yml": { Data: []byte(` root: prop1: foo prop2: 100 prop3: false prop4: nestedProp1: 4 `), }, "production.yml": { Data: []byte(` root: prop2: 200 `), }, } os.Setenv("APP_ENV", "production") config, err := configo.NewConfig(dir, configo.WithDeploymentFromEnv("APP_ENV")) if err != nil { panic(err) } err = config.Initialize() if err != nil { panic(err) } fmt.Println(config.MustGetString("root.prop1")) fmt.Println(config.MustGetInt("root.prop2")) fmt.Println(config.MustGetBool("root.prop3")) fmt.Println(config.MustGetInt("root.prop4.nestedProp1")) }
Output: foo 200 false 4
func NewConfig ¶
func NewConfig(dir fs.FS, opts ...ConfigOption) (*Config, error)
NewConfig creates a new Config dir is a FS of the directory containing the config files opts are functional options
func (*Config) GetDuration ¶
GetDuration returns the value at the given path as a duration
func (*Config) GetFloat64 ¶
GetFloat64 returns the value at the given path as a float64
func (*Config) GetIntSlice ¶
GetIntSlice returns the value at the given path as a slice of int values
func (*Config) GetStringMap ¶
GetStringMap returns the value at the given path as a map with string keys and values as interfaces
func (*Config) GetStringSlice ¶
GetStringSlice returns the value at the given path as a slice of string values
func (*Config) Initialize ¶
Initialize initializes and loads in the configurations This must be called before attempting to get values
func (*Config) MustGetBool ¶
MustGetBool is the same as `GetBool` except it panics in case of an error
func (*Config) MustGetDuration ¶
MustGetDuration is the same as `GetDuration` except it panics in case of an error
func (*Config) MustGetFloat64 ¶
MustGetFloat64 is the same as `GetFloat64` except it panics in case of an error
func (*Config) MustGetInt ¶
MustGetInt is the same as `GetInt` except it panics in case of an error
func (*Config) MustGetInt32 ¶
MustGetInt32 is the same as `GetInt32` except it panics in case of an error
func (*Config) MustGetInt64 ¶
MustGetInt64 is the same as `GetInt64` except it panics in case of an error
func (*Config) MustGetIntSlice ¶
MustGetIntSlice is the same as `GetIntSlice` except it panics in case of an error
func (*Config) MustGetString ¶
MustGetString is the same as `GetString` except it panics in case of an error
func (*Config) MustGetStringMap ¶
MustGetStringMap is the same as `GetStringMap` except it panics in case of an error
func (*Config) MustGetStringSlice ¶
MustGetStringSlice is the same as `GetStringSlice` except it panics in case of an error
func (*Config) MustGetTime ¶
MustGetTime is the same as `GetTime` except it panics in case of an error
func (*Config) MustGetUint ¶
MustGetUint is the same as `GetUint` except it panics in case of an error
func (*Config) MustGetUint32 ¶
MustGetUint32 is the same as `GetUint32` except it panics in case of an error
func (*Config) MustGetUint64 ¶
MustGetUint64 is the same as `GetUint64` except it panics in case of an error
type ConfigOption ¶
type ConfigOption func(*Config)
ConfigOption is a functional option to configure a Config instance
func WithDeployment ¶
func WithDeployment(deployment string) ConfigOption
WithDeployment sets the given deployment
func WithDeploymentFromEnv ¶
func WithDeploymentFromEnv(env string) ConfigOption
WithDeploymentFromEnv loads the deployment label from the given environment variable
func WithHostname ¶
func WithHostname(hostname string) ConfigOption
WithHostname uses the given string to set shortHostname and fullHostname
func WithHostnameFromEnv ¶
func WithHostnameFromEnv(env string) ConfigOption
WithHostnameFromEnv loads the hostname from the given environment variable
func WithInstance ¶
func WithInstance(instance string) ConfigOption
WithInstance sets the given instance
func WithInstanceFromEnv ¶
func WithInstanceFromEnv(env string) ConfigOption
WithInstanceFromEnv loads the instance id from the given environment variable