config

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 11 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 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:

  1. explicit --config path (missing file is an error)
  2. home dotfile: ~/.APPNAME.yaml
  3. user config dir: %APPDATA%/APPNAME/APPNAME.yaml, ~/.config/APPNAME/APPNAME.yaml, ~/Library/Application Support/...
  4. 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

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 Load

func Load(opts Options) (*Config, error)

Load layers defaults, files, env, and overrides in that order.

func (*Config) All

func (c *Config) All() map[string]any

All returns every layered key for dumps and debugging.

func (*Config) Bool

func (c *Config) Bool(path string) bool

Bool returns the value at path as a bool, false when absent.

func (*Config) Exists

func (c *Config) Exists(path string) bool

Exists reports whether the dotted path is set.

func (*Config) Float64

func (c *Config) Float64(path string) float64

Float64 returns the value at path as a float64, 0 when absent.

func (*Config) Get

func (c *Config) Get(path string) any

Get returns the value at the dotted path, or nil when absent.

func (*Config) Int

func (c *Config) Int(path string) int

Int returns the value at path as an int, 0 when absent.

func (*Config) String

func (c *Config) String(path string) string

String returns the value at path as a string, "" when absent.

func (*Config) Strings

func (c *Config) Strings(path string) []string

Strings returns the value at path as a string slice.

func (*Config) Unmarshal

func (c *Config) Unmarshal(target any) error

Unmarshal decodes into target with weak typing (env strings convert). Unknown keys are ignored; use UnmarshalStrict to reject them.

func (*Config) UnmarshalStrict

func (c *Config) UnmarshalStrict(target any) error

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.

Jump to

Keyboard shortcuts

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