README

conf

Package conf provide support for parsing configuration files.

Documentation online

Installation

go get bitbucket.org/gosimple/conf

Usage

Check example folder

import "bitbucket.org/gosimple/conf"

NOTE: All section names and options are case insensitive. All values are case sensitive.

Example 1

Config

host = something.com
port = 443
active = true
compression = off

list-str = hello, world
list-int = 1, 2, 3

Code

c, err := conf.ReadFile("something.config")
c.String("default", "host")				// return something.com
c.Int("default", "port")				// return 443
c.Bool("default", "active")				// return true
c.Bool("default", "compression")		// return false

c.StringList("default", "list-str")		// return ["hello", "world"]
c.IntList("default", "list-int")		// return [1, 2, 3]
Example 2

Config

[default]
host = something.com
port = 443
active = true
compression = off

[service-1]
compression = on

[service-2]
port = 444

Code

c, err := conf.ReadFile("something.config")
c.Bool("default", "compression") // returns false
c.Bool("service-1", "compression") // returns true
c.Bool("service-2", "compression") // returns GetError
Requests or bugs?

https://bitbucket.org/gosimple/conf/issues

Info

Package conf is based on goconfig

License

The source files are distributed under the The BSD 3-Clause License. You can find full license text in the LICENSE file.

Expand ▾ Collapse ▴

Documentation

Overview

    This package implements a parser for configuration files. This allows easy reading and writing of structured configuration files.

    Given the configuration file:

    [default]
    host = example.com
    port = 443
    php = on
    
    list-str = hello, world
    list-int = 1, 2, 3
    
    [service-1]
    host = s1.example.com
    allow-writing = false
    

    To read this configuration file, do:

    c, err := conf.ReadFile("server.conf")
    c.String("default", "host")             // returns example.com
    c.Int("", "port")                       // returns 443 (assumes "default")
    c.Bool("", "php")                       // returns true
    
    c.StringList("default", "list-str")		// return ["hello", "world"]
    c.IntList("default", "list-int")		// return [1, 2, 3]
    
    c.String("service-1", "host")           // returns s1.example.com
    c.Bool("service-1","allow-writing")     // returns false
    c.Int("service-1", "port")              // returns 0 and a GetError
    

    Note that all section and option names are case insensitive. All values are case sensitive.

    Goconfig's string substitution syntax has not been removed. However, it may be taken out or modified in the future.

    Index

    Constants

    View Source
    const (
    	// Get Errors
    	SectionNotFound = iota
    	OptionNotFound
    	MaxDepthReached
    
    	// Read Errors
    	BlankSection
    
    	// Get and Read Errors
    	CouldNotParse
    )

    Variables

    View Source
    var (
    	DefaultSection = "default" // Default section name (must be lower-case).
    	DepthValues    = 200       // Maximum allowed depth when recursively substituing variable names.
    
    	// Strings accepted as bool.
    	BoolStrings = map[string]bool{
    		"t":     true,
    		"true":  true,
    		"y":     true,
    		"yes":   true,
    		"on":    true,
    		"1":     true,
    		"f":     false,
    		"false": false,
    		"n":     false,
    		"no":    false,
    		"off":   false,
    		"0":     false,
    	}
    )

    Functions

    This section is empty.

    Types

    type Config

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

      Config is the representation of configuration settings. The public interface is entirely through methods.

      func New

      func New() *Config

        New creates an empty configuration representation. This representation can be filled with AddSection and AddOption and then saved to a file using WriteFile.

        func ReadBytes

        func ReadBytes(conf []byte) (c *Config, err error)

        func ReadFile

        func ReadFile(fname string) (c *Config, err error)

          ReadFile reads a file and returns a new configuration representation. This representation can be queried with String, etc.

          func (*Config) AddOption

          func (c *Config) AddOption(section string, option string, value string) bool

            AddOption adds a new option and value to the configuration. It returns true if the option and value were inserted, and false if the value was overwritten. If the section does not exist in advance, it is created.

            func (*Config) AddSection

            func (c *Config) AddSection(section string) bool

              AddSection adds a new section to the configuration. It returns true if the new section was inserted, and false if the section already existed.

              func (*Config) Bool

              func (c *Config) Bool(section string, option string) (value bool, err error)

                Bool has the same behaviour as String but converts the response to bool. See constant BoolStrings for string values converted to bool.

                func (*Config) BoolList

                func (c *Config) BoolList(section string, option string) (values []bool, err error)

                  BoolList has the same behaviour as StringList but converts the response to []bool.

                  func (*Config) Float64

                  func (c *Config) Float64(section string, option string) (value float64, err error)

                    Float64 has the same behaviour as String but converts the response to float64.

                    func (*Config) Float64List

                    func (c *Config) Float64List(section string, option string) (values []float64, err error)

                      Float64List has the same behaviour as StringList but converts the response to []float64.

                      func (*Config) HasOption

                      func (c *Config) HasOption(section string, option string) bool

                        HasOption checks if the configuration has the given option in the section. It returns false if either the option or section do not exist.

                        func (*Config) HasSection

                        func (c *Config) HasSection(section string) bool

                          HasSection checks if the configuration has the given section. (The default section always exists.)

                          func (*Config) Int

                          func (c *Config) Int(section string, option string) (value int, err error)

                            Int has the same behaviour as String but converts the response to int.

                            func (*Config) Int64

                            func (c *Config) Int64(section string, option string) (value int64, err error)

                              Int64 has the same behaviour as String but converts the response to int64.

                              func (*Config) Int64List

                              func (c *Config) Int64List(section string, option string) (values []int64, err error)

                                Int64List has the same behaviour as StringList but converts the response to []int64.

                                func (*Config) IntList

                                func (c *Config) IntList(section string, option string) (values []int, err error)

                                  IntList has the same behaviour as StringList but converts the response to []int.

                                  func (*Config) Options

                                  func (c *Config) Options(section string) (options []string, err error)

                                    Options returns the list of options available in the given section. It returns an error if the section does not exist and an empty list if the section is empty. Options within the default section are also included.

                                    func (*Config) RawString

                                    func (c *Config) RawString(section string, option string) (value string, err error)

                                      RawString gets the (raw) string value for the given option in the section. The raw string value is not subjected to unfolding, which was illustrated in the beginning of this documentation. It returns an error if either the section or the option do not exist.

                                      func (*Config) Read

                                      func (c *Config) Read(reader io.Reader) (err error)

                                        Read reads an io.Reader and returns a configuration representation. This representation can be queried with String, etc.

                                        func (*Config) RemoveOption

                                        func (c *Config) RemoveOption(section string, option string) bool

                                          RemoveOption removes a option and value from the configuration. It returns true if the option and value were removed, and false otherwise, including if the section did not exist.

                                          func (*Config) RemoveSection

                                          func (c *Config) RemoveSection(section string) bool

                                            RemoveSection removes a section from the configuration. It returns true if the section was removed, and false if section did not exist.

                                            func (*Config) Sections

                                            func (c *Config) Sections() (sections []string)

                                              Sections returns the list of sections in the configuration. (The default section always exists.)

                                              func (*Config) String

                                              func (c *Config) String(section string, option string) (value string, err error)

                                                String gets the string value for the given option in the section. If the value needs to be unfolded (see e.g. %(host)s example in the beginning of this documentation), then String does this unfolding automatically, up to DepthValues number of iterations. It returns an error if either the section or the option do not exist, or the unfolding cycled.

                                                func (*Config) StringList

                                                func (c *Config) StringList(section string, option string) (values []string, err error)

                                                  StringList gets the string values for the given option in the section. It returns an error if either the section or the option do not exist, or the unfolding cycled.

                                                  func (*Config) Write

                                                  func (c *Config) Write(writer io.Writer, header string) (err error)

                                                    Writes the configuration file to the io.Writer.

                                                    func (*Config) WriteBytes

                                                    func (c *Config) WriteBytes(header string) (config []byte)

                                                      WriteBytes returns the configuration file.

                                                      func (*Config) WriteFile

                                                      func (c *Config) WriteFile(fname string, perm uint32, header string) (err error)

                                                        WriteFile saves the configuration representation to a file. The desired file permissions must be passed as in os.Open. The header is a string that is saved as a comment in the first line of the file.

                                                        type GetError

                                                        type GetError struct {
                                                        	Reason    int
                                                        	ValueType string
                                                        	Value     string
                                                        	Section   string
                                                        	Option    string
                                                        }

                                                        func (GetError) Error

                                                        func (err GetError) Error() string

                                                        type ReadError

                                                        type ReadError struct {
                                                        	Reason int
                                                        	Line   string
                                                        }

                                                        func (ReadError) Error

                                                        func (err ReadError) Error() string

                                                        Directories

                                                        Path Synopsis