Documentation ¶
Overview ¶
Package configparser provides a simple parser for reading/writing configuration (INI) files.
Supports reading/writing the INI file format in addition to:
- Reading/writing duplicate section names (ex: MySQL NDB engine's config.ini)
- Options without values (ex: can be used to group a set of hostnames)
- Options without a named section (ex: a simple option=value file)
- Find sections with regexp pattern matching on section names, ex: dc1.east.webservers where regex is '.webservers'
- # or ; as comment delimiter
- = or : as value delimiter
Index ¶
- func Save(c *Configuration, filePath string) (err error)
- type Configuration
- func (c *Configuration) AllSections() ([]*Section, error)
- func (c *Configuration) Delete(regex string) (sections []*Section, err error)
- func (c *Configuration) FilePath() string
- func (c *Configuration) Find(regex string) ([]*Section, error)
- func (c *Configuration) NewSection(fqn string) *Section
- func (c *Configuration) PrintSection(fqn string)
- func (c *Configuration) Section(fqn string) (*Section, error)
- func (c *Configuration) Sections(fqn string) ([]*Section, error)
- func (c *Configuration) SetFilePath(filePath string)
- func (c *Configuration) String() string
- func (c *Configuration) StringValue(section, option string) (value string, err error)
- type Section
- func (s *Section) Add(option string, value string) (oldValue string)
- func (s *Section) Delete(option string) (value string)
- func (s *Section) Exists(option string) (ok bool)
- func (s *Section) Get(option, fallback string) string
- func (s *Section) Name() string
- func (s *Section) OptionNames() []string
- func (s *Section) Options() map[string]string
- func (s *Section) SetValueFor(option string, value string) string
- func (s *Section) String() string
- func (s *Section) ValueOf(option string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Save ¶
func Save(c *Configuration, filePath string) (err error)
Save the Configuration to file. Creates a backup (.bak) if file already exists.
Types ¶
type Configuration ¶
type Configuration struct {
// contains filtered or unexported fields
}
Configuration represents a configuration file with its sections and options.
func NewConfiguration ¶
func NewConfiguration() *Configuration
NewConfiguration returns a new Configuration instance with an empty file path.
func Read ¶
func Read(filePath string) (*Configuration, error)
ReadFile parses a specified configuration file and returns a Configuration instance.
func (*Configuration) AllSections ¶
func (c *Configuration) AllSections() ([]*Section, error)
AllSections returns a slice of all sections available.
func (*Configuration) Delete ¶
func (c *Configuration) Delete(regex string) (sections []*Section, err error)
DeleteSection deletes the specified sections matched by a regex name and returns the deleted sections.
func (*Configuration) FilePath ¶
func (c *Configuration) FilePath() string
Filepath returns the configuration file path.
func (*Configuration) Find ¶
func (c *Configuration) Find(regex string) ([]*Section, error)
Find returns a slice of Sections matching the regexp against the section name.
func (*Configuration) NewSection ¶
func (c *Configuration) NewSection(fqn string) *Section
NewSection creates and adds a new Section with the specified name.
func (*Configuration) PrintSection ¶
func (c *Configuration) PrintSection(fqn string)
PrintSection prints a text representation of all sections matching the fully qualified section name.
func (*Configuration) Section ¶
func (c *Configuration) Section(fqn string) (*Section, error)
Section returns the first section matching the fully qualified section name.
func (*Configuration) Sections ¶
func (c *Configuration) Sections(fqn string) ([]*Section, error)
Sections returns a slice of Sections matching the fully qualified section name.
func (*Configuration) SetFilePath ¶
func (c *Configuration) SetFilePath(filePath string)
SetFilePath sets the Configuration file path.
func (*Configuration) String ¶
func (c *Configuration) String() string
String returns the text representation of a parsed configuration file.
func (*Configuration) StringValue ¶
func (c *Configuration) StringValue(section, option string) (value string, err error)
StringValue returns the string value for the specified section and option.
type Section ¶
type Section struct {
// contains filtered or unexported fields
}
A Section in a configuration
func (*Section) Add ¶
Add adds a new option to the section. Adding and existing option will overwrite the old one. The old value is returned
func (*Section) Delete ¶
Delete removes the specified option from the section and returns the deleted option's value.
func (*Section) OptionNames ¶
OptionNames returns a slice of option names in the same order as they were parsed.
func (*Section) SetValueFor ¶
SetValueFor sets the value for the specified option and returns the old value.