Skip to content

kenshaw/ini

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About ini Build Status Coverage

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.

Why Another ini Package?

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.

Installing

Install in the usual Go fashion:

go get -u github.com/kenshaw/ini

Using

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.

TODO

  • convert to github.com/alecthomas/participle parser

About

Package ini provides ini file parsing and manipulation for Go.

Resources

License

Stars

Watchers

Forks

Packages

No packages published