Documentation
¶
Overview ¶
Package config loads layered configuration for terminull apps.
Precedence, low to high: Defaults, config files, environment, Overrides (parsed CLI flags, handed over by package app).
Files are searched in order and every existing file is layered, so project-local settings win over user-global ones:
- explicit --config path (missing file is an error)
- home dotfile: ~/.APPNAME.yaml
- user config dir: %APPDATA%/APPNAME/APPNAME.yaml, ~/.config/APPNAME/APPNAME.yaml, ~/Library/Application Support/...
- working directory: ./APPNAME.yaml then ./.APPNAME.yaml
Environment variables use APPNAME_ prefixes: APPNAME_DB_HOST becomes db.host. Numeric-looking and true/false values become typed.
Unknown keys fail loudly, but only at UnmarshalStrict time: typos in files or env surface as errors naming the key instead of silent zero values.
Index ¶
- type Config
- func (c *Config) All() map[string]any
- func (c *Config) Bool(path string) bool
- func (c *Config) Exists(path string) bool
- func (c *Config) Float64(path string) float64
- func (c *Config) Get(path string) any
- func (c *Config) Int(path string) int
- func (c *Config) String(path string) string
- func (c *Config) Strings(path string) []string
- func (c *Config) Unmarshal(target any) error
- func (c *Config) UnmarshalStrict(target any) error
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config wraps the layered koanf instance.
func (*Config) Unmarshal ¶
Unmarshal decodes into target with weak typing (env strings convert). Unknown keys are ignored; use UnmarshalStrict to reject them.
func (*Config) UnmarshalStrict ¶
UnmarshalStrict decodes like Unmarshal but fails naming any key that has no matching struct field, catching typos in files and env.
type Options ¶
type Options struct {
// AppName drives file names and the env prefix: "deploy" looks for
// deploy.yaml and reads DEPLOY_ variables.
AppName string
// ConfigFile is an explicit --config path. Missing file is an error.
ConfigFile string
// Defaults is the lowest-precedence layer.
Defaults map[string]any
// Overrides is the highest-precedence layer (parsed CLI flags).
Overrides map[string]any
// EnvPrefix overrides the derived UPPER(AppName)+"_" prefix.
EnvPrefix string
// Environ injects environment lines for tests; nil uses os.Environ.
Environ []string
// EnvironFunc injects an environ function; takes precedence over Environ.
EnvironFunc func() []string
}
Options tunes a Load call. Zero value loads nothing but empty defaults.