config

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2022 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheHome added in v0.4.0

func CacheHome(name string) (home string)

CacheHome returns the path to the directory where cache files should be stored.

func DataHome added in v0.4.0

func DataHome(name string) (home string)

DataHome returns the path to the directory where user data should be stored.

Types

type Config

type Config struct {
	// LegalParameters holds the names and types of all parameters that can
	// be parsed. If the parser runs into a parameter that is not listed
	// here, it will print out an error message and keep on parsing.
	LegalParameters map[string]Type

	// Parameters holds the names and values of all parsed parameters. If a
	// value is non-nil, it can be safely type asserted into whatever type
	// was requested.
	Parameters map[string]any
}

Config holds a list of configuration parameters.

func (*Config) Load

func (config *Config) Load(name string) (err error)

Load loads and parses the files /etc/xdg/<name>/<name>.conf and <home>/.config/<name>/<name>.conf, unless the corresponding XDG environment variables are set - then it uses those.

func (*Config) LoadFrom

func (config *Config) LoadFrom(reader io.Reader) (err error)

LoadFrom parses a configuration file from an io.Reader. Configuration files are divided into lines where each line may be blank, a comment, or a key-value pair. If the line is blank or begins with a # character, it is ignored. Else, the line must have a key and a value separated by a colon. Before they are processed, leading and trailing whitespace is trimmed from the key and the value. Keys are case sensitive.

func (*Config) Save

func (config *Config) Save(name string) (err error)

Save overwrites the main user configuration file, which is located at <home>/.config/<name>/<name>.conf unless $XDG_CONFIG_HOME has been set, in which case the value of that variable is used instead.

func (*Config) SaveTo

func (config *Config) SaveTo(writer io.Writer) (err error)

SaveTo writes the configuration data to the specified io.Writer. Keys are alphabetically sorted.

type Error

type Error int

Error represents an error that can be returned by functions or methods in this module.

const (
	// ErrorIllegalName is thrown when an application name contains illegal
	// characters such as a slash.
	ErrorIllegalName Error = iota

	// ErrorNoSeparator is thrown when a configuration file has an
	// incorrectly formatted key-value pair.
	ErrorNoSeparator

	// ErrorUnknownParameter is thrown when an unknown key is encountered in
	// a configuration file.
	ErrorUnknownParameter

	// ErrorWrongColorLength is thrown when a configuration file has a color
	// literal with a total length unequal to 7.
	ErrorWrongColorLength

	// ErrorMalformedColorLiteral is thrown when a configuration file has an
	// improperly formatted color literal, or a color literal was expected
	// and something else was encountered.
	ErrorMalformedColorLiteral

	// ErrorMalformedIntegerLiteral is thrown when a configuration file has
	// an improperly formatted integer literal, or an integer literal was
	// expected and something else was encountered.
	ErrorMalformedIntegerLiteral

	// ErrorMalformedFloatLiteral is thrown when a configuration file has
	// an improperly formatted float literal, or a float literal was
	// expected and something else was encountered.
	ErrorMalformedFloatLiteral
)

func (Error) Error

func (err Error) Error() (description string)

Error returns a description of the error.

type Type

type Type int

Type represents the data type of a configuration parameter.

const (
	// string
	// It is just a basic string with inner whitespace preserved. No quotes
	// should be used in the file.
	TypeString Type = iota

	// Type: image/color.RGBA
	// Represented as a 24 bit hexadecimal number (case insensitive)
	// preceded with a # sign where the first two digits represent the red
	// channel, the middle two digits represent the green channel, and the
	// last two digits represent the blue channel.
	TypeColor

	// Type: int
	// An integer literal, like 123456789
	TypeInteger

	// Type: float64
	// A floating point literal, like 1234.56789
	TypeFloat

	// Type: bool
	// Values true, yes, on, and 1 are all truthy (case insensitive) and
	// anything else is falsy.
	TypeBoolean
)

Jump to

Keyboard shortcuts

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