Documentation
¶
Overview ¶
Package envcfg reads typed values from the process environment. Each helper falls back to a caller-supplied default when the variable is unset or unparseable, and logs a warning so a typo (e.g. PORT=eight-five) is visible in container logs instead of silently using the default.
Single-call usage from main():
port := envcfg.Int("PORT", 8085)
The package is intentionally tiny — every HB Labs service reads its config from a handful of vars, not a YAML schema, so we don't need viper/koanf machinery.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bool ¶
Bool returns the boolean value of the env var named key, or def when unset / unparseable. Accepts the strconv.ParseBool grammar (1, t, T, TRUE, true, True / 0, f, F, FALSE, false, False).
func ByteSize ¶
ByteSize returns a byte count parsed from a Kubernetes-style quantity in the env var named key, falling back to def when unset, unparseable, or non-positive. Accepts binary suffixes (Ki, Mi, Gi, Ti), decimal suffixes (k, M, G, T), or a bare integer (bytes). Same convention as a k8s manifest's resource requests, so "1Mi", "512Ki", "10M" and "1048576" are all valid.
func Duration ¶
Duration returns the Go-duration value of the env var named key, constrained to be strictly positive. Falls back to def when the var is unset, unparseable, or non-positive — non-positive values are almost always a misconfiguration (a zero interval loops hot, a negative one is nonsense) so the helper rejects them with a warning rather than passing them through.
Format follows time.ParseDuration: "30s", "5m", "1h30m", etc.
func Expand ¶
Expand replaces every "${VAR}" and "${VAR:-default}" reference in s with values from the process environment, leaving bare "$NAME" alone. Used by config / catalog loaders that want per-environment overrides without dragging in a templating engine.
Resolution: a set, non-empty env var wins; otherwise the ":-default" (if any) is used; otherwise the substitution is the empty string. An unterminated "${" passes through unchanged so a stray "$" in user copy doesn't take down the boot.
We hand-roll the lexer (rather than reusing os.Expand) because os.Expand always consumes "$NAME" without braces too, with no way to opt out — and YAML scalars legitimately contain literal "$" (CSS selectors, jQuery, currency strings) that we don't want silently swallowed.
func Int ¶
Int returns the integer value of the env var named key, falling back to def when unset or invalid. A non-integer value logs a warning so the operator sees the typo without the program dying.
func PositiveInt ¶
PositiveInt returns the integer value of the env var named key, constrained to be strictly positive (> 0). Falls back to def when the var is unset, unparseable, or non-positive — and logs a warning in the non-positive case so a misconfiguration like `HB_BATCH_SIZE=0` is visible in container logs instead of silently looping over a zero-sized batch forever.
Use when a value's domain semantically excludes zero / negatives (batch sizes, page caps, worker counts). For values where zero is meaningful (disable a feature, "no cap"), reach for Int.
Types ¶
This section is empty.