ini

package module
v0.0.0-...-a8267f5 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2017 License: MIT Imports: 12 Imported by: 5

README

Ini

GoDoc Build Status Coverage Status Go Report Card MIT licensed

Ini is a Go package for for parsing ini (or cfg) files. See Godoc for the API.

Stable

The api is now stable and will remain stable until version 2 is released. To prove how stable ini.Parse is, it battled go-fuzz for 10 hours, processing over 1.1 billion randomized inputs and won:

2015/07/21 23:58:54 slaves: 8, corpus: 611 (3h8m ago), crashers: 0, restarts: 1/
10000, execs: 1100121441 (30462/sec), cover: 493, uptime: 10h1m

Installation

Run the following line to install.

$ go get github.com/Thomasdezeeuw/ini

Examples

See the _examples directory for a multiple examples.

License

Licensed under the MIT license, copyright (C) Thomas de Zeeuw. See LICENSE for more information.

Documentation

Overview

Package ini parses an ini formatted file. The parsed configuration can be decoded into a variable or used directly.

Index

Examples

Constants

View Source
const Global = ""

Global is the section name for key-values not under a section. It is used for retrieving key-value pairs in the "global" section, or not under any other section.

value := config[ini.Global]["key"]
value2, found := config[ini.Global]["key2"]

Variables

This section is empty.

Functions

func Decode

func Decode(r io.Reader, dst interface{}) error

Decode decodes a configuration into a struct or map, see `Config.Decode`.

func DecodeValue

func DecodeValue(value string, dst interface{}) error

DecodeValue decodes a single configuration value into a variable.

func IsCovertionError

func IsCovertionError(err error) bool

IsCovertionError checks if an error is a covertion error.

func IsOverflowError

func IsOverflowError(err error) bool

IsOverflowError checks if an error is an overflow error.

func IsSyntaxError

func IsSyntaxError(err error) bool

IsSyntaxError checks if an error is a syntax error.

Types

type Config

type Config map[string]Section

Config holds all key-value pairs under sections. To retrieve a key-value pair you can use:

value := config["section"]["key"]
value2, found := config["section"]["key2"]

The same as you would for a map. For retrieving key-value pairs not in any section, or the "global" section, you can use the `ini.Global` constant, like so:

value := config[ini.Global]["key"]
value2, found := config[ini.Global]["key2"]

func Parse

func Parse(r io.Reader) (Config, error)

Parse parses ini formatted input.

Note: the reader already gets buffered, so there is no need to buffer it yourself.

Example
r := strings.NewReader("key = value")

conf, err := Parse(r)
if err != nil {
	panic(err)
}

value := conf[Global]["key"]
fmt.Println("value:", value)

value, found := conf[Global]["unknown"]
fmt.Println("found:", found)
fmt.Println("value:", value)
Output:

value: value
found: false
value:
Example (Section)
confFile := "key = value\n" +
	"[section1]\n" +
	"key = value2"
r := strings.NewReader(confFile)

conf, err := Parse(r)
if err != nil {
	panic(err)
}

value := conf[Global]["key"]
value2 := conf["section1"]["key"]
fmt.Println("value:", value)
fmt.Println("value2:", value2)
Output:

value: value
value2: value2

func (*Config) Bytes

func (c *Config) Bytes() []byte

Bytes returns an ini formatted configuration, ready to be written to a file.

func (*Config) Decode

func (c *Config) Decode(dst interface{}) error

Decode decodes a configuration into a struct. Any properties to be set need to be public. Keys are renamed, whitespace is removed and keys start with a capaital, like so:

"my key" -> "MyKey"

Tags are supported to define the key of a configuration. See below for a small example, for more examples see the tags example in the _examples directory.

struct {
	AppName `ini:"name"`
}

Slices are supported by using a comma separated list, like so:

"string1, string2" -> []string{"string1", "string2"}
"1, 2, 3" -> []int{1, 2, 3}

Booleans are supported as well:

"1, t, T, TRUE, true, True" -> true
"0, f, F, FALSE, false, False" -> false

Time is supported with the following formats:

"2006-01-02", "2006-01-02 15:04", "2006-01-02 15:04:05", time.RFC3339,
time.RFC1123 and time.RFC822

Duration is also supported, see `time.ParseDuration` for the documentation.

Note: underneath Decode uses the reflect package which isn't great for performance, so use it with care.

func (*Config) String

func (c *Config) String() string

String returns an ini formatted configuration, ready to be written to a file.

func (*Config) WriteTo

func (c *Config) WriteTo(w io.Writer) (int64, error)

WriteTo writes the configuration to the writer in the ini format.

type Section

type Section map[string]string

Section holds the key value pairs inside a configuration.

section := config["section"]
value, found := section["key"]

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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