configutil

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2018 License: MIT Imports: 8 Imported by: 15

Documentation

Overview

Package configutil contains helpers for reading configuration for programs. It contains defaults for common config locations, and reads the files as yaml or json into a config struct you create for your program.

Index

Constants

View Source
const (
	// EnvVarConfigPath is the env var for configs.
	EnvVarConfigPath = "CONFIG_PATH"
	// ExtensionJSON is a file extension.
	ExtensionJSON = ".json"
	// ExtensionYAML is a file extension.
	ExtensionYAML = ".yaml"
	// ExtensionYML is a file extension.
	ExtensionYML = ".yml"
)
View Source
const (
	// ErrConfigPathUnset is a common error.
	ErrConfigPathUnset = exception.Class("config path unset")

	// ErrInvalidConfigExtension is a common error.
	ErrInvalidConfigExtension = exception.Class("config extension invalid")
)

Variables

View Source
var (
	// DefaultPaths are default path locations.
	// They are tested and read in order, so the later
	// paths will override data found in the earlier ones.
	DefaultPaths = []string{
		"/var/secrets/config.yml",
		"/var/secrets/config.yaml",
		"/var/secrets/config.json",
		"./_config/config.yml",
		"./_config/config.yaml",
		"./_config/config.json",
		"./config.yml",
		"./config.yaml",
		"./config.json",
	}
)

Functions

func Deserialize

func Deserialize(ext string, r io.Reader, ref Any) error

Deserialize deserializes a config.

func IsConfigPathUnset

func IsConfigPathUnset(err error) bool

IsConfigPathUnset returns if an error is an ErrConfigPathUnset.

func IsIgnored

func IsIgnored(err error) bool

IsIgnored returns if we should ignore the config read error.

func IsInvalidConfigExtension

func IsInvalidConfigExtension(err error) bool

IsInvalidConfigExtension returns if an error is an ErrInvalidConfigExtension.

func IsNotExist

func IsNotExist(err error) bool

IsNotExist returns if an error is an os.ErrNotExist.

func Paths

func Paths(defaults ...string) (output []string)

Paths returns config paths. The results are the provided defaults and the `CONFIG_PATH` environment variable as a csv if it's set.

func PathsWithDefaults

func PathsWithDefaults(paths ...string) []string

PathsWithDefaults returns the default paths and additional optional paths.

func Read

func Read(ref Any, paths ...string) error

Read reads a config from optional path(s). Paths will be tested from a standard set of defaults (ex. config.yml) and optionally a csv named in the `CONFIG_PATH` environment variable.

func ReadFromReader

func ReadFromReader(ref Any, r io.Reader, ext string) error

ReadFromReader reads a config from a given reader.

func TryReadFromPaths

func TryReadFromPaths(ref Any, paths ...string) (path string, err error)

TryReadFromPaths tries to read the config from a list of given paths, reading from the first file that exists.

Types

type Any

type Any = interface{}

Any is a loose type alias to interface{}.

type Labels

type Labels = map[string]string

Labels is a loose type alias to map[string]string

type Vars

type Vars = map[string]interface{}

Vars is a loose type alias to map[string]string

Jump to

Keyboard shortcuts

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