ini

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2018 License: MIT Imports: 10 Imported by: 3

README

ini

GoDoc

ini parse by golang. ini config data manage

  • easy to use
  • support multi file,data load
  • support data override merge
  • support parse ENV key

Godoc

Usage

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

; comments
[sec1]
key = val0
some = value
stuff = things
  • usage
package main

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

// 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)
	}

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

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

	bv, ok := config.GetBool("debug")
	fmt.Printf("get bool\n - ok: %v, val: %v\n", ok, bv)

	name, ok := config.GetString("name")
	fmt.Printf("get string\n - ok: %v, val: %v\n", ok, name)

	sec1, ok := config.GetSection("sec1")
	fmt.Printf("get section\n - ok: %v, val: %#v\n", ok, sec1)

	str, ok := config.GetString("sec1.key")
	fmt.Printf("get sub-value by path 'section.key'\n - ok: %v, val: %s\n", ok, str)

	// can parse env name(ParseEnv: true)
	fmt.Printf("get env 'envKey' val: %s\n", config.MustString("shell"))
	fmt.Printf("get env 'envKey1' val: %s\n", config.MustString("noEnv"))

	// set value
	config.Set("name", "new name")
	name, ok = config.GetString("name")
	fmt.Printf("set string\n - ok: %v, val: %v\n", ok, name)

	// export data to file
	// _, err = config.WriteToFile("testdata/export.ini")
	// if err != nil {
	// 	panic(err)
	// }
}
  • output(by go run ./examples/demo.go)
get int
 - ok: true, val: 100
get bool
 - ok: true, val: true
get string
 - ok: true, val: inhere
get section
 - ok: true, val: map[string]string{"key":"val0", "some":"change val", "stuff":"things", "newK":"newVal"}
get sub-value by path 'section.key'
 - ok: true, val: val0
get env 'envKey' val: /bin/zsh
get env 'envKey1' val: defValue
set string
 - ok: true, val: new name

Ref

License

MIT

Documentation

Overview

Ini parse by golang. ini config file/data manage

Source code and other details for the project are available at GitHub:

https://github.com/gookit/ini

INI parser is: https://github.com/gookit/ini/parser

Example
// config, err := LoadFiles("testdata/tesdt.ini")
// LoadExists will ignore not exists file
config, err := 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())

iv, ok := config.GetInt("age")
fmt.Printf("get int\n - ok: %v, val: %v\n", ok, iv)

bv, ok := config.GetBool("debug")
fmt.Printf("get bool\n - ok: %v, val: %v\n", ok, bv)

name, ok := config.GetString("name")
fmt.Printf("get string\n - ok: %v, val: %v\n", ok, name)

sec1, ok := config.GetSection("sec1")
fmt.Printf("get section\n - ok: %v, val: %#v\n", ok, sec1)

str, ok := config.GetString("sec1.key")
fmt.Printf("get sub-value by path 'section.key'\n - ok: %v, val: %s\n", ok, str)

// can parse env name(ParseEnv: true)
fmt.Printf("get env 'envKey' val: %s\n", config.MustString("shell"))
fmt.Printf("get env 'envKey1' val: %s\n", config.MustString("noEnv"))

// set value
config.Set("name", "new name")
name, ok = config.GetString("name")
fmt.Printf("set string\n - ok: %v, val: %v\n", ok, name)

// export data to file
// _, err = config.WriteToFile("testdata/export.ini")
// if err != nil {
// 	panic(err)
// }

// Out:
// get int
// - ok: true, val: 100
// get bool
// - ok: true, val: true
// get string
// - ok: true, val: inhere
// get section
// - ok: true, val: map[string]string{"stuff":"things", "newK":"newVal", "key":"val0", "some":"change val"}
// get sub-value by path 'section.key'
// - ok: true, val: val0
// get env 'envKey' val: /bin/zsh
// get env 'envKey1' val: defValue
// set string
// - ok: true, val: new name

Index

Examples

Constants

View Source
const (
	SepSection = "."
	DefSection = "__default"
)

Variables

View Source
var DefOptions = &Options{ParseEnv: true}

DefOptions

Functions

func IgnoreCase added in v1.0.1

func IgnoreCase(opts *Options)

IgnoreCase

func ParseEnv added in v1.0.1

func ParseEnv(opts *Options)

ParseEnv usage: ini.NewWithOptions(ini.ParseEnv)

func Readonly added in v1.0.1

func Readonly(opts *Options)

Readonly usage: ini.NewWithOptions(ini.Readonly)

Types

type Ini

type Ini struct {
	// contains filtered or unexported fields
}

Ini data manager

func LoadExists

func LoadExists(files ...string) (ini *Ini, err error)

LoadExists

func LoadFiles

func LoadFiles(files ...string) (ini *Ini, err error)

LoadFiles

func LoadStrings added in v1.0.1

func LoadStrings(strings ...string) (ini *Ini, err error)

LoadStrings

func New

func New() *Ini

New

func NewWithOptions

func NewWithOptions(opts ...func(*Options)) *Ini

NewWithOptions usage: ini.NewWithOptions(ini.ParseEnv, ini.Readonly)

func (*Ini) AddSection

func (ini *Ini) AddSection(name string, values map[string]string)

AddSection

func (*Ini) Data

func (ini *Ini) Data() map[string]Section

Data get all data

func (*Ini) DefBool

func (ini *Ini) DefBool(key string, def bool) bool

DefBool get a bool value, if not found return default value

func (*Ini) DefInt

func (ini *Ini) DefInt(key string, def int) (val int)

DefInt get a int value, if not found return default value

func (*Ini) DefString

func (ini *Ini) DefString(key string, def string) string

DefString get a string value, if not found return default value

func (*Ini) DelKey added in v1.0.2

func (ini *Ini) DelKey(key string) (ok bool)

DelKey

func (*Ini) DelSection added in v1.0.2

func (ini *Ini) DelSection(name string) bool

DelSection

func (*Ini) Get

func (ini *Ini) Get(key string) (val string, ok bool)

Get a value by key string. you can use '.' split for get value in a special section

func (*Ini) GetBool

func (ini *Ini) GetBool(key string) (value bool, ok bool)

GetBool 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
  • false
  • yes
  • no
  • off
  • on
  • 0
  • 1

The `ok` boolean will be false in the event that the value could not be parsed as a bool

func (*Ini) GetInt

func (ini *Ini) GetInt(key string) (val int, ok bool)

GetInt get a int value

func (*Ini) GetSection

func (ini *Ini) GetSection(name string) (sec map[string]string, ok bool)

GetSection

func (*Ini) GetString

func (ini *Ini) GetString(key string) (val string, ok bool)

GetString like Get method, but will parse ENV value.

func (*Ini) GetStringMap added in v1.0.1

func (ini *Ini) GetStringMap(name string) (mp map[string]string, ok bool)

GetStringMap

func (*Ini) HasSection

func (ini *Ini) HasSection(name string) bool

HasSection

func (*Ini) LoadData added in v1.0.2

func (ini *Ini) LoadData(data map[string]Section)

LoadData

func (*Ini) LoadExists

func (ini *Ini) LoadExists(files ...string) (err error)

LoadExists

func (*Ini) LoadFiles

func (ini *Ini) LoadFiles(files ...string) (err error)

LoadFiles

func (*Ini) LoadStrings

func (ini *Ini) LoadStrings(strings ...string) (err error)

LoadStrings

func (*Ini) MustBool

func (ini *Ini) MustBool(key string) bool

MustBool get a string value, if not found return false

func (*Ini) MustInt

func (ini *Ini) MustInt(key string) int

MustInt get a int value, if not found return 0

func (*Ini) MustString

func (ini *Ini) MustString(key string) string

MustString get a string value, if not found return empty string

func (*Ini) Options added in v1.0.2

func (ini *Ini) Options() *Options

Options

func (*Ini) PrettyJson added in v1.0.2

func (ini *Ini) PrettyJson() string

WriteToFile get pretty Json string

func (*Ini) Reset

func (ini *Ini) Reset()

Reset all data

func (*Ini) Set

func (ini *Ini) Set(key, val string, section ...string)

Set a value to the section by key. if section is empty, will set to default section

func (*Ini) SetBool

func (ini *Ini) SetBool(key string, val bool, section ...string)

SetBool

func (*Ini) SetInt

func (ini *Ini) SetInt(key string, val int, section ...string)

SetInt

func (*Ini) SetSection

func (ini *Ini) SetSection(name string, values map[string]string)

SetSection

func (*Ini) SetString

func (ini *Ini) SetString(key, val string, section ...string)

SetString

func (*Ini) String added in v1.0.2

func (ini *Ini) String() string

String

func (*Ini) WithOptions added in v1.0.1

func (ini *Ini) WithOptions(opts ...func(*Options))

WithOptions

func (*Ini) WriteTo

func (ini *Ini) WriteTo(out io.Writer) (n int64, err error)

Write out an INI File representing the current state to a writer.

func (*Ini) WriteToFile

func (ini *Ini) WriteToFile(file string) (n int64, err error)

WriteToFile

type Options

type Options struct {
	Readonly   bool
	ParseEnv   bool
	IgnoreCase bool
}

Options

type Section

type Section map[string]string

section in ini data

Directories

Path Synopsis
This a parser for parse INI format content to golang data There are example data: # comments name = inhere age = 28 debug = true hasQuota1 = 'this is val' hasQuota2 = "this is val1" shell = ${SHELL} noEnv = ${NotExist|defValue} ; array in def section tags[] = a tags[] = b tags[] = c ; comments [sec1] key = val0 some = value stuff = things ; array in section types[] = x types[] = y how to use, please see examples:
This a parser for parse INI format content to golang data There are example data: # comments name = inhere age = 28 debug = true hasQuota1 = 'this is val' hasQuota2 = "this is val1" shell = ${SHELL} noEnv = ${NotExist|defValue} ; array in def section tags[] = a tags[] = b tags[] = c ; comments [sec1] key = val0 some = value stuff = things ; array in section types[] = x types[] = y how to use, please see examples:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL