config

package
v0.0.0-...-5e351a8 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2020 License: BSD-2-Clause Imports: 8 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DEFAULT_COMMENT       = "# "
	ALTERNATIVE_COMMENT   = "; "
	DEFAULT_SEPARATOR     = ":"
	ALTERNATIVE_SEPARATOR = "="
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config is the representation of configuration settings.

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)

ReadDefault 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 (self *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 (self *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 (self *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) Float

func (self *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 (self *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 (self *Config) HasSection(section string) bool

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

func (*Config) Int

func (self *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) Options

func (self *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 (self *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) RemoveOption

func (self *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 (self *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 (self *Config) Sections() (sections []string)

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

func (*Config) String

func (self *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 (self *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.

Jump to

Keyboard shortcuts

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