configutil

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2019 License: MIT Imports: 9 Imported by: 15

Documentation

Overview

Package configutil contains helpers for reading and setting up configuration. It contains defaults for common config locations, and reads the files as yaml or json into a config struct you create for your program. It also has helpers to realize config from various sources like the environment or cli flags.

Index

Constants

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

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 AnyError added in v1.20201204.1

func AnyError(errors ...error) error

AnyError returns the first non-nil error.

func CoalesceBool

func CoalesceBool(value *bool, defaultValue bool, inheritedValues ...bool) bool

CoalesceBool returns a coalesced value.

func CoalesceBytes

func CoalesceBytes(value, defaultValue []byte, inheritedValues ...[]byte) []byte

CoalesceBytes returns a coalesced value.

func CoalesceDuration

func CoalesceDuration(value, defaultValue time.Duration, inheritedValues ...time.Duration) time.Duration

CoalesceDuration returns a coalesced value.

func CoalesceFloat32

func CoalesceFloat32(value, defaultValue float32, inheritedValues ...float32) float32

CoalesceFloat32 returns a coalesced value.

func CoalesceFloat64

func CoalesceFloat64(value, defaultValue float64, inheritedValues ...float64) float64

CoalesceFloat64 returns a coalesced value.

func CoalesceInt

func CoalesceInt(value, defaultValue int, inheritedValues ...int) int

CoalesceInt returns a coalesced value.

func CoalesceInt32

func CoalesceInt32(value, defaultValue int32, inheritedValues ...int32) int32

CoalesceInt32 returns a coalesced value.

func CoalesceInt64

func CoalesceInt64(value, defaultValue int64, inheritedValues ...int64) int64

CoalesceInt64 returns a coalesced value.

func CoalesceString

func CoalesceString(value, defaultValue string, inheritedValues ...string) string

CoalesceString returns a coalesced value.

func CoalesceStrings

func CoalesceStrings(value, defaultValue []string, inheritedValues ...[]string) []string

CoalesceStrings returns a coalesced value.

func CoalesceTime

func CoalesceTime(value, defaultValue time.Time, inheritedValues ...time.Time) time.Time

CoalesceTime returns a coalesced value.

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 Read

func Read(ref Any, options ...Option) (path string, err 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 SetBool added in v1.20201204.1

func SetBool(destination **bool, sources ...BoolSource) error

SetBool coalesces a given list of sources into a variable.

func SetDuration added in v1.20201204.1

func SetDuration(destination *time.Duration, sources ...DurationSource) error

SetDuration coalesces a given list of sources into a variable.

func SetFloat64 added in v1.20201204.1

func SetFloat64(destination *float64, sources ...Float64Source) error

SetFloat64 coalesces a given list of sources into a variable.

func SetInt added in v1.20201204.1

func SetInt(destination *int, sources ...IntSource) error

SetInt coalesces a given list of sources into a variable.

func SetString added in v1.20201204.1

func SetString(destination *string, sources ...StringSource) error

SetString coalesces a given list of sources into a variable.

func SetStrings added in v1.20201204.1

func SetStrings(destination *[]string, sources ...StringsSource) error

SetStrings coalesces a given list of sources into a variable.

Types

type Any

type Any = interface{}

Any is a loose type alias to interface{}.

type BoolFunc added in v1.20201204.1

type BoolFunc func() (*bool, error)

BoolFunc is a bool value source. It can be used with configutil.SetBool

func (BoolFunc) Bool added in v1.20201204.1

func (vf BoolFunc) Bool() (*bool, error)

Bool returns an invocation of the function.

type BoolSource added in v1.20201204.1

type BoolSource interface {
	// Bool should return a bool if the source has a given value.
	// It should return nil if the value is not found.
	// It should return an error if there was a problem fetching the value.
	Bool() (*bool, error)
}

BoolSource is a type that can return a value.

type BoolValue added in v1.20201204.1

type BoolValue bool

BoolValue implements value provider.

func Bool added in v1.20201204.1

func Bool(value *bool) *BoolValue

Bool returns a BoolValue for a given value.

func (*BoolValue) Bool added in v1.20201204.1

func (b *BoolValue) Bool() (*bool, error)

Bool returns the value for a constant.

type ConfigOptions added in v1.20201204.1

type ConfigOptions struct {
	Resolver func(interface{}) error
	Paths    []string
}

ConfigOptions are options built for reading configs.

type ConfigResolver added in v1.20201204.1

type ConfigResolver interface {
	Resolve() error
}

ConfigResolver is a type that can be resolved.

type Duration added in v1.20201204.1

type Duration time.Duration

Duration implements value provider.

func (Duration) Duration added in v1.20201204.1

func (dc Duration) Duration() (*time.Duration, error)

Duration returns the value for a constant.

type DurationFunc added in v1.20201204.1

type DurationFunc func() (*time.Duration, error)

DurationFunc is a value source from a function.

func (DurationFunc) Duration added in v1.20201204.1

func (vf DurationFunc) Duration() (*time.Duration, error)

Duration returns an invocation of the function.

type DurationSource added in v1.20201204.1

type DurationSource interface {
	// Duration should return a time.Duration if the source has a given value.
	// It should return nil if the value is not present.
	// It should return an error if there was a problem fetching the value.
	Duration() (*time.Duration, error)
}

DurationSource is a type that can return a time.Duration value.

type Env added in v1.20201204.1

type Env string

Env is a value provider where the string represents the environment variable name. It can be used with *any* config.Set___ type.

func (Env) Bool

func (e Env) Bool() (*bool, error)

Bool returns a given environment variable as a bool.

func (Env) Duration

func (e Env) Duration() (*time.Duration, error)

Duration returns a given environment variable as a time.Duration.

func (Env) Float64

func (e Env) Float64() (*float64, error)

Float64 returns a given environment variable as a float64.

func (Env) Int

func (e Env) Int() (*int, error)

Int returns a given environment variable as an int.

func (Env) String

func (e Env) String() (*string, error)

String returns a given environment variable as a string.

func (Env) Strings

func (e Env) Strings() ([]string, error)

Strings returns a given environment variable as strings.

type Float64 added in v1.20201204.1

type Float64 float64

Float64 implements value provider.

func (Float64) Float64 added in v1.20201204.1

func (f Float64) Float64() (*float64, error)

Float64 returns the value for a constant.

type Float64Func added in v1.20201204.1

type Float64Func func() (*float64, error)

Float64Func is a float value source from a commandline flag.

func (Float64Func) Float64 added in v1.20201204.1

func (vf Float64Func) Float64() (*float64, error)

Float64 returns an invocation of the function.

type Float64Source added in v1.20201204.1

type Float64Source interface {
	// Float should return a float64 if the source has a given value.
	// It should return nil if the value is not found.
	// It should return an error if there was a problem fetching the value.
	Float64() (*float64, error)
}

Float64Source is a type that can return a value.

type Int added in v1.20201204.1

type Int int

Int implements value provider.

func (Int) Int added in v1.20201204.1

func (i Int) Int() (*int, error)

Int returns the value for a constant.

type IntFunc added in v1.20201204.1

type IntFunc func() (*int, error)

IntFunc is an int value source from a commandline flag.

func (IntFunc) Int added in v1.20201204.1

func (vf IntFunc) Int() (*int, error)

Int returns an invocation of the function.

type IntSource added in v1.20201204.1

type IntSource interface {
	// Int should return a int if the source has a given value.
	// It should return nil if the value is not found.
	// It should return an error if there was a problem fetching the value.
	Int() (*int, error)
}

IntSource is a type that can return a value.

type Labels

type Labels = map[string]string

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

type Option added in v1.20201204.1

type Option func(*ConfigOptions) error

Option is a modification of config options.

func OptAddPaths added in v1.20210103.1

func OptAddPaths(paths ...string) Option

OptAddPaths adds paths to the options

func OptResolver

func OptResolver(resolver func(interface{}) error) Option

OptResolver sets an additional resolver for the config read.

func OptSetPaths

func OptSetPaths(paths ...string) Option

OptSetPaths adds paths to the options

type String added in v1.20201204.1

type String string

String implements value provider.

func (String) String added in v1.20201204.1

func (s String) String() (*string, error)

StringValue returns the value for a constant.

type StringFunc added in v1.20201204.1

type StringFunc func() (*string, error)

StringFunc is a value source from a function.

func (StringFunc) String added in v1.20201204.1

func (svf StringFunc) String() (*string, error)

String returns an invocation of the function.

type StringSource added in v1.20201204.1

type StringSource interface {
	// String should return a string if the source has a given value.
	// It should return nil if the value is not present.
	// It should return an error if there was a problem fetching the value.
	String() (*string, error)
}

StringSource is a type that can return a value.

type Strings added in v1.20201204.1

type Strings []string

Strings implements a value provider.

func (Strings) Strings added in v1.20201204.1

func (s Strings) Strings() ([]string, error)

Strings returns the value for a constant.

type StringsFunc added in v1.20201204.1

type StringsFunc func() ([]string, error)

StringsFunc is a value source from a function.

func (StringsFunc) Strings added in v1.20201204.1

func (svf StringsFunc) Strings() ([]string, error)

Strings returns an invocation of the function.

type StringsSource added in v1.20201204.1

type StringsSource interface {
	// Strings should return a string array if the source has a given value.
	// It should return nil if the value is not present.
	// It should return an error if there was a problem fetching the value.
	Strings() ([]string, error)
}

StringsSource is a type that can return a value.

type Vars

type Vars = map[string]interface{}

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

Directories

Path Synopsis
_examples
config command

Jump to

Keyboard shortcuts

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