Documentation ¶
Overview ¶
Package config is a go config management implement. support YAML,TOML,JSON,INI,HCL format.
Source code and other details for the project are available at GitHub:
https://github.com/gookit/config
JSON format content example:
{ "name": "app", "debug": false, "baseKey": "value", "age": 123, "envKey": "${SHELL}", "envKey1": "${NotExist|defValue}", "map1": { "key": "val", "key1": "val1", "key2": "val2" }, "arr1": [ "val", "val1", "val2" ], "lang": { "dir": "res/lang", "defLang": "en", "allowed": { "en": "val", "zh-CN": "val2" } } }
Usage please see example(more example please see examples folder in the lib):
Index ¶
- Constants
- Variables
- func AddDriver(driver Driver)
- func BindStruct(key string, dst interface{}) error
- func Bool(key string, defVal ...bool) bool
- func ClearAll()
- func Data() map[string]interface{}
- func Delimiter(sep byte) func(*Options)
- func DumpTo(out io.Writer, format string) (int64, error)
- func EnableCache(opts *Options)
- func Exists(key string, findByPath ...bool) bool
- func Float(key string, defVal ...float64) float64
- func Get(key string, findByPath ...bool) interface{}
- func GetEnv(name string, defVal ...string) (val string)
- func GetValue(key string, findByPath ...bool) (interface{}, bool)
- func Getenv(name string, defVal ...string) (val string)
- func Int(key string, defVal ...int) int
- func Int64(key string, defVal ...int64) int64
- func IntMap(key string) map[string]int
- func Ints(key string) []int
- func LoadData(dataSource ...interface{}) error
- func LoadDir(dir, suffix string) error
- func LoadExists(sourceFiles ...string) error
- func LoadExistsByFormat(format string, sourceFiles ...string) error
- func LoadFiles(sourceFiles ...string) error
- func LoadFilesByFormat(format string, sourceFiles ...string) error
- func LoadFlags(keys []string) error
- func LoadOSEnv(keys []string, keyToLower bool)
- func LoadRemote(format, url string) error
- func LoadSources(format string, src []byte, more ...[]byte) error
- func LoadStrings(format string, str string, more ...string) error
- func MapOnExists(key string, dst interface{}) error
- func MapStruct(key string, dst interface{}) error
- func OnConfigChange(run func(in fsnotify.Event))
- func ParseEnv(opts *Options)
- func ParseEnvVarStringHookFunc() mapstructure.DecodeHookFunc
- func Readonly(opts *Options)
- func Set(key string, val interface{}, setByPath ...bool) error
- func SetData(data map[string]interface{})
- func SetDecoder(format string, decoder Decoder)deprecated
- func SetEncoder(format string, encoder Encoder)deprecated
- func String(key string, defVal ...string) string
- func StringMap(key string) map[string]string
- func Strings(key string) []string
- func Uint(key string, defVal ...uint) uint
- func WatchConfig(suffix string)
- func WithHookFunc(fn HookFunc) func(*Options)
- func WithOptions(opts ...func(*Options))
- func WriteTo(out io.Writer) (int64, error)
- type Config
- func (c *Config) AddDriver(driver Driver)
- func (c *Config) BindStruct(key string, dst interface{}) error
- func (c *Config) Bool(key string, defVal ...bool) (value bool)
- func (c *Config) ClearAll()
- func (c *Config) ClearCaches()
- func (c *Config) ClearData()
- func (c *Config) ConfigDir() []string
- func (c *Config) Data() map[string]interface{}
- func (c *Config) DelDriver(format string)
- func (c *Config) DumpTo(out io.Writer, format string) (n int64, err error)
- func (c *Config) Error() error
- func (c *Config) Exists(key string, findByPath ...bool) (ok bool)
- func (c *Config) Float(key string, defVal ...float64) (value float64)
- func (c *Config) Get(key string, findByPath ...bool) interface{}
- func (c *Config) GetValue(key string, findByPath ...bool) (value interface{}, ok bool)
- func (c *Config) HasDecoder(format string) bool
- func (c *Config) HasEncoder(format string) bool
- func (c *Config) Int(key string, defVal ...int) (value int)
- func (c *Config) Int64(key string, defVal ...int64) (value int64)
- func (c *Config) IntMap(key string) (mp map[string]int)
- func (c *Config) Ints(key string) (arr []int)
- func (c *Config) IsEmpty() bool
- func (c *Config) LoadData(dataSources ...interface{}) (err error)
- func (c *Config) LoadDir(dir, suffix string) (err error)
- func (c *Config) LoadExists(sourceFiles ...string) (err error)
- func (c *Config) LoadExistsByFormat(format string, sourceFiles ...string) (err error)
- func (c *Config) LoadFiles(sourceFiles ...string) (err error)
- func (c *Config) LoadFilesByFormat(format string, sourceFiles ...string) (err error)
- func (c *Config) LoadFlags(keys []string) (err error)
- func (c *Config) LoadOSEnv(keys []string, keyToLower bool)
- func (c *Config) LoadRemote(format, url string) (err error)
- func (c *Config) LoadSources(format string, src []byte, more ...[]byte) (err error)
- func (c *Config) LoadStrings(format string, str string, more ...string) (err error)
- func (c *Config) LoadedFiles() []string
- func (c *Config) MapOnExists(key string, dst interface{}) error
- func (c *Config) MapStruct(key string, dst interface{}) error
- func (c *Config) Name() string
- func (v *Config) OnConfigChange(run func(in fsnotify.Event))
- func (c *Config) Options() *Options
- func (c *Config) Readonly()
- func (c *Config) Set(key string, val interface{}, setByPath ...bool) (err error)
- func (c *Config) SetData(data map[string]interface{})
- func (c *Config) SetDecoder(format string, decoder Decoder)deprecated
- func (c *Config) SetDecoders(decoders map[string]Decoder)deprecated
- func (c *Config) SetEncoder(format string, encoder Encoder)deprecated
- func (c *Config) SetEncoders(encoders map[string]Encoder)deprecated
- func (c *Config) String(key string, defVal ...string) string
- func (c *Config) StringMap(key string) (mp map[string]string)
- func (c *Config) Strings(key string) (arr []string)
- func (c *Config) Structure(key string, dst interface{}) error
- func (c *Config) ToJSON() string
- func (c *Config) Uint(key string, defVal ...uint) (value uint)
- func (c *Config) WatchConfig(suffix string)
- func (c *Config) With(fn func(c *Config)) *Config
- func (c *Config) WithOptions(opts ...func(*Options)) *Config
- func (c *Config) WriteTo(out io.Writer) (n int64, err error)
- type Decoder
- type Driver
- type Encoder
- type HookFunc
- type Options
Constants ¶
const ( Ini = "ini" Hcl = "hcl" Yml = "yml" JSON = "json" Yaml = "yaml" Toml = "toml" )
There are supported config format
const ( OnSetValue = "set.value" OnSetData = "set.data" OnLoadData = "load.data" OnCleanData = "clean.data" )
there are some event names for config data changed.
Variables ¶
var JSONAllowComments = true
JSONAllowComments support write comments on json file.
var JSONDriver = &jsonDriver{name: JSON}
JSONDriver instance fot json
Functions ¶
func AddDriver ¶
func AddDriver(driver Driver)
AddDriver set a decoder and encoder driver for a format.
func BindStruct ¶
BindStruct alias method of the 'Structure'
func Get ¶
Get config value by key string, support get sub-value by key path(eg. 'map.key'), ok is true, find value from config ok is false, not found or error
func Getenv ¶
Getenv get os ENV value by name. like os.Getenv, but support default value Notice: - Key is not case-sensitive when getting
func LoadExists ¶
LoadExists load one or multi files, will ignore not exist
func LoadExistsByFormat ¶
LoadExistsByFormat load one or multi files by give format
func LoadFilesByFormat ¶
LoadFilesByFormat load one or multi files by give format
func LoadRemote ¶
LoadRemote load config data from remote URL.
func LoadSources ¶
LoadSources load one or multi byte data
func LoadStrings ¶
LoadStrings load one or multi string
func MapOnExists ¶ added in v2.7.1
MapOnExists mapping data to the dst structure only on key exists.
func MapStruct ¶
MapStruct alias method of the 'Structure'
Usage:
dbInfo := &Db{} config.MapStruct("db", dbInfo)
func OnConfigChange ¶ added in v2.5.2
func ParseEnvVarStringHookFunc ¶
func ParseEnvVarStringHookFunc() mapstructure.DecodeHookFunc
ParseEnvVarStringHookFunc returns a DecodeHookFunc that parse ENV var
func SetDecoder
deprecated
func SetEncoder
deprecated
func WatchConfig ¶ added in v2.5.2
func WatchConfig(suffix string)
func WithHookFunc ¶ added in v2.7.1
WithHookFunc set hook func
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config structure definition
func NewWithOptions ¶
NewWithOptions config instance
func (*Config) BindStruct ¶
BindStruct alias method of the 'Structure'
func (*Config) Bool ¶
Bool looks up a value for a key in this section and attempts to parse that value as a boolean, along with a boolean result similar to a map lookup. of following(case insensitive):
- true
- yes
- false
- no
- 1
- 0
The `ok` boolean will be false in the event that the value could not be parsed as a bool
func (*Config) LoadData ¶
LoadData load data from map OR struct
The dataSources can be:
- map[string]interface{}
func (*Config) LoadExists ¶
LoadExists load and parse config files, but will ignore not exists file.
func (*Config) LoadExistsByFormat ¶
LoadExistsByFormat load one or multi files by give format
func (*Config) LoadFilesByFormat ¶
LoadFilesByFormat load one or multi files by give format
func (*Config) LoadFlags ¶
LoadFlags parse command line arguments, based on provide keys. Usage:
// debug flag is bool type c.LoadFlags([]string{"env", "debug:bool"})
func (*Config) LoadRemote ¶
LoadRemote load config data from remote URL.
Usage:
c.LoadRemote(config.JSON, "http://abc.com/api-config.json")
func (*Config) LoadSources ¶
LoadSources load data from byte content.
Usage:
config.LoadSources(config.Yml, []byte(` name: blog arr: key: val
`))
func (*Config) LoadStrings ¶
LoadStrings load data from source string content.
func (*Config) LoadedFiles ¶
LoadedFiles get loaded files name
func (*Config) MapOnExists ¶ added in v2.7.1
MapOnExists mapping data to the dst structure only on key exists.
func (*Config) OnConfigChange ¶ added in v2.6.1
func (*Config) Readonly ¶
func (c *Config) Readonly()
Readonly disable set data to config.
Usage:
config.LoadFiles(a, b, c) config.Readonly()
func (*Config) SetDecoder
deprecated
func (*Config) SetDecoders
deprecated
func (*Config) SetEncoder
deprecated
func (*Config) SetEncoders
deprecated
func (*Config) Structure ¶
Structure get config data and binding to the dst structure.
Usage:
dbInfo := Db{} config.Structure("db", &dbInfo)
func (*Config) WithOptions ¶
WithOptions apply some options
type Options ¶
type Options struct { // parse env value. like: "${EnvName}" "${EnvName|default}" ParseEnv bool // config is readonly Readonly bool // enable config data cache EnableCache bool // parse key, allow find value by key path. eg: 'key.sub' will find `map[key]sub` ParseKey bool // tag name for binding data to struct // Deprecated: please set tag name by DecoderConfig TagName string // the delimiter char for split key path, if `FindByPath=true`. default is '.' Delimiter byte // default write format DumpFormat string // default input format ReadFormat string // DecoderConfig setting for binding data to struct DecoderConfig *mapstructure.DecoderConfig // HookFunc on data changed. HookFunc HookFunc }
Options config options
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package dotnev provide load .env data to os ENV
|
Package dotnev provide load .env data to os ENV |
Package hcl is driver use HCL format content as config source
|
Package hcl is driver use HCL format content as config source |
Package hclv2 is driver use HCL format content as config source
|
Package hclv2 is driver use HCL format content as config source |
Package ini is driver use INI format content as config source
|
Package ini is driver use INI format content as config source |
Package json use the https://github.com/json-iterator/go for parse json
|
Package json use the https://github.com/json-iterator/go for parse json |
Package json5 use the https://github.com/yosuke-furukawa/json5 for parse json5
|
Package json5 use the https://github.com/yosuke-furukawa/json5 for parse json5 |
Package other is an example of a custom driver
|
Package other is an example of a custom driver |
Package toml is driver use TOML format content as config source
|
Package toml is driver use TOML format content as config source |
Package yaml is a driver use YAML format content as config source
|
Package yaml is a driver use YAML format content as config source |
Package yaml is a driver use YAML format content as config source
|
Package yaml is a driver use YAML format content as config source |