Documentation ¶
Overview ¶
Package swap is an agnostic config parser (supporting YAML, TOML, JSON and environment vars) and a toolbox factory with automatic configuration based on your build environment.
Index ¶
- Variables
- func Parse(config interface{}, files ...string) (err error)
- func ParseByEnv(config interface{}, env *Environment, files ...string) (err error)
- func SetColoredLogs(enabled bool)
- type Builder
- type Configurable
- type Environment
- type EnvironmentHandler
- type Factory
- type FactoryFunc
- type Repository
- type Sources
Constants ¶
This section is empty.
Variables ¶
var DefaultEnvs = defaultEnvs{ Production: NewEnvironment("production", `(production)|(master)|(^v(0|[1-9]+)(\\.(0|[1-9]+)+)?(\\.(\\*|(0|[1-9]+)+))?$)`), Staging: NewEnvironment("staging", `(staging)|(release/*)|(hotfix/*)|(bugfix/*)`), Testing: NewEnvironment("testing", `(testing)|(test)`), Development: NewEnvironment("development", `(development)|(develop)|(dev)|(feature/*)`), Local: NewEnvironment("local", `local`), }
DefaultEnvs contains the default environment's configurations.
var FileSearchCaseSensitive bool
FileSearchCaseSensitive determine config files search mode, false by default.
Functions ¶
func Parse ¶
Parse strictly parse only the specified config files in the exact order they are into the config interface, one by one. The latest files will override the former. Will also parse fmt template keys in configs and struct flags.
func ParseByEnv ¶
func ParseByEnv(config interface{}, env *Environment, files ...string) (err error)
ParseByEnv parse all the passed files plus all the matched ones for the given Environment (if not nil) into the config interface. Environment specific files will override generic files. The latest files passed will override the former. Will also parse fmt template keys and struct flags.
func SetColoredLogs ¶
func SetColoredLogs(enabled bool)
SetColoredLogs enable / disable colors in the stdOut.
Types ¶
type Builder ¶
type Builder struct { EnvHandler *EnvironmentHandler DebugOptions debugOptions // contains filtered or unexported fields }
Builder recursively build/configure struct fields on the given struct, choosing the right configuration files based on the build environment.
func NewBuilder ¶
NewBuilder return a builder, a custom EnvHandler can be provided later.
func (*Builder) Build ¶
Build initialize and (eventually) configure the provided struct pointer looking for the config files in the provided configPath.
func (*Builder) RegisterType ¶
func (s *Builder) RegisterType(t reflect.Type, factory FactoryFunc) *Builder
RegisterType register a configurator func for a specific type and return the builder itself.
func (*Builder) WithCustomEnvHandler ¶
func (s *Builder) WithCustomEnvHandler(eh *EnvironmentHandler) *Builder
WithCustomEnvHandler return the same instance of the Builder but with the custom environmentHandler.
type Configurable ¶
Configurable interface allow the configuration of fields which are automatically initialized to their zero value.
type Environment ¶
type Environment struct {
// contains filtered or unexported fields
}
Environment struct represent an arbitrary environment with its tag and regexp (to detect it based on custom criterions).
func NewEnvironment ¶
func NewEnvironment(tag, regexp string) *Environment
NewEnvironment create a new instance of Environment. It will panic if an invalid regexp is provided or if the regexp does not match the primary tag.
`tag` is the primary environment tag and the part of the config files name that the config parser will look for while searching for environment specific files.
`regexp` must match all the tags we consider valid for the receiver.
func (*Environment) MatchTag ¶
func (e *Environment) MatchTag(tag string) bool
MatchTag return true if the environment regexp match the passed string.
func (*Environment) Tag ¶
func (e *Environment) Tag() string
Tag return the primary tag of the receiver.
type EnvironmentHandler ¶
type EnvironmentHandler struct { // Sources define the sources used to determine the current environment. Sources *Sources // contains filtered or unexported fields }
EnvironmentHandler is the object that manges the environment.
func NewEnvironmentHandler ¶
func NewEnvironmentHandler(environments []*Environment) *EnvironmentHandler
NewEnvironmentHandler return a new instance of environmentHandler with default Sources and the passed environments.
Sources define the sources used to determine the current environment. If DirectEnvironmentTag is empty then the system environment variable SystemEnvironmentTagKey will be checked, if also the system environment variable is empty the Git.BranchName will be used.
func (*EnvironmentHandler) Current ¶
func (eh *EnvironmentHandler) Current() *Environment
Current returns the current active environment by matching the found tag against any environments regexp.
func (*EnvironmentHandler) SetCurrent ¶
func (eh *EnvironmentHandler) SetCurrent(tag string)
SetCurrent set the current environment using a tag. It must be matched by one of the environments regexp.
type FactoryFunc ¶
FactoryFunc is the factory method type.
type Repository ¶
type Repository struct {
BranchName, Commit, Build, Tag string
Error error
// contains filtered or unexported fields
}
Repository represent a git repository.
func NewGitRepository ¶
func NewGitRepository(path string) *Repository
NewGitRepository return a new *Repository instance for the given path.
type Sources ¶
type Sources struct { // SystemEnvironmentTagKey is the system environment variable key // for the build environment tag, the default value is 'BUILD_ENV'. SystemEnvironmentTagKey string // Git is the project version control system. // The default path is './' (the working directory). Git *Repository // contains filtered or unexported fields }
Sources define alternative methods to obtain the current environment.