Coder Social home page Coder Social logo

morontt / ini Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gookit/ini

0.0 2.0 0.0 106 KB

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

Home Page: https://godoc.org/github.com/gookit/ini

License: MIT License

Go 100.00%

ini's Introduction

ini

GoDoc Build Status Coverage Status Go Report Card

INI data parse by golang. INI config data management tool library.

中文说明

  • Easy to use(get: Int Int64 Bool String StringMap ..., set: Set)
  • 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
	err := ini.LoadExists("testdata/test.ini", "not-exist.ini")
	if err != nil {
		panic(err)
	}

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

Read data

  • Get integer
age := config.Int("age")
fmt.Print(age) // 100
  • Get bool
val := config.Bool("debug")
fmt.Print(val) // true
  • Get string
name := config.String("name")
fmt.Print(name) // inhere
  • Get section data(string map)
val := config.StringMap("sec1")
fmt.Println(val) 
// map[string]string{"key":"val0", "some":"change val", "stuff":"things", "newK":"newVal"}
  • Value is ENV var
value := config.String("shell")
fmt.Printf("%q", value)  // "/bin/zsh"
  • Get value by key path
value := config.String("sec1.key")
fmt.Print(value) // val0
  • Use var refer
value := config.String("sec1.varRef")
fmt.Printf("%q", value) // "val in default section"
  • Setting new value
// set value
config.Set("name", "new name")
name = config.String("name")
fmt.Printf("%q", value) // "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

ini's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.