Config

Stable

Layered, thread-safe configuration for Go

Used by LogWisp · Built on TOML

Thread-safe configuration for Go from defaults, TOML files, environment and flags, with configurable precedence and per-value source tracking.

  • Struct-based, typed and validated
  • Configurable precedence across sources
  • Reloads when the TOML file changes
  • Reports where each value came from

How a value is chosen

A program registers its settings once, as a struct with toml tags, and the defaults it registers fix each setting’s type. Four sources can then set a value, and each is loaded whole: parsed, every value checked against its type, then published in one step, or refused with the last good load kept.

Sources

  • Defaults a registered struct’s toml tags; each default’s type is the one a value must convert to
  • File TOML, a regular file, its owner and size checked on the open descriptor
  • Environment APP_SERVER_PORT for server.port: a prefix, a transform, a whitelist
  • Command line --port=9000 or --port 9000, a bare --debug; dotted paths too

Each load

  1. Parse the file through lixenwraith/toml; variables and flags stay text
  2. Check every value against its default’s type: overflow, sign, precision, durations, addresses
  3. Publish the whole source, under one lock; a failed load keeps the one before
  4. Watch: the file’s fingerprint, a quiet debounce, then the same load; a bad edit keeps the last good state

Precedence

CLI › env › file › defaultper path, unless SetPrecedence orders them otherwise; a key, variable or flag removed falls back to the next

Reads

  • AsStruct a detached snapshot, decoded once per change
  • GetTyped[T] one path, through the same checked conversions
  • Save a temporary file synced and renamed over the target
A reader never sees half a load: a source is checked whole, then swapped in under one lock.
Sources
Every path keeps what each source holds for it, not only the winner: GetSources shows them, so where a value came from is never a guess. A source loaded again replaces all of itself, so a variable or flag that went away stops counting.
Conversions
Text from a variable or a flag becomes the registered type or fails the load: overflow, a negative unsigned, a fraction for an integer and a float that loses precision are refused. Strings read as durations, RFC 3339 times, IP addresses, CIDR networks and URLs. An error names the path, never the value.
Snapshots
AsStruct returns a copy on every call, decoded once per change and shared with nothing, so a reader never holds a value a reload is changing. Build fills the struct it was given once and leaves it alone after.
Validation
Required paths and required:"true" fields, and validators on the whole config or on the typed struct, run at Build; helpers check ports, ranges, patterns and addresses.

Files, saves and reloads

A file is read only if it is a regular file, checked on the descriptor it was opened as, without blocking, so a FIFO swapped in fails at once. On Unix its owner can be required, and a size limit holds before and during the read. Save writes the effective values, or one source’s, to a temporary file beside the target, syncs it and renames it over; the target keeps its permissions, a new file gets 0600, and the comments loaded with it come back as a preamble.

The watcher fingerprints the file’s contents, waits for a quiet debounce, then runs the same load. An invalid edit, a timeout, a deleted file or a changed permission keep the last valid state, and a reload overtaken by a newer one never publishes. An event only says something changed; the reader takes the latest snapshot.

cfg, err := config.NewBuilder().
    WithTarget(&Settings{Port: 8080}).
    WithFile("service.toml").
    WithEnvPrefix("APP_").
    Build()