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.
Example ¶
One struct drives the flags, the environment and --help. The group with env-namespace DB reads DB_USER; a flag wins over the environment, the environment over the default.
package main
import (
"fmt"
"os"
"github.com/assanoff/skit/config"
)
type options struct {
Addr string `long:"addr" env:"HTTP_ADDR" default:":8080" description:"listen address"`
DB struct {
User string `long:"user" env:"USER" default:"app" description:"db user"`
} `group:"db" namespace:"db" env-namespace:"DB"`
}
func main() {
_ = os.Setenv("DB_USER", "reporter")
defer os.Unsetenv("DB_USER")
var opts options
_, err := config.NewParser(&opts).ParseArgs([]string{"--addr", ":9090"})
fmt.Println(opts.Addr, opts.DB.User, err)
}
Output: :9090 reporter <nil>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsHelp ¶
IsHelp reports whether err is the benign "help requested" error, which callers should treat as a clean exit.
func LoadDotenv ¶
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.
Example ¶
LoadDotenv seeds the environment from a .env file; a variable already set keeps its value.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/assanoff/skit/config"
)
func main() {
dir, _ := os.MkdirTemp("", "dotenv")
defer os.RemoveAll(dir)
env := filepath.Join(dir, ".env")
_ = os.WriteFile(env, []byte("HTTP_ADDR=:7070\nDB_USER=dev\n"), 0o600)
_ = os.Setenv("DB_USER", "ci")
defer os.Unsetenv("DB_USER")
defer os.Unsetenv("HTTP_ADDR")
if err := config.LoadDotenv(env, filepath.Join(dir, "missing.env")); err != nil {
fmt.Println(err)
}
fmt.Println(os.Getenv("HTTP_ADDR"), os.Getenv("DB_USER"))
}
Output: :7070 ci
func NewParser ¶
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 ¶
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.
Example ¶
Reset drops a secret from the environment once it is read, so it does not leak into child processes.
package main
import (
"fmt"
"os"
"github.com/assanoff/skit/config"
)
func main() {
_ = os.Setenv("DB_PASSWORD", "s3cret")
config.Reset("DB_PASSWORD")
_, ok := os.LookupEnv("DB_PASSWORD")
fmt.Println(ok)
}
Output: false
Types ¶
This section is empty.