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
- Variables
- type Config
- func (c *Config) AddOption(section string, option string, value string) bool
- func (c *Config) AddSection(section string) bool
- func (c *Config) Bool(section string, option string) (value bool, err error)
- func (c *Config) BoolList(section string, option string) (values []bool, err error)
- func (c *Config) Float64(section string, option string) (value float64, err error)
- func (c *Config) Float64List(section string, option string) (values []float64, err error)
- func (c *Config) HasOption(section string, option string) bool
- func (c *Config) HasSection(section string) bool
- func (c *Config) Int(section string, option string) (value int, err error)
- func (c *Config) Int64(section string, option string) (value int64, err error)
- func (c *Config) Int64List(section string, option string) (values []int64, err error)
- func (c *Config) IntList(section string, option string) (values []int, err error)
- func (c *Config) Options(section string) (options []string, err error)
- func (c *Config) RawString(section string, option string) (value string, err error)
- func (c *Config) Read(reader io.Reader) (err error)
- func (c *Config) RemoveOption(section string, option string) bool
- func (c *Config) RemoveSection(section string) bool
- func (c *Config) Sections() (sections []string)
- func (c *Config) String(section string, option string) (value string, err error)
- func (c *Config) StringList(section string, option string) (values []string, err error)
- func (c *Config) Write(writer io.Writer, header string) (err error)
- func (c *Config) WriteBytes(header string) (config []byte)
- func (c *Config) WriteFile(fname string, perm uint32, header string) (err error)
- type GetError
- type ReadError
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, } )
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 ReadFile ¶
ReadFile reads a file and returns a new configuration representation. This representation can be queried with String, etc.
func (*Config) AddOption ¶
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 ¶
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 ¶
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 ¶
BoolList has the same behaviour as StringList but converts the response to []bool.
func (*Config) Float64 ¶
Float64 has the same behaviour as String but converts the response to float64.
func (*Config) Float64List ¶
Float64List has the same behaviour as StringList but converts the response to []float64.
func (*Config) HasOption ¶
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 ¶
HasSection checks if the configuration has the given section. (The default section always exists.)
func (*Config) Int64List ¶
Int64List has the same behaviour as StringList but converts the response to []int64.
func (*Config) IntList ¶
IntList has the same behaviour as StringList but converts the response to []int.
func (*Config) Options ¶
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 ¶
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 ¶
Read reads an io.Reader and returns a configuration representation. This representation can be queried with String, etc.
func (*Config) RemoveOption ¶
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 ¶
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 ¶
Sections returns the list of sections in the configuration. (The default section always exists.)
func (*Config) String ¶
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 ¶
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) WriteBytes ¶
WriteBytes returns the configuration file.