config

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2019 License: MIT Imports: 9 Imported by: 0

README

config

This package implements a basic configuration file parser language which provides a structure similar to what you would find on Microsoft Windows INI files.

The configuration file consists of sections, led by a "[section]" header and followed by "name: value" entries; "name=value" is also accepted. Note that leading whitespace is removed from values. The optional values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Additional defaults can be provided on initialization and retrieval. Comments are indicated by ";" or "#"; a comment may begin anywhere on a line, including on the same line after parameters or section declarations.

For example:

[My Section]
foodir: %(dir)s/whatever
dir=foo

would resolve the "%(dir)s" to the value of "dir" (foo in this case). All reference expansions are done on demand.

The functionality and workflow is loosely based on the configparser package of the Python Standard Library.

Installation

go get github.com/robfig/config

Operating instructions

Given a sample configuration file:

[DEFAULT]
host: www.example.com
protocol: http://
base-url: %(protocol)s%(host)s

[service-1]
url: %(base-url)s/some/path
delegation: on
maxclients: 200 # do not set this higher
comments: This is a multi-line
	entry	# And this is a comment

To read this configuration file, do:

c, _ := config.ReadDefault("config.cfg")

c.String("service-1", "url")
// result is string "http://www.example.com/some/path"

c.Int("service-1", "maxclients")
// result is int 200

c.Bool("service-1", "delegation")
// result is bool true

c.String("service-1", "comments")
// result is string "This is a multi-line\nentry"

Note the support for unfolding variables (such as %(base-url)s), which are read from the special (reserved) section name [DEFAULT].

A new configuration file can also be created with:

c := config.NewDefault()
c.AddSection("Section")
c.AddOption("Section", "option", "value")
c.WriteFile("config.cfg", 0644, "A header for this file")

This results in the file:

# A header for this file

[Section]
option: value

Note that sections, options and values are all case-sensitive.

License

The source files are distributed under the Mozilla Public License, version 2.0, unless otherwise noted.
Please read the FAQ if you have further questions regarding the license.

Documentation

Index

Constants

View Source
const (
	// Default section name.
	DEFAULT_SECTION = "DEFAULT"

	DEFAULT_COMMENT       = "# "
	ALTERNATIVE_COMMENT   = "; "
	DEFAULT_SEPARATOR     = "="
	ALTERNATIVE_SEPARATOR = ":"
)

Variables

View Source
var (
	BASE_SECTION string
)

Functions

func AddOption

func AddOption(section string, option string, value string) bool

func AddSection

func AddSection(section string) bool

func Bool

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

func Float

func Float(section string, option string) (value float64, err error)

func HasOption

func HasOption(section string, option string) bool

func HasSection

func HasSection(section string) bool

func Int

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

func Merge

func Merge(source *Config)

func Options

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

func RawString

func RawString(section string, option string) (value string, err error)

func RawStringDefault

func RawStringDefault(option string) (value string, err error)

func RemoveOption

func RemoveOption(section string, option string) bool

func RemoveSection

func RemoveSection(section string) bool

func SectionOptions

func SectionOptions(section string) (options []string, err error)

func Sections

func Sections() (sections []string)

func String

func String(section string, option string) (value string, err error)

func WriteFile

func WriteFile(fname string, perm os.FileMode, header string) error

Types

type Config

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

Config is the representation of configuration settings.

func InitConfig

func InitConfig(fname string, base_section string) (cfg *Config, err error)

初始化全局配置文件

func New

func New(comment, separator string, preSpace, postSpace bool) *Config

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

== Arguments

comment: has to be `DEFAULT_COMMENT` or `ALTERNATIVE_COMMENT` separator: has to be `DEFAULT_SEPARATOR` or `ALTERNATIVE_SEPARATOR` preSpace: indicate if is inserted a space before of the separator postSpace: indicate if is added a space after of the separator

func NewDefault

func NewDefault() *Config

NewDefault creates a configuration representation with values by default.

func Read

func Read(fname string, comment, separator string, preSpace, postSpace bool) (*Config, error)

Read reads a configuration file and returns its representation. All arguments, except `fname`, are related to `New()`

func ReadDefault

func ReadDefault(fname string) (*Config, error)

ReadDefault reads a configuration file and returns its representation. It uses values by default.

func (*Config) AddOption

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

AddOption adds a new option and value to the configuration.

If the section is nil then uses the section by default; if it does not exist, it is created in advance.

It returns true if the option and value were inserted, and false if the value was overwritten.

func (*Config) AddSection

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

AddSection adds a new section to the configuration.

If the section is nil then uses the section by default which it's already created.

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 "boolString" for string values converted to bool.

func (*Config) DynamicString

func (c *Config) DynamicString(str string) (value string, err error)

String gets the string value for the given dynamic string. e.g. %(host)s example;${host} example

func (*Config) Float

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

Float has the same behaviour as String but converts the response to float.

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) Merge

func (target *Config) Merge(source *Config)

Merge merges the given configuration "source" with this one ("target").

Merging means that any option (under any section) from source that is not in target will be copied into target. When the target already has an option with the same name and section then it is overwritten (i.o.w. the source wins).

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) RawStringDefault

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

RawStringDefault gets the (raw) string value for the given option from the DEFAULT section.

It returns an error if the option does not exist in the DEFAULT section.

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) SectionOptions

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

SectionOptions returns only the list of options available in the given section. Unlike Options, SectionOptions doesn't return options in default section. It returns an error if the section doesn't 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 _DEPTH_VALUES number of iterations.

It returns an error if either the section or the option do not exist, or the unfolding cycled.

func (*Config) WriteFile

func (c *Config) WriteFile(fname string, perm os.FileMode, header string) 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 OptionError

type OptionError string

func (OptionError) Error

func (e OptionError) Error() string

type SectionError

type SectionError string

func (SectionError) Error

func (e SectionError) Error() string

Jump to

Keyboard shortcuts

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