Documentation ¶
Overview ¶
Package envexist provides few tools to validate env vars based on modules
Each module registers itself to envexist (typically in init()), and waits the result in a callback. The application entry (main() in package main) takes responsibility to call envexist.Parse(), which validates env vars and triggers further initialization.
Package envexist IS NOT (and cannot be) thread-safe.
Example ¶
// reset envexist as we're in test environment, you should not need this Release() // simulate os.Setenv("MYMODULE_PARAM_2", "中文") m, ch := Main("MYMODULE") // there will be an asterisk before the Name column m.Need("PARAM_1", "a super detailed and descriptive decription which introduces how this variable should be and what it should do", "example value") // nothing special m.Want("PARAM_2", "desc", "example") // there will be an asterisk before the Example column m.May("PARAM_3", "the value in example works as default value", "default_Value") if !Parse() { PrintEnvList() return } // consume your data here env := <-ch param1 := env["PARAM_1"] _ = param1
Output: +----------------------+----------------------+---------------------------+-----------------+ | Name | Value | Description | Example | +----------------------+----------------------+---------------------------+-----------------+ |*MYMODULE_PARAM_1 | | a super detailed and desc | example value | | | | riptive decription which | | | | | introduces how this varia | | | | | ble should be and what it | | | | | should do | | +----------------------+----------------------+---------------------------+-----------------+ | MYMODULE_PARAM_2 | 中文 | desc | example | +----------------------+----------------------+---------------------------+-----------------+ | MYMODULE_PARAM_3 | | the value in example work |*default_Value | | | | s as default value | | +----------------------+----------------------+---------------------------+-----------------+
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
func Parse() (ok bool)
Parse checks all required env vars are set, and pass final data through channel
It blocks until first error or all callbacks executed, that's why you should not use New() in application entry.
Env vars are passed only if this returns true.
YOU MUST NOT CALL New() or Main() AFTER THIS.
func PrintEnvList ¶
func PrintEnvList()
PrintEnvList lists all registered env vars with fmt.Print
TODO: customizable formatting and output writer
Types ¶
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module represents a set of env vars
func Main ¶
Main registers a new module suitable for main application entry
The env var will passed through data after calling Parse()
It calls New() with a callback, which pushes data into ch. As ch is a buffered channel, Parse() can return before you retrieve the result from ch.
func New ¶
New registers a new module suitable for libraries
The env var will passed through data after calling Parse()
It ALWAYS converts name to uppercase.
func (*Module) May ¶
May registers an optional env var with default value in example
It ALWAYS converts name to uppercase.
You can always get some value if Parse() returns true. If corresponding envvar is not set, default value is used instead.
func (*Module) Need ¶
Need registers a required env var
It ALWAYS converts name to uppercase.
You have to set corresponding envvar or Parse() will fail.
When handling returned data in callback (or channel), it is guaranteed that _, ok := data[name] to be true.