Documentation
¶
Overview ¶
Package config provides helpers for populating configuration structs from environment variables, .env files, and JSON files. It builds on the caarlos0/env struct-tag conventions (`env:`, `envPrefix:`, `envDefault:`, ...) already used throughout platform-go's per-package config subpackages, giving callers a single, application-agnostic place to mount those tags onto their config values.
Index ¶
- func ApplyEnvironmentVariables(cfg any, opts ...Option) error
- func LoadFromDotEnvFile[T any](path string, opts ...Option) (*T, error)
- func LoadFromEnvironment[T any](opts ...Option) (*T, error)
- func LoadFromJSONFile[T any](path string, opts ...Option) (*T, error)
- func LoadFromTOMLFile[T any](path string, opts ...Option) (*T, error)
- func LoadFromYAMLFile[T any](path string, opts ...Option) (*T, error)
- func ResolveDotEnvPath(baseDir, filename string) (string, error)
- func Validate(ctx context.Context, cfg any) error
- type OnSetFunc
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyEnvironmentVariables ¶
ApplyEnvironmentVariables populates cfg (a non-nil pointer to a struct) from environment variables using the caarlos0/env struct tags, following that library's standard semantics:
- A field whose env var is set is assigned that value, overriding any value the field already held (e.g. one decoded from a file).
- A field with an envDefault whose env var is unset is assigned the default, which likewise overrides any pre-existing value. Because of this, when layering env vars on top of a decoded config, a field carrying an envDefault always ends up at either its env value or its default — never a value that came only from the file.
- A field with no env var and no envDefault is left untouched.
func LoadFromDotEnvFile ¶
LoadFromDotEnvFile loads the .env file at path into the process environment and then builds a *T from the environment. godotenv does not override variables already present in the process, so real environment values still take precedence over values in the file.
func LoadFromEnvironment ¶
LoadFromEnvironment builds a *T populated entirely from environment variables.
func LoadFromJSONFile ¶
LoadFromJSONFile decodes the JSON file at path into a *T, then overlays environment variables on top of it via ApplyEnvironmentVariables. A set env var takes precedence over the file value. Note the caarlos0/env caveat documented on ApplyEnvironmentVariables: a field carrying an envDefault whose env var is unset is reset to that default even if the file supplied a value, so give such fields their env var (or no envDefault) when the file should win.
func LoadFromTOMLFile ¶
LoadFromTOMLFile behaves like LoadFromJSONFile but decodes a TOML file (via BurntSushi/toml). TOML keys map to struct fields by their `toml:` tag, falling back to a case-insensitive field-name match.
func LoadFromYAMLFile ¶
LoadFromYAMLFile behaves like LoadFromJSONFile but decodes a YAML file (via gopkg.in/yaml.v3). YAML keys map to struct fields by their `yaml:` tag, falling back to the lower-cased field name.
func ResolveDotEnvPath ¶
ResolveDotEnvPath joins baseDir and filename and returns the result if that file exists. A missing file yields "" (and a nil error) so callers can treat "no .env present" as "skip loading" rather than a failure; any other stat error is returned.
func Validate ¶
Validate runs cfg's context-aware validation if cfg implements ozzo-validation's ValidatableWithContext; otherwise it is a no-op. It is a convenience for validating a freshly loaded config, particularly one built solely from the environment where there is no file baseline to fall back on.
Types ¶
type OnSetFunc ¶
OnSetFunc is invoked for each field the parser populates from the environment. It mirrors caarlos0/env's OnSet hook and is handy for debug logging which variables were applied. Wire it to any logger via WithOnSet.
type Option ¶
type Option func(*options)
Option configures how environment variables are applied.
func WithOnSet ¶
WithOnSet registers a hook invoked for each field populated from the environment. Passing nil is a no-op.
func WithPrefix ¶
WithPrefix sets a prefix prepended to every env var key the parser reads (e.g. "MYAPP_"). Nested envPrefix struct tags are appended after it.