ini
is a simple Go package for manipulating ini files.
ini
is mostly a simple wrapper around the ini/parser
package
also contained in this repository. ini/parser
was implemented by generating a
Pigeon parser from a PEG grammar.
With the correct configuration, the ini
package is able to read git
config files, very simple TOML files, and Java
Properties files.
Prior to writing this package, a number of existing Go ini packages/parsers were investigated. The packages available at the time did not possess the complete feature set needed: specifically, the available packages did not work well with badly formatted files (and their parsers were not easily fixable), they would erase any comments/spacing when writing out a modified ini file, and they were not written in idiomatic Go.
As such, it was necessary to author a new package that could work with a variety of badly formatted ini files, in idiomatic Go, and provide a simple interface to reading/writing/manipulating ini files.
Install in the usual Go fashion:
go get -u github.com/kenshaw/ini
ini
can be used similarly to the following:
// _examples/test2/main.go
package main
import (
"fmt"
"log"
"github.com/kenshaw/ini"
)
var (
data = `
firstkey = one
[some section]
key = blah ; comment
[another section]
key = blah`
gitconfig = `
[difftool "gdmp"]
cmd = ~/gdmp/x "$LOCAL" "$REMOTE"
`
)
func main() {
f, err := ini.LoadString(data)
if err != nil {
log.Fatal(err)
}
s := f.GetSection("some section")
fmt.Printf("some section.key: %s\n", s.Get("key"))
s.SetKey("key2", "another value")
f.Write("out.ini")
// create a gitconfig parser
g, err := ini.LoadString(gitconfig)
if err != nil {
log.Fatal(err)
}
// setup gitconfig name/key manipulation functions
g.SectionManipFunc = ini.GitSectionManipFunc
g.SectionNameFunc = ini.GitSectionNameFunc
fmt.Printf("difftool.gdmp.cmd: %s\n", g.GetKey("difftool.gdmp.cmd"))
}
Please see the GoDoc API page for a full API listing.
- convert to github.com/alecthomas/participle parser