All Projects → gookit → ini

gookit / ini

Licence: MIT license
📝 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 解析加载

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to ini

libconfini
Yet another INI parser
Stars: ✭ 106 (+47.22%)
Mutual labels:  config, configuration-management, ini, ini-parser, ini-config
exenv
Exenv makes loading environment variables from external sources easy.
Stars: ✭ 35 (-51.39%)
Mutual labels:  config, dotenv, environment-variables, env
parse it
A python library for parsing multiple types of config files, envvars & command line arguments that takes the headache out of setting app configurations.
Stars: ✭ 86 (+19.44%)
Mutual labels:  config, environment-variables, configuration-management, ini
superconfig
Access environment variables. Also includes presence validation, type coercion and default values.
Stars: ✭ 33 (-54.17%)
Mutual labels:  config, dotenv, environment-variables, configuration-management
dotenvy
Speed up your production sites by ditching .env for key/value variable pairs as Apache, Nginx, and shell equivalents
Stars: ✭ 31 (-56.94%)
Mutual labels:  dotenv, environment, environment-variables, env
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+2476.39%)
Mutual labels:  config, environment-variables, configuration-management, env
ts-dotenv
Strongly-typed environment variables for Node.js
Stars: ✭ 18 (-75%)
Mutual labels:  dotenv, environment, environment-variables, env
read-env
🔧 Transform environment variables into JSON object with sanitized values.
Stars: ✭ 60 (-16.67%)
Mutual labels:  config, environment-variables, env
climatecontrol
Python library for loading settings and config data from files and environment variables
Stars: ✭ 20 (-72.22%)
Mutual labels:  config, environment-variables, configuration-management
salak.rs
A multi layered configuration loader and zero-boilerplate configuration parser.
Stars: ✭ 27 (-62.5%)
Mutual labels:  config, environment, env
dotfiles
My personal app/env configs and dotfiles.
Stars: ✭ 27 (-62.5%)
Mutual labels:  config, environment, configuration-management
goodconf
Transparently load variables from environment or JSON/YAML file.
Stars: ✭ 80 (+11.11%)
Mutual labels:  config, environment-variables, env
Dynaconf
Configuration Management for Python ⚙
Stars: ✭ 2,082 (+2791.67%)
Mutual labels:  config, environment-variables, configuration-management
nest-typed-config
Intuitive, type-safe configuration module for Nest framework ✨
Stars: ✭ 47 (-34.72%)
Mutual labels:  config, dotenv, configuration-management
environment
🌳 Environment variable configuration for Node.js made easy.
Stars: ✭ 12 (-83.33%)
Mutual labels:  config, environment, env
Env
Simple lib to parse environment variables to structs
Stars: ✭ 2,164 (+2905.56%)
Mutual labels:  config, environment, environment-variables
sitri
Sitri - powerful settings & configs for python
Stars: ✭ 20 (-72.22%)
Mutual labels:  config, environment-variables, configuration-management
Phpdotenv
Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
Stars: ✭ 11,648 (+16077.78%)
Mutual labels:  dotenv, environment, environment-variables
Env Var
Verification, sanitization, and type coercion for environment variables in Node.js
Stars: ✭ 201 (+179.17%)
Mutual labels:  dotenv, environment, environment-variables
env
A lightweight package for loading OS environment variables into structs for Go projects
Stars: ✭ 24 (-66.67%)
Mutual labels:  config, environment-variables, env

INI

GitHub go.mod Go version GitHub tag (latest SemVer) Coverage Status Go Report Card Unit-Tests Go Reference

INI contents parser by Golang, INI config data management.

中文说明

Features

  • Easy to use(get: Int Int64 Bool String StringMap ..., set: Set)
  • Support multi file, data load
  • Support for decode data to struct
  • Support data override merge
  • Support parse ENV variable
  • Support comments start with ; #, multi line comments /* .. */
  • Support multi line value with """ or '''
  • Complete unit test(coverage > 90%)
  • Support variable reference, default compatible with Python's configParser format %(VAR)s

Parser

Package parser is a Parser for parse INI format content to golang data

Dotenv

Package dotenv that supports importing ENV data from files (eg .env)

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

Install

go get github.com/gookit/ini/v2

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/v2"
)

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

Mapping data to struct

type User struct {
	Name string
	Age int
}

user := &User{}
ini.MapStruct(ini.DefSection(), user)

dump.P(user)

Special, mapping all data:

ini.MapStruct("", ptr)

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 for default instance:

ini.WithOptions(ini.ParseEnv,ini.ParseVar)

Setting options with new instance:

cfg := ini.New()
cfg.WithOptions(ini.ParseEnv, ini.ParseVar, func (opts *Options) {
	opts.SectionSep = ":"
	opts.DefSection = "default"
})

Dotenv

Package dotenv that supports importing data from files (eg .env) to ENV

Usage

err := dotenv.Load("./", ".env")
// err := dotenv.LoadExists("./", ".env")

val := dotenv.Get("ENV_KEY")
// Or use 
// val := os.Getenv("ENV_KEY")

// get int value
intVal := dotenv.Int("LOG_LEVEL")

// with default value
val := dotenv.Get("ENV_KEY", "default value")

Tests

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

Gookit packages

  • gookit/ini Go config management, use INI files
  • gookit/rux Simple and fast request router for golang HTTP
  • gookit/gcli Build CLI application, tool library, running CLI commands
  • gookit/slog Lightweight, easy to extend, configurable logging library written in Go
  • gookit/color A command-line color library with true color support, universal API methods and Windows support
  • gookit/event Lightweight event manager and dispatcher implements by Go
  • gookit/cache Generic cache use and cache manager for golang. support File, Memory, Redis, Memcached.
  • gookit/config Go config management. support JSON, YAML, TOML, INI, HCL, ENV and Flags
  • gookit/filter Provide filtering, sanitizing, and conversion of golang data
  • gookit/validate Use for data validation and filtering. support Map, Struct, Form data
  • gookit/goutil Some utils for the Go: string, array/slice, map, format, cli, env, filesystem, test and more
  • More, please see https://github.com/gookit

Related

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].