sourcer

package
v0.0.0-...-f645f3a Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2018 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseTOML

func ParseTOML(content []byte) (map[string]interface{}, error)

ParseTOML parses the given content as JSON.

func ParseYAML

func ParseYAML(content []byte) (map[string]interface{}, error)

ParseYAML parses the given content as YAML.

Types

type FileParser

type FileParser func(content []byte) (map[string]interface{}, error)

type Sourcer

type Sourcer interface {
	// Tags returns a list of tags which are required to get a value from
	// the source. Order matters.
	Tags() []string

	// Get will retrieve a value from the source with the given tag values.
	// The tag values passed to this method will be in the same order as
	// returned from the Tags method. The flag return value directs config
	// population whether or not this value should be treated as missing or
	// skippable.
	Get(values []string) (string, SourcerFlag, error)

	// Assets returns a list of names of assets that compose the sourcer.
	// This can be a list of matched files that are read, or a token that
	// denotes a fixed source.
	Assets() []string

	// Dump returns the full content of the sourcer. This is used by the
	// logging package to show the content of the environment and config
	// files when a value is missing or otherwise illegal.
	Dump() map[string]string
}

Sourcer pulls requested names from a variable source. This can be the environment, a file, a remote server, etc. This can be done on-demand per variable, or a cache of variables can be built on startup and then pulled from a cached mapping as requested.

func NewDirectorySourcer

func NewDirectorySourcer(dirname string, parser FileParser) Sourcer

NewDirectorySourcer creates a sourcer that reads files from a directory. For details on parsing format, refer to NewFileParser. Each file in a directory is read in alphabetical order. Nested directories are ignored when reading direcotry content, and each found regular file is assumed to be parseable by the given FileParser.

func NewEnvSourcer

func NewEnvSourcer(prefix string) Sourcer

NewEnvSourcer creates a Sourcer that pulls values from the environment. The environment variable {PREFIX}_{NAME} is read before and, if empty, the environment varaible {NAME} is read as a fallback. The prefix is normalized by replacing all non-alpha characters with an underscore, removing leading and trailing underscores, and collapsing consecutive underscores with a single character.

func NewFileSourcer

func NewFileSourcer(filename string, parser FileParser) Sourcer

NewFileSourcer creates a sourcer that reads content from a file. The format of the file is read by the given FileParser. The content of the file must be an encoding of a map from string keys to JSON-serializable values. If a nil parser is supplied, one will be selected based on the extension of the file. JSON, YAML, and TOML files are supported.

func NewGlobSourcer

func NewGlobSourcer(pattern string, parser FileParser) Sourcer

NewGlobSourcer creates a sourcer that reads all files that match the given glob pattern. Each matching file is read in alphabetical order of path. Each matching pathis assumed to be parseable by the given FileParser.

func NewMultiSourcer

func NewMultiSourcer(sourcers ...Sourcer) Sourcer

NewMultiSourcer creates a sourcer that reads form each sourcer. The last value found is returned - sourcers should be provided from low priority to high priority.

func NewOptionalDirectorySourcer

func NewOptionalDirectorySourcer(dirname string, parser FileParser) Sourcer

NewOptionalDirectorySourcer creates a directory sourcer if the provided directoy exists. the provided file is not found, a sourcer is returned returns no values.

func NewOptionalFileSourcer

func NewOptionalFileSourcer(filename string, parser FileParser) Sourcer

NewOptionalFileSourcer creates a file sourcer if the provided file exists. If the provided file is not found, a sourcer is returned returns no values.

func NewTOMLFileSourcer

func NewTOMLFileSourcer(filename string) Sourcer

NewTOMLFileSourcer creates a file sourcer that parses conent as TOML.

func NewYAMLFileSourcer

func NewYAMLFileSourcer(filename string) Sourcer

NewYAMLFileSourcer creates a file sourcer that parses conent as YAML.

type SourcerFlag

type SourcerFlag int
const (
	FlagUnknown SourcerFlag = iota
	FlagFound
	FlagMissing
	FlagSkip
)

Jump to

Keyboard shortcuts

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