config

package
v0.2.21 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load[T any](opts *Options) (*T, error)

Load creates a new Config instance and immediately calls it's Load method.

Types

type Config

type Config[T any] struct {
	// contains filtered or unexported fields
}

Config provides loading of configuration values for a service.

func New

func New[T any](opts *Options) *Config[T]

New creates a new Config instance.

func (*Config[T]) Load added in v0.1.91

func (c *Config[T]) Load() (*T, error)

Load reads configuration using the given options, using it to populate a struct.

Configuration is first loaded from environment variables, then files, and finally Kubernetes mounts. The last loaded configuration value wins if any conflicts occur.

The struct should contains fields with "koanf" tags identifying the configuration key to assign. For example:

type LoggingConfig struct {
	// reads "level" into the field
	Level string `koanf:"level"`
}

The field type can be anything supported by mapstructure, or an implementation of encoding.TextUnmarshaler. If configuration keys contain ".", then nested structs must be used to access the value. For example:

type Config struct {
	Log struct{
		// reads "log.level" into the field
		Level string `koanf:"level"`
	} `koanf:"log"`
}

A default value can be set using a "default" struct tag with the desired value. For example:

type LoggingConfig struct {
	Level string `koanf:"level" default:"info"`
}

A default-able field type can be anything supported by defaults, or an implementation of defaults.Setter, encoding.TextUnmarshaler, or encoding/json.Unmarshaler.

type Options

type Options struct {
	// The prefix of environment variables to read configuration from. Matching environment
	// variables have the prefix removed, are converted to lowercase and any underscores ("_") are
	// replaced with periods ("."). For example, "APP_LOG_LEVEL" with a prefix of "APP_" would
	// become "log.level".
	EnvPrefix string

	// The file paths to read configuration from. Supported file formats and extensions are:
	//
	// - JSON: .json
	// - YAML: .yml, .yaml
	// - TOML: .toml
	//
	// The file content is read into a map where nested map keys are joined using ".". For example,
	// a JSON file containing {"log":{"level":"info"}} would become "log.level".
	Files []string

	// The directories of Kubernetes pod mounts to read configuration from. The filenames of the
	// mounted values become the configuration keys. Any underscores ("_") in the key are replaced
	// with periods ("."). For example, a configmap field of "log_level" would become "log.level".
	Mounts []string
}

Options provides the values to bootstrap configuration collection.

Directories

Path Synopsis
providers
Package splitter provides unmarshalers for Koanf config structs to convert strings to string slices using a particular delimiter.
Package splitter provides unmarshalers for Koanf config structs to convert strings to string slices using a particular delimiter.

Jump to

Keyboard shortcuts

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