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

Skip to content
/ ini Public

📝 Go INI config management. support multi file load, data override merge. parse ENV variable, parse variable reference. Dotenv file parse and loader. INI配置读取管理,支持多文件加载,数据覆盖合并, 解析ENV变量, 解析变量引用。DotEnv 解析加载

License

Notifications You must be signed in to change notification settings

gookit/ini

Repository files navigation

ini

GoDoc Build Status Coverage Status Go Report Card

ini parse by golang. ini config data manage

  • easy to use(get: Int Bool String StringMap, set: SetInt SetBool SetString ...)
  • support multi file, data load
  • support data override merge
  • support parse ENV variable
  • complete unit test(coverage > 90%)
  • support variable reference, default compatible with Python's configParser format %(VAR)s

中文说明

More formats

If you want more support for file content formats, recommended use gookit/config

  • gookit/config - Support multi formats: JSON(default), INI, YAML, TOML, HCL

GoDoc

Usage

  • example data(testdata/test.ini):
# comments
name = inhere
age = 50
debug = true
hasQuota1 = 'this is val'
hasQuota2 = "this is val1"
can2arr = val0,val1,val2
shell = ${SHELL}
noEnv = ${NotExist|defValue}
nkey = val in default section

; comments
[sec1]
key = val0
some = value
stuff = things
varRef = %(nkey)s

Load data

package main

import (
	"github.com/gookit/ini"
)

// go run ./examples/demo.go
func main() {
	// config, err := ini.LoadFiles("testdata/tesdt.ini")
	// LoadExists will ignore not exists file
	config, err := ini.LoadExists("testdata/test.ini", "not-exist.ini")
	if err != nil {
		panic(err)
	}

	// load more, will override prev data by key
	config.LoadStrings(`
age = 100
[sec1]
newK = newVal
some = change val
`)
	// fmt.Printf("%v\n", config.Data())
}

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 section data(string map)
val, ok := config.StringMap("sec1")
fmt.Println(ok, val) 
// true map[string]string{"key":"val0", "some":"change val", "stuff":"things", "newK":"newVal"}
  • value is ENV var
value, ok := config.String("shell")
fmt.Printf("%v %q", ok, value)  // true "/bin/zsh"
  • get value by key path
value, ok := config.String("sec1.key")
fmt.Print(ok, value) // true val0
  • use var refer
value, ok := config.String("sec1.varRef")
fmt.Printf("%v %q", ok, value) // true "val in default section"
  • setting new value
// set value
config.Set("name", "new name")
name, ok = config.String("name")
fmt.Printf("%v %q", ok, value) // true "new name"

Variable reference resolution

[portal] 
url = http://%(host)s:%(port)s/api
host = localhost 
port = 8080

If variable resolution is enabled,will parse %(host)s and replace it:

cfg := ini.New()
// enable ParseVar
cfg.WithOptions(ini.ParseVar)

fmt.Print(cfg.MustString("portal.url"))
// OUT: 
// http://localhost:8080/api 

Available options

type Options struct {
	// set to read-only mode. default False
	Readonly bool
	// parse ENV var name. default True
	ParseEnv bool
	// parse variable reference "%(varName)s". default False
	ParseVar bool

	// var left open char. default "%("
	VarOpen string
	// var right close char. default ")s"
	VarClose string

	// ignore key name case. default False
	IgnoreCase bool
	// default section name. default "__default"
	DefSection string
	// sep char for split key path. default ".", use like "section.subKey"
	SectionSep string
}
  • setting options
cfg := ini.New()
cfg.WithOptions(ini.ParseEnv,ini.ParseVar, func (opts *Options) {
	opts.SectionSep = ":"
	opts.DefSection = "default"
})

Tests

  • go tests with cover
go test ./... -cover
  • run lint by GoLint
golint ./...

Refer

License

MIT

About

📝 Go INI config management. support multi file load, data override merge. parse ENV variable, parse variable reference. Dotenv file parse and loader. INI配置读取管理,支持多文件加载,数据覆盖合并, 解析ENV变量, 解析变量引用。DotEnv 解析加载

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 6

Languages