config

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package config standardizes 12-factor configuration on jessevdk/go-flags: a single options struct whose fields carry `long`/`env`/`default`/`description` tags, so the same definition drives CLI flags, environment variables, and `--help`.

Nested groups use `group`/`namespace`/`env-namespace` tags; the parser uses a "." flag delimiter and a "_" env-namespace delimiter, so a group with env-namespace "DB" and a field with env "USER" reads DB_USER. For local development LoadDotenv seeds the environment from a .env file, but real environment variables always take precedence.

Usage

Define one options struct, parse it, and treat a --help request as a clean exit:

type Config struct {
    Addr string `long:"addr" env:"HTTP_ADDR" default:":8080" description:"listen address"`
    DB   struct {
        User string `long:"user" env:"USER" description:"db user"`
    } `group:"db" namespace:"db" env-namespace:"DB"` // reads DB_USER
}

var cfg Config
if err := config.Parse(&cfg); err != nil {
    if config.IsHelp(err) {
        os.Exit(0)
    }
    log.Fatal(err)
}

After consuming a secret, drop it from the environment so it cannot leak into child processes or crash dumps:

config.Reset("DB_PASSWORD")

API

  • Parse(data, dotenvFiles...): load dotenv then parse os.Args into data, dispatching any matched `command:`-tagged subcommand's Execute.
  • NewParser(data): a preconfigured *flags.Parser for finer control.
  • LoadDotenv(files...): seed env from .env files (default ".env"); missing files are ignored and existing env vars are never overwritten.
  • IsHelp(err): reports the benign "help requested" error.
  • Reset(keys...): unset environment variables.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsHelp

func IsHelp(err error) bool

IsHelp reports whether err is the benign "help requested" error, which callers should treat as a clean exit.

func LoadDotenv

func LoadDotenv(files ...string) error

LoadDotenv loads the given .env files (default ".env") into the process environment if present. Missing files are ignored, and existing environment variables are never overwritten — in production values come from the orchestrator, not a file.

func NewParser

func NewParser(data any) *flags.Parser

NewParser builds a go-flags parser with a deterministic env-namespace delimiter ("_") so nested env names are predictable (DB_USER, HTTP_ADDR, ...). data is typically a pointer to an options struct, optionally containing `command:`-tagged subcommands that implement flags.Commander.

func Parse

func Parse(data any, dotenvFiles ...string) error

Parse loads the dotenv files (if any) then parses os.Args into data, dispatching any matched subcommand's Execute. It returns the go-flags error unchanged so callers can detect --help via IsHelp.

func Reset

func Reset(keys ...string)

Reset removes variables from the environment, e.g. to drop a secret after it has been consumed so it cannot leak into child processes or crash dumps.

Types

This section is empty.

Jump to

Keyboard shortcuts

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