confparse

package module
v0.0.0-...-7392cb0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2017 License: MIT Imports: 12 Imported by: 0

README

YACP

GoDoc Build Status Coverage Status

Yet Another Configuration Parser

This is a small package the provides a ini style configuration parser. This is what is allowed:

  • Comments start, either with the "#" or ":" anything after it, till newline is ignored
  • Sections are written like the following [default] and contain a map of key values, anything between square brackets is a valid section.
  • Key and values are like "ip=192.168.10.1" ,the separator is "=" otherwise will not be considered a key value.
  • The Parser can handle bool, int and floats (both 64bit), strings and string slices, as long as they are divided by columns.
  • Empty lines are ignored, white spaces are ignored as well.
How to use it

Pretty simple, there is only one method to create a new parser just call

  ini, err := confparse.New("config-name.whatever")

It isn't name sensible any valid name can be passed. Then any of the valid supported values can be retrieved like so:

  value ,err := init.GetInt("sectionname.valuename")
  value ,err := init.GetFloat("sectionname.valuename")
  value ,err := init.GetSlice("sectionname.valuename")
  value ,err := init.GetString("sectionname.valuename")
  value ,err := init.GetBool("sectionname.valuename")
  section ,err := init.GetSection("sectionname")

There is also a Watch function that listen if any changes are made to the configuration file, if it does find some, the configuration get reloaded and parsed every time a change occurs. You can call it like so:

  ini.Watch()

If you need to run a function every time an event occurs on the file watched there the:

 ini.OnConfChange(run func(ev fsnotify.Event))

And that's pretty much about it.

Philosophy

This software is developed following the "mantra" keep it simple, stupid or better known as KISS. Something so simple like configuration files should not required over engineered solutions. Though it provides most of the functionality needed by generic configuration files, and most important of all meaning full error messages.

Disclaimer

This software in alpha quality, don't use it in a production environment, it's not even completed.

Thank You Notes

I should thanks the gopheraccademy since I wrote this little lib after I read an article about configuration and tokenisers.

Documentation

Index

Constants

View Source
const (
	EOF token = iota
	KEY_VALUE
	SECTION
	WHITESPACE
	NON_VALID
	COMMENT
)

Variables

View Source
var (
	KEY_NOT_FOUND error = fmt.Errorf("key not found ")
	SEC_NOT_FOUND error = fmt.Errorf("sec not found ")
	NOT_BOOL            = fmt.Errorf("Value is not a bool ")
	NOT_INT             = fmt.Errorf("Value is not an int ")
	NOT_FLOAT           = fmt.Errorf("Value is not a float ")
	NOT_STRING          = fmt.Errorf("Value is not a string ")
)

Functions

This section is empty.

Types

type IniParser

type IniParser struct {
	// contains filtered or unexported fields
}

func New

func New(confname string) (*IniParser, error)

New creates and parse a new configuration from a file name returns a valid parsed object and a nil, or an error and nil object in the successful case the values are ready to be retrieved

func (*IniParser) GetBool

func (i *IniParser) GetBool(sectionKey string) (bool, error)

GetBool retrieves a bool value from named section with key name, returns either an error and an invalid value or a nil and a valid value.

func (*IniParser) GetDuration

func (i *IniParser) GetDuration(sectionKey string) (time.Duration, error)

GetDuration retrieves a time.Duration value from the named section/key, returns either an error and an -1 or a valid duration and a nil error.

func (*IniParser) GetFloat

func (i *IniParser) GetFloat(sectionKey string) (float64, error)

GetFloat retrieves a float64 value from named section with key name, returns either an error and an invalid value or a nil and a valid value.

func (*IniParser) GetInt

func (i *IniParser) GetInt(sectionKey string) (int64, error)

GetInt retrieves a int64 value from named section with key name, returns either an error and an invalid value or a nil and a valid value.

func (*IniParser) GetSection

func (i *IniParser) GetSection(section string) (map[string]string, error)

GetSection retrieves an entire section coverting the values to the appropriate type is left to the user everything is a string

func (*IniParser) GetSlice

func (i *IniParser) GetSlice(sectionKey string) ([]string, error)

GetSlice retrieves a slice value from named section with key name, returns either an error and an invalid value or a nil and a valid value.

func (*IniParser) GetString

func (i *IniParser) GetString(sectionKey string) (string, error)

GetString retrieves a string value from named section with key name, returns either an error and an invalid value or a nil and a valid value.

func (*IniParser) OnConfChange

func (i *IniParser) OnConfChange(run func(ev fsnotify.Event))

OnConfChange accept a single parameter, a function that gets run right after every event and receive a copy of it.

func (*IniParser) Parse

func (i *IniParser) Parse()

Parse actually parses the object content, note the object is always in a valid state, must be called if the Parser has been created with New, in case it has been created with NewFromFile it has already been parsed.

func (*IniParser) Watch

func (i *IniParser) Watch() error

Watch add a file system watcher on the config file itself and reloads the configuration and parses it every time a write event is received.

Jump to

Keyboard shortcuts

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