ini

package module
v0.0.0-...-664e1e4 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2017 License: MIT Imports: 10 Imported by: 1

README

go-ini

INI parsing library for Go (golang).

This library supports read/write and implements io.ReadWriteCloser, io.ReadFrom and io.WriteTo for convenience of integration

View the API documentation here.

N.B. that the current (v2) of this library is a substantial change from v1 (see https://github.com/vaughan0/go-ini). General usage remains unchanged, but support for direct access to internal structures has been dropped.

Usage

Parse an INI file:

import "github.com/dombenson/go-ini"

file, err := ini.LoadFile("myfile.ini")

Get (string) data from the parsed file:

name, ok := file.Get("person", "name")
if !ok {
  panic("'name' variable missing from 'person' section")
}

Get (array) data from the parsed file:

colours, ok := file.GetArr("apples", "colour")
if !ok {
  panic("'colours' array variable missing from 'apples' section")
}

Create a new file for writing:

file := ini.NewFile()

Set a value in the file:

file.Set("person", "name", "fred")

Write a file out:

file.WriteTo(io.Writer)

File Format

INI files are parsed by go-ini line-by-line. Each line may be one of the following:

  • A section definition: [section-name]
  • A property: key = value
  • An array property: key[] = value
  • A comment: #blahblah or ;blahblah
  • Blank. The line will be ignored.

Properties defined before any section headers are placed in the default section, which has the empty string as it's key.

Example:

# I am a comment
; So am I!

[apples]
colour[] = red
colour[] = green
shape = applish

[oranges]
shape = square
colour = blue

Tests

The tests in this package are written to use the subtest feature of Go 1.7.

Attempting to run the tests with older go will yield a result like

./ini_test.go:366: t.Run undefined (type *testing.T has no field or method Run)

See https://github.com/mpvl/subtest if you need to run them on an older release of Go.

Documentation

Overview

Package ini provides functions for parsing INI configuration files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Copier

type Copier interface {
	// Copy loaded data to a writer
	Copy(Setter)
}

type ErrSyntax

type ErrSyntax struct {
	Line   int
	Source string // The contents of the erroneous line, without leading or trailing whitespace
}

ErrSyntax is returned when there is a syntax error in an INI file.

func (ErrSyntax) Error

func (e ErrSyntax) Error() string

type File

type File interface {
	StreamReadWriter
}

func Copy

func Copy(in Copier) File

Create a file and populate with data from an existing ini.Reader

func Load

func Load(in io.Reader) (File, error)

Load creates a File and populates it with data from a reader.

func LoadFile

func LoadFile(filename string) (file File, err error)

LoadFile creates a File and populates it with data from a file on disk This is a convenience helper since it is a very common use case

func NewFile

func NewFile() File

Newfile will create and initialise a File object

type Getter

type Getter interface {
	// Looks up a value for a key in a section and returns that value, along with a boolean result similar to a map lookup.
	Get(section, key string) (value string, ok bool)
	// Looks up a value for a key in a section and returns that value, along with a boolean result similar to a map lookup.
	// The `ok` boolean will be false in the event that the value could not be parsed as an int
	GetInt(section, key string) (value int, ok bool)
	// Looks up a value for a key in a section and returns that value, along with a boolean result similar to a map lookup.
	// The `ok` boolean will be false in the event that the value could not be parsed as a bool
	GetBool(section, key string) (value bool, ok bool)
	// Looks up a value for an array key in a section and returns that value, along with a boolean result similar to a map lookup.
	GetArr(section, key string) (value []string, ok bool)
	// Lists the sections in the file
	Sections() (value []string)
	// Lists the values in a section the file
	Values(section string) (value map[string]string)
}

type ReadWriter

type ReadWriter interface {
	Reader
	Writer
}

A ReadWriter is able to load, get, modify and save data

type Reader

type Reader interface {
	io.ReaderFrom
	Getter
	Copier
}

A Reader is able to load and extract data from an io.Reader

type Setter

type Setter interface {
	// Set the value for a key in a section, along with a boolean result similar to a map lookup.
	Set(section, key, value string) bool
	// Set a key in a section to an integer value
	SetInt(section, key string, value int) bool
	// Set a key in a section to a boolean value
	SetBool(section, key string, value bool) bool
	// Set a key in a section to a string slice
	SetArr(section, key string, value []string) bool
}

type StreamReadWriter

type StreamReadWriter interface {
	StreamReader
	StreamWriter
}

A StreamReadWriter can get/set data and be treated as an io.ReadWriteCloser

type StreamReader

type StreamReader interface {
	Reader
	io.Writer
}

A StreamReader can additionally accept data by being used as an io.Writer

type StreamWriter

type StreamWriter interface {
	Writer
	io.ReadCloser
}

A StreamWriter can additionally be passed as an io.Reader;

type Writer

type Writer interface {
	io.WriterTo
	Setter
	// Remove a key from a section (OK if it does not exist)
	Remove(section, key string)
	// RemoveSection removes a whole section from an ini file (OK if it does not exist)
	RemoveSection(section string)
}

A Writer is able to set and write data to an io.Writer

Jump to

Keyboard shortcuts

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