Documentation ¶
Overview ¶
Package yetenv provides some logic to determine the environment (develop, staging, production).
Index ¶
- Variables
- type ConditionalLoadFunc
- type ConfigFileExtension
- type ConfigLoader
- func (c *ConfigLoader) LoadFromConditionalFile(filePath string, conditionFunc ConditionalLoadFunc) *ConfigLoader
- func (c *ConfigLoader) LoadFromFile(filePath string) *ConfigLoader
- func (c *ConfigLoader) LoadFromFileForEnvironment(environment Environment) *ConfigLoader
- func (c *ConfigLoader) LoadInto(cfg interface{}) error
- func (c *ConfigLoader) UseCustomLoadBehavior() *ConfigLoader
- func (c *ConfigLoader) UseDefaultLoadBehavior() *ConfigLoader
- func (c *ConfigLoader) UseEnvironment(environment Environment) *ConfigLoader
- func (c *ConfigLoader) UseFileNameForEnvironment(environment Environment, fileName string) *ConfigLoader
- func (c *ConfigLoader) UseFileProcessor(extension ConfigFileExtension) *ConfigLoader
- func (c *ConfigLoader) UseLoadBehavior(behavior LoadBehavior) *ConfigLoader
- func (c *ConfigLoader) UseLoadPath(path string) *ConfigLoader
- type Environment
- type LoadBehavior
Constants ¶
This section is empty.
Variables ¶
var DefaultConditionForDevelopEnvironment = func(configLoader *ConfigLoader, currentEnvironment Environment) bool { return currentEnvironment == Develop }
DefaultConditionForDevelopEnvironment retuns true when the current environment is Develop otherwise false.
var DefaultConditionForProductionEnvironment = func(configLoader *ConfigLoader, currentEnvironment Environment) bool { return currentEnvironment == Production }
DefaultConditionForProductionEnvironment returns true when the current environment is Production otherweise false.
var DefaultConditionForStagingEnvironment = func(configLoader *ConfigLoader, currentEnvironment Environment) bool { return currentEnvironment == Staging }
DefaultConditionForStagingEnvironment returns true when the current environment is Staging otherwise false.
var DefaultConditionForTestEnvironment = func(configLoader *ConfigLoader, currentEnvironment Environment) bool { return currentEnvironment == Test }
DefaultConditionForDevelopEnvironment retuns true when the current environment is Develop otherwise false.
var DefaultVariableName = "ENVIRONMENT"
DefaultVariableName defines the default name of the environment variable.
var (
ErrUnknownLoadBehavior = errors.New("load behavior is unknown - only default or custom load behavior is allowed")
)
Functions ¶
This section is empty.
Types ¶
type ConditionalLoadFunc ¶
type ConditionalLoadFunc func(configLoader *ConfigLoader, currentEnvironment Environment) bool
ConditionalLoadFunc allows to define a condition for a file to be loaded by the config loader.
type ConfigFileExtension ¶
type ConfigFileExtension string
ConfigFileExtension represents a possible config file extension which is usable by the config loader.
const ( YAML ConfigFileExtension = ".yaml" JSON ConfigFileExtension = ".json" TOML ConfigFileExtension = ".toml" DOTENV ConfigFileExtension = ".env" )
type ConfigLoader ¶
type ConfigLoader struct { LoadPath string FileExtension ConfigFileExtension ConfigFiles map[Environment]string Environment Environment LoadBehavior LoadBehavior // contains filtered or unexported fields }
ConfigLoader loads configuration values from files and the OS environment into a configuration struct. It uses the builder pattern and needs to be extecuted by the finishing method.
func NewConfigLoader ¶
func NewConfigLoader() *ConfigLoader
NewConfigLoader initializes a new ConfigLoader builder.
func (*ConfigLoader) LoadFromConditionalFile ¶
func (c *ConfigLoader) LoadFromConditionalFile(filePath string, conditionFunc ConditionalLoadFunc) *ConfigLoader
LoadFromConditionalFile can be used to load a config file only when the condition of the conditionFunc is met. It will not use the LoadPath, so the full path to config file should be provided.
func (*ConfigLoader) LoadFromFile ¶
func (c *ConfigLoader) LoadFromFile(filePath string) *ConfigLoader
LoadFromFile can be used to load a specific config file when using custom load behavior. It will not use the LoadPath, so the full path to config file should be provided.
func (*ConfigLoader) LoadFromFileForEnvironment ¶
func (c *ConfigLoader) LoadFromFileForEnvironment(environment Environment) *ConfigLoader
LoadFromFileForEnvironment can be used to reuse environmental load logic for a custom load behavior. For example: LoadFromFileForEnvironment(Develop) will behave the same as in the default load behavior.
func (*ConfigLoader) LoadInto ¶
func (c *ConfigLoader) LoadInto(cfg interface{}) error
LoadInto will finish the ConfigLoader and execute the load process. The provided config struct should be a pointer.
func (*ConfigLoader) UseCustomLoadBehavior ¶
func (c *ConfigLoader) UseCustomLoadBehavior() *ConfigLoader
UseCustomLoadBehavior sets load behavior to LoadBehaviorCustom.
func (*ConfigLoader) UseDefaultLoadBehavior ¶
func (c *ConfigLoader) UseDefaultLoadBehavior() *ConfigLoader
UseDefaultLoadBehavior sets load behavior to LoadBehaviorDefault.
func (*ConfigLoader) UseEnvironment ¶
func (c *ConfigLoader) UseEnvironment(environment Environment) *ConfigLoader
UseEnvironment can be used to change the current environment value of the ConfigLoader. Defaults to the value of the ENVIRONMENT variable.
func (*ConfigLoader) UseFileNameForEnvironment ¶
func (c *ConfigLoader) UseFileNameForEnvironment(environment Environment, fileName string) *ConfigLoader
UseFileNameForEnvironment can be used to change the config file name for a specific environment. Default file names are: Develop -> 'cfg.dev' Develop -> 'cfg.test' Staging -> 'cfg.staging' Production -> 'cfg.prod' Custom -> 'cfg' or '.env'
func (*ConfigLoader) UseFileProcessor ¶
func (c *ConfigLoader) UseFileProcessor(extension ConfigFileExtension) *ConfigLoader
UseFileProcessor can be used to change the file processor for config files when using the default load behavior. It defaults to "DOTENV".
func (*ConfigLoader) UseLoadBehavior ¶
func (c *ConfigLoader) UseLoadBehavior(behavior LoadBehavior) *ConfigLoader
UseLoadBehavior can be used to set a LoadBehavior to a specific value.
func (*ConfigLoader) UseLoadPath ¶
func (c *ConfigLoader) UseLoadPath(path string) *ConfigLoader
UseLoadPath can be used to change the load path for the default load behavior. It defaults to "./".
type Environment ¶
type Environment string
Environment defines the environment of an application (e.g. Develop, Staging, Production, etc.)
const ( Production Environment = "production" Staging Environment = "staging" Test Environment = "test" Develop Environment = "develop" Custom Environment = "custom" )
func GetEnvironment ¶
func GetEnvironment() Environment
GetEnvironment returns the current Environment value depending on the OS environment value of the variable defined by DefaultVariableName.
func GetEnvironmentFromVariable ¶
func GetEnvironmentFromVariable(variableName string) Environment
GetEnvironmentFromVariable returns the current Environment value depending on the OS environment value of the variable provided by the parameter.
type LoadBehavior ¶
type LoadBehavior int
LoadBehavior is used to define the load behavior of the config loader.
const ( LoadBehaviorUnknown LoadBehavior = iota LoadBehaviorDefault LoadBehaviorCustom )