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
- Variables
- func AnyError(errors ...error) error
- func CoalesceBool(value *bool, defaultValue bool, inheritedValues ...bool) bool
- func CoalesceBytes(value, defaultValue []byte, inheritedValues ...[]byte) []byte
- func CoalesceDuration(value, defaultValue time.Duration, inheritedValues ...time.Duration) time.Duration
- func CoalesceFloat32(value, defaultValue float32, inheritedValues ...float32) float32
- func CoalesceFloat64(value, defaultValue float64, inheritedValues ...float64) float64
- func CoalesceInt(value, defaultValue int, inheritedValues ...int) int
- func CoalesceInt32(value, defaultValue int32, inheritedValues ...int32) int32
- func CoalesceInt64(value, defaultValue int64, inheritedValues ...int64) int64
- func CoalesceString(value, defaultValue string, inheritedValues ...string) string
- func CoalesceStrings(value, defaultValue []string, inheritedValues ...[]string) []string
- func CoalesceTime(value, defaultValue time.Time, inheritedValues ...time.Time) time.Time
- func IsConfigPathUnset(err error) bool
- func IsIgnored(err error) bool
- func IsInvalidConfigExtension(err error) bool
- func IsNotExist(err error) bool
- func Read(ref Any, options ...Option) (path string, err error)
- func SetBool(destination **bool, sources ...BoolSource) error
- func SetDuration(destination *time.Duration, sources ...DurationSource) error
- func SetFloat64(destination *float64, sources ...Float64Source) error
- func SetInt(destination *int, sources ...IntSource) error
- func SetString(destination *string, sources ...StringSource) error
- func SetStrings(destination *[]string, sources ...StringsSource) error
- type Any
- type BoolFunc
- type BoolSource
- type BoolValue
- type ConfigOptions
- type ConfigResolver
- type Duration
- type DurationFunc
- type DurationSource
- type Env
- type Float64
- type Float64Func
- type Float64Source
- type Int
- type IntFunc
- type IntSource
- type Labels
- type Option
- type String
- type StringFunc
- type StringSource
- type Strings
- type StringsFunc
- type StringsSource
- type Vars
Constants ¶
const ( // ErrConfigPathUnset is a common error. ErrConfigPathUnset = exception.Class("config path unset") // ErrInvalidConfigExtension is a common error. ErrInvalidConfigExtension = exception.Class("config extension invalid") )
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 ¶
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 CoalesceBool ¶
CoalesceBool returns a coalesced value.
func CoalesceBytes ¶
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 ¶
CoalesceFloat32 returns a coalesced value.
func CoalesceFloat64 ¶
CoalesceFloat64 returns a coalesced value.
func CoalesceInt ¶
CoalesceInt returns a coalesced value.
func CoalesceInt32 ¶
CoalesceInt32 returns a coalesced value.
func CoalesceInt64 ¶
CoalesceInt64 returns a coalesced value.
func CoalesceString ¶
CoalesceString returns a coalesced value.
func CoalesceStrings ¶
CoalesceStrings returns a coalesced value.
func CoalesceTime ¶
CoalesceTime returns a coalesced value.
func IsConfigPathUnset ¶
IsConfigPathUnset returns if an error is an ErrConfigPathUnset.
func IsInvalidConfigExtension ¶
IsInvalidConfigExtension returns if an error is an ErrInvalidConfigExtension.
func IsNotExist ¶
IsNotExist returns if an error is an os.ErrNotExist.
func Read ¶
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 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 BoolFunc ¶ added in v1.20201204.1
BoolFunc is a bool value source. It can be used with configutil.SetBool
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.
type ConfigOptions ¶ added in v1.20201204.1
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 DurationFunc ¶ added in v1.20201204.1
DurationFunc is a value source from a 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.
type Float64Func ¶ added in v1.20201204.1
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 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 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
OptAddPaths adds paths to the options
func OptResolver ¶
OptResolver sets an additional resolver for the config read.
type StringFunc ¶ added in v1.20201204.1
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 StringsFunc ¶ added in v1.20201204.1
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.