Thanks to visit codestin.com
Credit goes to github.com

Skip to content
/ config Public
forked from gookit/config

Go config manage(load,get,set). support JSON, YAML, TOML, INI, HCL and Flags. multi file load, data override merge, parse ENV var. Go应用配置加载管理,支持多种格式,多文件加载,远程文件加载,支持数据合并,解析环境变量名

License

Notifications You must be signed in to change notification settings

liamyue/config

 
 

Repository files navigation

config

GoDoc Build Status Coverage Status Go Report Card

Golang application config manage tool library.

中文说明

  • support multi format: JSON(default), INI, YAML, TOML, HCL
    • JSON content support comments. will auto clear comments
  • support multi file/data load
  • support for loading configuration data from remote URLs
  • support for setting configuration data from command line arguments
  • support data overlay and merge, automatically load by key when loading multiple copies of data
  • support get sub value by path, like map.key arr.2
  • support parse ENV name. like envKey: ${SHELL} -> envKey: /bin/zsh
  • generic api Get Int String Bool Ints IntMap Strings StringMap ...
  • complete unit test(coverage > 90%)

Only use INI

If you just want to use INI for simple config management, recommended use gookit/ini

GoDoc

Usage

Here using the yaml format as an example(testdata/yml_other.yml):

name: app2
debug: false
baseKey: value2
shell: ${SHELL}
envKey1: ${NotExist|defValue}

map1:
    key: val2
    key2: val20

arr1:
    - val1
    - val21

Load data

examples code please see _examples/yaml.go:

package main

import (
    "github.com/gookit/config"
    "github.com/gookit/config/yaml"
)

// go run ./examples/yaml.go
func main() {
	config.WithOptions(config.ParseEnv)
	
	// add driver for support yaml content
	config.AddDriver(yaml.Driver)
	// config.SetDecoder(config.Yaml, yaml.Decoder)

	err := config.LoadFiles("testdata/yml_base.yml")
	if err != nil {
		panic(err)
	}

	// fmt.Printf("config data: \n %#v\n", config.Data())

	// load more files
	err = config.LoadFiles("testdata/yml_other.yml")
	// can also load multi at once
	// err := config.LoadFiles("testdata/yml_base.yml", "testdata/yml_other.yml")
	if err != nil {
		panic(err)
	}
}

Read data

  • get integer
age, ok := config.Int("age")
fmt.Print(ok, age) // true 100
  • Get bool
val, ok := config.Bool("debug")
fmt.Print(ok, age) // true true
  • Get string
name, ok := config.String("name")
fmt.Print(ok, name) // true inhere
  • Get strings(slice)
arr1, ok := config.Strings("arr1")
fmt.Printf("%v %#v", ok, arr1) // true []string{"val1", "val21"}
  • Get string map
val, ok := config.StringMap("map1")
fmt.Printf("%v %#v",ok, val) // true map[string]string{"key":"val2", "key2":"val20"}
  • Value contains ENV var
value, ok := config.String("shell")
fmt.Print(ok, value) // true /bin/zsh
  • Get value by key path
// from array
value, ok := config.String("arr1.0")
fmt.Print(ok, value) // true "val1"

// from map
value, ok := config.String("map1.key")
fmt.Print(ok, value) // true "val2"
  • Setting new value
// set value
config.Set("name", "new name")
name, ok = config.String("name")
fmt.Print(ok, name) // true "new name"

API Methods Refer

Load Config

  • LoadData(dataSource ...interface{}) (err error)
  • LoadFlags(keys []string) (err error)
  • LoadExists(sourceFiles ...string) (err error)
  • LoadFiles(sourceFiles ...string) (err error)
  • LoadRemote(format, url string) (err error)
  • LoadSources(format string, src []byte, more ...[]byte) (err error)
  • LoadStrings(format string, str string, more ...string) (err error)

Getting Values

  • Bool(key string) (value bool, ok bool)
  • Int(key string) (value int, ok bool)
  • Int64(key string) (value int64, ok bool)
  • Ints(key string) (arr []int, ok bool)
  • IntMap(key string) (mp map[string]int, ok bool)
  • Float(key string) (value float64, ok bool)
  • String(key string) (value string, ok bool)
  • Strings(key string) (arr []string, ok bool)
  • StringMap(key string) (mp map[string]string, ok bool)
  • Get(key string, findByPath ...bool) (value interface{}, ok bool)

Setting Values

  • Set(key string, val interface{}, setByPath ...bool) (err error)

Run Tests

go test -cover
// contains all sub-folder
go test -cover ./...

Related Packages

Ini Config Use

License

MIT

About

Go config manage(load,get,set). support JSON, YAML, TOML, INI, HCL and Flags. multi file load, data override merge, parse ENV var. Go应用配置加载管理,支持多种格式,多文件加载,远程文件加载,支持数据合并,解析环境变量名

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%