Documentation
¶
Overview ¶
Package config provides a configuration system for applications.
Index ¶
- Constants
- func KeyValid(key string) bool
- type Config
- type ConfigCloser
- type File
- func (this *File) Diff(other *File) map[string]struct{}
- func (this *File) Get(key string) (Value, error)
- func (this *File) Has(key string) (bool, error)
- func (this *File) Map() map[string]Value
- func (this *File) Reset(key string) error
- func (this *File) Set(key string, value Value) error
- func (this *File) WriteTo(writer io.Writer) (n int64, err error)
- type Value
- type ValueBool
- type ValueNumber
- type ValueString
Constants ¶
const ( // ErrClosed is returned when Get/Set/Reset is called after the config // has been closed. ErrClosed = configError("attempt to access a closed config") // ErrNonexistentEntry is returned when an entry was not found. ErrNonexistentEntry = configError("nonexistent entry") // ErrMalformedEntry is returned when a config entry could not be // parsed. ErrMalformedEntry = configError("malformed entry") // ErrMalformedKey is returned when a key has invalid runes. ErrMalformedKey = configError("malformed key") // ErrMalformedValue is returned when a value could not be parsed. ErrMalformedValue = configError("malformed value") // ErrMalformedString is returned when a string value could not be // parsed. ErrMalformedStringValue = configError("malformed string value") // ErrMalformedNumber is returned when a number value could not be // parsed. ErrMalformedNumberValue = configError("malformed number value") // ErrMalformedBool is returned when a boolean value could not be // parsed. ErrMalformedBoolValue = configError("malformed bool value") // ErrMalformedEscapeSequence us returned when an escape sequence could // not be parsed. ErrMalformedEscapeSequence = configError("malformed escape sequence") )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config interface { // Get gets a value, first considering the user-level config file, and // then falling back to system level config files. If the value could // not be found anywhere, the specified fallback value is returned. If // the key is invalid, it returns nil, ErrMalformedKey. Get(key string, fallback Value) (Value, error) // GetString is like Get, but will only return strings. If the value is // not a string, it will return fallback. GetString(key string, fallback string) (string, error) // GetNumber is like Get, but will only return numbers. If the value is // not a number, it will return fallback. GetNumber(key string, fallback float64) (float64, error) // GetBool is like Get, but will only return booleans. If the value is // not a boolean, it will return fallback. GetBool(key string, fallback bool) (bool, error) // Set sets a value in the user-level config file. If the key is // invalid, it returns ErrMalformedKey. Note that calling this behavior // *will* cause a write to disk, and a read from disk for whatever is // watching the user file. Set(key string, value Value) error // Reset removes the value from the user-level config file, resetting it // to what is described by the system-level config files. If the key is // invalid, it returns ErrMalformedKey. Note that calling this behavior // *will* cause a write to disk if successful , and a read from disk for // whatever is watching the user file. Reset(key string) error // OnChange specifies a function to be called whenever a value is // changed. The callback is always run within the backend's event loop // using tomo.Do. This could have been a channel but I didn't want to do // that to people. OnChange(func(key string)) event.Cookie }
Config provides access to an application's configuration, and can notify an application of changes to it.
type ConfigCloser ¶
type ConfigCloser interface { Config // Close closes the config, causing it to stop watching for changes. // Reads or writes to the config after this will return an error. Close() error }
ConfigCloser is a config with a Close behavior, which stops watching the config file and causes any subsequent sets/gets to return errors. Anything that receives a ConfigCloser must close it when done.
func NewConfig ¶
func NewConfig(user string, system ...string) (ConfigCloser, error)
NewConfig creates a new Config using paths to the user-level config file, and a set of system config files. These files need not exist: the user-level file will be created when Set is called for the first time if it does not exist already, and nonexistent system files are simply ignored (unless the Config finds them at any point to have been spontaneously created).
The user file is written to when Set is called, and the system files are only read from. Values in the user file override those in the system files, and system files specified nearer to the start of the vararg list will override those farther down.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File represents a config file. It preserves the order of the lines, as well as blank lines and comments.
func Parse ¶
Parse parses a config file from a reader. This function operates on a best-effort basis: A file will always be returned, and any errors encountered will be joined together. For a description of the format, see the README.md of this package.
func (*File) Get ¶
Get gets the keyed value. If the value is unspecified, it returns nil, ErrNonexistentEntry. If the key is invalid, it returns nil, ErrMalformedKey.
func (*File) Has ¶
Has returns whether the key exists. If the key is invalid, it returns false, ErrMalformedKey.
func (*File) Reset ¶
Reset removes the value from the file. If the value is set again, it will be added back at the same location. Note that because of this, the positions of lines are not forgotten until the file is written and reloaded. This is why the method is called Reset and not Remove. If the key is invalid, it returns ErrMalformedKey.
type Value ¶
Value is a config value. Its String behavior produces a lossless and syntactically valid representation of the value.
func ParseValue ¶
ParseValue parses a value from a string. For any Value v, ParseValue(v.String()) should hold data exactly equal to v. This function does not trim whitespace.
type ValueNumber ¶
type ValueNumber float64
ValueNumber is a number value.
func (ValueNumber) Equals ¶
func (value ValueNumber) Equals(other Value) bool
func (ValueNumber) String ¶
func (value ValueNumber) String() string
type ValueString ¶
type ValueString string
ValueString is a string value.
func (ValueString) Equals ¶
func (value ValueString) Equals(other Value) bool
func (ValueString) String ¶
func (value ValueString) String() string