Documentation ¶
Overview ¶
Package etc provides the reading, parsing, and accessing of configuration data. Different readers can be passed as sources for the SML formatted input.
{etc {global {base-directory [$BASEDIR||/var/lib/myserver]} {host-address localhost:1234} {max-users 50} } {service-a {url http://[global/host-address]/service-a} {directory [global/base-directory||.]/service-a} } }
After reading this from a file, reader, or string the number of users can be retrieved with a default value of 10 by calling
maxUsers := cfg.ValueAsInt("global/max-users", 10)
The leading "etc" node of the path is set by default.
If values contain templates formatted [<env-or-path>||<default>] the configuration tries to read the value out of the environment (if the name starts with a dollar sign) or given path inside the configuration. This will be done tope-down. So like in the example above the global base directory will be retrieved out of the environment and can later be referenced by another entry. The default value is optional. It will be used, if the environment variable or the path cannot be found. If a path is invalid and has no default the template will stay inside the value. So accessing the directory of service-a by
svcDir := cfg.ValueAsString("service-a/directory", ".")
leads to "/var/lib/myserver/service-a" and if the base directory isn't set to "./service-a". If nothing is set the default value is the "." passed in the method call.
Index ¶
- func NewContext(ctx context.Context, cfg *Etc) context.Context
- type Application
- type Etc
- func (e *Etc) Apply(appl Application) (*Etc, error)
- func (e *Etc) Do(path string, f func(p string) error) error
- func (e *Etc) Dump() (Application, error)
- func (e *Etc) HasPath(path string) bool
- func (e *Etc) Split(path string) (*Etc, error)
- func (e *Etc) String() string
- func (e *Etc) ValueAsBool(path string, dv bool) bool
- func (e *Etc) ValueAsDuration(path string, dv time.Duration) time.Duration
- func (e *Etc) ValueAsFloat64(path string, dv float64) float64
- func (e *Etc) ValueAsInt(path string, dv int) int
- func (e *Etc) ValueAsString(path, dv string) string
- func (e *Etc) ValueAsTime(path, format string, dv time.Time) time.Time
- func (e *Etc) Write(target io.Writer, prettyPrint bool) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Application ¶
Application is used to apply values to a configurtation.
type Etc ¶
type Etc struct {
// contains filtered or unexported fields
}
Etc contains the read etc configuration and provides access to it. ThetcRoot node "etc" is automatically preceded to the path. The node name have to consist out of 'a' to 'z', '0' to '9', and '-'. The nodes of a path are separated by '/'.
func FromContext ¶
FromContext returns the configuration stored in ctx, if any.
func Read ¶
Read reads the SML source of the configuration from a reader, parses it, and returns the etc instance.
func ReadFile ¶
ReadFile reads the SML source of a configuration file, parses it, and returns the etc instance.
func ReadString ¶
ReadString reads the SML source of the configuration from a string, parses it, and returns the etc instance.
func (*Etc) Apply ¶
func (e *Etc) Apply(appl Application) (*Etc, error)
Apply creates a new configuration by adding of overwriting the passed values. The keys of the map have to be slash separated configuration paths without the leading "etc".
func (*Etc) Do ¶
Do iterates over the children of the given path and executes the function f with that path.
func (*Etc) Dump ¶
func (e *Etc) Dump() (Application, error)
Dump creates a map of paths and their values to apply them into other configurations.
func (*Etc) HasPath ¶
HasPath checks if the configurations has the defined path regardles of the value or possible subconfigurations.
func (*Etc) Split ¶
Split produces a subconfiguration below the passed path. The last path part will be the new root, all values below that configuration node will be below the created root. In case of an invalid path an empty configuration will be returned as default.
func (*Etc) ValueAsBool ¶
ValueAsBool retrieves the bool value at a given path. If it doesn't exist the default value dv is returned.
func (*Etc) ValueAsDuration ¶
ValueAsDuration retrieves the duration value at a given path. If it doesn't exist the default value dv is returned.
func (*Etc) ValueAsFloat64 ¶
ValueAsFloat64 retrieves the float64 value at a given path. If it doesn't exist the default value dv is returned.
func (*Etc) ValueAsInt ¶
ValueAsInt retrieves the int value at a given path. If it doesn't exist the default value dv is returned.
func (*Etc) ValueAsString ¶
ValueAsString retrieves the string value at a given path. If it doesn't exist the default value dv is returned.
func (*Etc) ValueAsTime ¶
ValueAsTime retrieves the string value at a given path and interprets it as time with the passed format. If it doesn't exist the default value dv is returned.