envcfg

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func Bool(key string, def bool) 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

func ByteSize(key string, def int64) int64

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

func Duration(key string, def time.Duration) time.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

func Expand(s string) string

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

func Int(key string, def int) 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

func PositiveInt(key string, def int) int

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.

func String

func String(key, def string) string

String returns the trimmed value of the env var named key, or def if the var is empty / unset. Whitespace-only values are treated as empty so a stray space in a .env file doesn't beat the default.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL