ini

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2018 License: MIT Imports: 12 Imported by: 3

README

ini

GoDoc

ini parse by golang. ini config data manage

parse content is ref the project: https://github.com/dombenson/go-ini, Thank you very much

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
- 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

License

MIT

Documentation

Index

Constants

View Source
const (
	DefSection = "__default"
)

Variables

View Source
var DefOptions = Options{ParseEnv: true}

DefOptions

Functions

func Decode

func Decode(blob []byte, v interface{}) (err error)

Decode

func Encode

func Encode(v interface{}) (out []byte, err error)

Encode

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 New

func New() *Ini

New

func NewWithOptions

func NewWithOptions(opts Options) *Ini

NewWithOptions

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) 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) HasSection

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

HasSection

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) MergeData

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

MergeData

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) ReadFrom

func (ini *Ini) ReadFrom(in io.Reader) (n int64, err error)

ReadFrom Loads INI data from a reader and stores the data in the manager.

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) SetOptions

func (ini *Ini) SetOptions(opts Options)

SetOptions

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) 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 {
	ParseEnv   bool
	IgnoreCase bool
}

Options

type Section

type Section map[string]string

section in ini data

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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