etc

package
v4.24.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2017 License: BSD-3-Clause Imports: 12 Imported by: 5

Documentation

Overview

Package etc of the Tideland Go Library package 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

View Source
const (
	ErrIllegalSourceFormat = iota + 1
	ErrIllegalConfigSource
	ErrCannotReadFile
	ErrCannotPostProcess
	ErrInvalidPath
	ErrCannotSplit
	ErrCannotApply
)

Error codes of the etc package.

Variables

This section is empty.

Functions

func IsInvalidPathError

func IsInvalidPathError(err error) bool

IsInvalidPathError checks if a path cannot be found.

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 interface {
	fmt.Stringer

	// HasPath checks if the configurations has the defined path
	// regardles of the value or possible subconfigurations.
	HasPath(path string) bool

	// Do iterates over the children of the given path and executes
	// the function f with that path.
	Do(path string, f func(p string) error) error

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

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

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

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

	// 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.
	ValueAsTime(path, layout string, dv time.Time) time.Time

	// ValueAsDuration retrieves the duration value at a given path.
	// If it doesn't exist the default value dv is returned.
	ValueAsDuration(path string, dv time.Duration) time.Duration

	// Spit 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.
	Split(path string) (Etc, error)

	// Dunp creates a map of paths and their values to apply
	// them into other configurations.
	Dump() (Application, 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".
	Apply(appl Application) (Etc, error)

	// Write writes the configuration as SML to the passed target.
	// If prettyPrint is true the written SML is indented and has
	// linebreaks.
	Write(target io.Writer, prettyPrint bool) error
}

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.

Jump to

Keyboard shortcuts

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