config

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2019 License: MIT Imports: 13 Imported by: 0

README

itdesigndev/config

Go configuration library. Simple wrapper around knadh/koanf (https://github.com/knadh/koanf)

See config_test.go for examples.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

The Config structure

func FromFile

func FromFile(f string) (c *Config, err error)

FromFile creates a new Config object from either a yaml or a json file - depending on the extension

func FromJson

func FromJson(f string) (c *Config, err error)

FromJson creates a new Config object from json file

func FromYaml

func FromYaml(f string) (c *Config, err error)

FromYaml creates a new Config object from yaml file

func New

func New() *Config

New creates a new Config object with "." as delimiter

func NewConfig

func NewConfig(dl string) *Config

NewConfig creates a new Config object with specified delimiter

func (*Config) All

func (c *Config) All() map[string]interface{}

All returns a flat map of flattened key paths and their corresponding config values

func (*Config) Bool

func (c *Config) Bool(path string) bool

Bool returns the bool value of a given key path or false if the path does not exist or if the value is not a valid bool representation. Accepted string representations of bool are the ones supported by strconv.ParseBool.

func (*Config) BoolMap

func (c *Config) BoolMap(path string) map[string]bool

BoolMap returns the map[string]bool value of a given key path or an empty map[string]bool if the path does not exist or if the value is not a valid bool map.

func (*Config) Bools

func (c *Config) Bools(path string) []bool

Bools returns the []bool slice value of a given key path or an empty []bool slice if the path does not exist or if the value is not a valid bool slice.

func (*Config) Bytes

func (c *Config) Bytes(path string) []byte

Bytes returns the []byte value of a given key path or an empty []byte slice if the path does not exist or if the value is not a valid string.

func (*Config) Cut

func (c *Config) Cut(path string) *Config

Cut cuts the loaded nested conf map at the given path and returns a new Koanf instance with the children

func (*Config) Delimiter

func (c *Config) Delimiter() string

Delimiter returns the delimiter string

func (*Config) Duration

func (c *Config) Duration(path string) time.Duration

Duration returns the time.Duration value of a given key path assuming that the key contains a valid numeric value.

func (*Config) Exists

func (c *Config) Exists(path string) bool

Exists returns true if the given key path exists in the conf map

func (*Config) Float64

func (c *Config) Float64(path string) float64

Float64 returns the float64 value of a given key path or 0 if the path does not exist or if the value is not a valid float64.

func (*Config) Float64Map

func (c *Config) Float64Map(path string) map[string]float64

Float64Map returns the map[string]float64 value of a given key path or an empty map[string]float64 if the path does not exist or if the value is not a valid float64 map.

func (*Config) Float64s

func (c *Config) Float64s(path string) []float64

Float64s returns the []float64 slice value of a given key path or an empty []float64 slice if the path does not exist or if the value is not a valid float64 slice.

func (*Config) Get

func (c *Config) Get(path string) interface{}

Get returns the value for the given key path, and if it doesnt exist, returns nil

func (*Config) Int

func (c *Config) Int(path string) int

Int returns the int value of a given key path or 0 if the path does not exist or if the value is not a valid int.

func (*Config) Int64

func (c *Config) Int64(path string) int64

Int64 returns the int64 value of a given key path or 0 if the path does not exist or if the value is not a valid int64.

func (*Config) Int64Map

func (c *Config) Int64Map(path string) map[string]int64

Int64Map returns the map[string]int64 value of a given key path or an empty map[string]int64 if the path does not exist or if the value is not a valid int64 map.

func (*Config) Int64s

func (c *Config) Int64s(path string) []int64

Int64s returns the []int64 slice value of a given key path or an empty []int64 slice if the path does not exist or if the value is not a valid int slice.

func (*Config) IntMap

func (c *Config) IntMap(path string) map[string]int

IntMap returns the map[string]int value of a given key path or an empty map[string]int if the path does not exist or if the value is not a valid int map.

func (*Config) Ints

func (c *Config) Ints(path string) []int

Ints returns the []int slice value of a given key path or an empty []int slice if the path does not exist or if the value is not a valid int slice.

func (*Config) KeyMap

func (c *Config) KeyMap() map[string][]string

KeyMap returns a map of all possible key path combinations possible in the loaded nested conf map

func (*Config) Keys

func (c *Config) Keys() []string

Keys returns the list of flattened key paths that can be used to access config values

func (*Config) LoadDefault

func (c *Config) LoadDefault(m map[string]interface{}) error

LoadDefault loads default values from map eg

cfg.LoadDefault(map[string]interface{}{
			"com":                 "COM4",
			"reset":               false,
			"path":                "/tmp",
			"timeperiod":          60,
			"debug.active":        false,
			"debug.path":          "/tmp",
			"websserver.active":   true,
			"websserver.port":     ":8080",
			"webservice.version":  true,
			"webservice.winsol":   true,
			"webservice.data":     true,
			"webservice.download": true,
		})

func (*Config) LoadEnv

func (c *Config) LoadEnv(pfx string) error

LoadEnv loads config rom env variables Load environment variables and merge into the loaded config. pfx is the prefix to filter the env vars by. c.dl is the delimiter used to represent the key hierarchy in env vars. The (optional, or can be nil) function can be used to transform the env var names, for instance, to lowercase them.

For example, env vars: MYVAR_TYPE and MYVAR_PARENT1_CHILD1_NAME will be merged into the "type" and the nested "parent1.child1.name" keys in the config file here as we lowercase the key, replace `_` with `.` and strip the MYVAR_ prefix so that only "parent1.child1.name" remains.

func (*Config) LoadFile

func (c *Config) LoadFile(f string) (err error)

LoadFile loads either a yaml or a json - depending on the extension

func (*Config) LoadFlag

func (c *Config) LoadFlag(f *flag.FlagSet) error

LoadFlag loads config from flags

func (*Config) LoadJson

func (c *Config) LoadJson(f string) error

LoadJson loads json config file

func (*Config) LoadYaml

func (c *Config) LoadYaml(f string) error

LoadYaml loads yaml config file

func (*Config) MapKeys

func (c *Config) MapKeys(path string) []string

MapKeys returns a sorted string list of keys in a map addressed by the given path. If the path is not a map, an empty string slice is returned.

func (*Config) Merge

func (c *Config) Merge(config *Config) *Config

Merge merges the config map of a Config instance into the current instance

func (*Config) Print

func (c *Config) Print() *Config

Print prints a human readable copy of the flattened key paths and their values for debugging

func (*Config) Sprint

func (c *Config) Sprint() string

Sprint returns a human readable copy of the flattened key paths and their values for debugging

func (*Config) String

func (c *Config) String(path string) string

String returns the string value of a given key path or "" if the path does not exist or if the value is not a valid string.

func (*Config) StringMap

func (c *Config) StringMap(path string) map[string]string

StringMap returns the map[string]string value of a given key path or an empty map[string]string if the path does not exist or if the value is not a valid string map.

func (*Config) Strings

func (c *Config) Strings(path string) []string

Strings returns the []string slice value of a given key path or an empty []string slice if the path does not exist or if the value is not a valid string slice.

func (*Config) Time

func (c *Config) Time(path, layout string) time.Time

Time attempts to parse the value of a given key path and return time.Time representation. If the value is numeric, it is treated as a UNIX timestamp and if it's string, a parse is attempted with the given layout.

func (*Config) Unmarshal

func (c *Config) Unmarshal(path string, out interface{}) error

Unmarshal scans the given nested key path into a given struct (like json.Unmarshal)

func (*Config) WatchFile

func (c *Config) WatchFile(f string, onReloaded func()) error

WatchFile watch loaded config file based on extension (either yaml or json) for changes, reloads it when changed and triggers onReloaded callback

func (*Config) WatchJson

func (c *Config) WatchJson(f string, onReloaded func()) error

WatchJson watch loaded json config file for changes, reloads it when changed and triggers onReloaded callback

func (*Config) WatchYaml

func (c *Config) WatchYaml(f string, onReloaded func()) error

WatchYaml watch loaded yaml config file for changes, reloads it when changed and triggers onReloaded callback

Jump to

Keyboard shortcuts

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