etc

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2020 License: BSD-3-Clause Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewContext

func NewContext(ctx context.Context, cfg *Etc) context.Context

NewContext returns a new context that carries a configuration.

Types

type Application

type Application map[string]string

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

func FromContext(ctx context.Context) (*Etc, bool)

FromContext returns the configuration stored in ctx, if any.

func Read

func Read(source io.Reader) (*Etc, error)

Read reads the SML source of the configuration from a reader, parses it, and returns the etc instance.

func ReadFile

func ReadFile(filename string) (*Etc, error)

ReadFile reads the SML source of a configuration file, parses it, and returns the etc instance.

func ReadString

func ReadString(source string) (*Etc, error)

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

func (e *Etc) Do(path string, f func(p string) error) error

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

func (e *Etc) HasPath(path string) bool

HasPath checks if the configurations has the defined path regardles of the value or possible subconfigurations.

func (*Etc) Split

func (e *Etc) Split(path string) (*Etc, error)

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

func (e *Etc) String() string

String implements the fmt.Stringer interface.

func (*Etc) ValueAsBool

func (e *Etc) ValueAsBool(path string, dv bool) bool

ValueAsBool retrieves the bool value at a given path. If it doesn't exist the default value dv is returned.

func (*Etc) ValueAsDuration

func (e *Etc) ValueAsDuration(path string, dv time.Duration) time.Duration

ValueAsDuration retrieves the duration value at a given path. If it doesn't exist the default value dv is returned.

func (*Etc) ValueAsFloat64

func (e *Etc) ValueAsFloat64(path string, dv float64) float64

ValueAsFloat64 retrieves the float64 value at a given path. If it doesn't exist the default value dv is returned.

func (*Etc) ValueAsInt

func (e *Etc) ValueAsInt(path string, dv int) int

ValueAsInt retrieves the int value at a given path. If it doesn't exist the default value dv is returned.

func (*Etc) ValueAsString

func (e *Etc) ValueAsString(path, dv string) string

ValueAsString retrieves the string value at a given path. If it doesn't exist the default value dv is returned.

func (*Etc) ValueAsTime

func (e *Etc) ValueAsTime(path, format string, dv time.Time) time.Time

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.

func (*Etc) Write

func (e *Etc) Write(target io.Writer, prettyPrint bool) error

Write writes the configuration as SML to the passed target. If prettyPrint is true the written SML is indented and has linebreaks.

Jump to

Keyboard shortcuts

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