conf: bitbucket.org/gosimple/conf Index | Files | Directories

package conf

import "bitbucket.org/gosimple/conf"

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

gosimple/conf has moved to Github, please visit:

https://github.com/gosimple/conf

for current source code.

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

Package Files

conf.go doc.go get.go read.go write.go

Constants

const (
    // Get Errors
    SectionNotFound = iota
    OptionNotFound
    MaxDepthReached

    // Read Errors
    BlankSection

    // Get and Read Errors
    CouldNotParse
)

Variables

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,
    }
)

type Config Uses

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

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

func New Uses

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 Uses

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

func ReadFile Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

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

func (*Config) Int Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

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

func (*Config) String Uses

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 Uses

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 Uses

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

Writes the configuration file to the io.Writer.

func (*Config) WriteBytes Uses

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

WriteBytes returns the configuration file.

func (*Config) WriteFile Uses

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 Uses

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

func (GetError) Error Uses

func (err GetError) Error() string

type ReadError Uses

type ReadError struct {
    Reason int
    Line   string
}

func (ReadError) Error Uses

func (err ReadError) Error() string

Directories

PathSynopsis
example

Package conf imports 9 packages (graph) and is imported by 2 packages. Updated 2019-05-16. Refresh now. Tools for package owners.