config

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 9 Imported by: 6

Documentation

Overview

Package config provides application configuration loading and binding.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind[T any](c *T, prefix string, v *viper.Viper) *T

Bind configuration section if it implements Binder interface.

func LoadRemoteSecret

func LoadRemoteSecret(name string) (string, error)

LoadRemoteSecret loads a remote secret from configuration provided in environment variable.

Environment variable name is expected to be in the format:

<name>_FILE - path to the file containing the secret

Types

type Binder

type Binder interface {
	Bind(prefix string, v *viper.Viper)
}

Binder is an interface that can be implemented by configuration sections to bind to configuration file.

type Cache

type Cache struct {
	Type             cache.Type    `mapstructure:"type" validate:"required,oneof=memory redis redis-cluster redis-sentinel"`
	TTL              time.Duration `mapstructure:"ttl" validate:"omitempty,min=0"`
	ConnectionString string        `mapstructure:"connection" validate:"omitempty"`
	Password         string        `mapstructure:"password" validate:"omitempty"`
	KeyPrefix        string        `mapstructure:"key_prefix" validate:"omitempty"`
}

Cache is the cache configuration section.

func (*Cache) Bind

func (c *Cache) Bind(prefix string, v *viper.Viper)

Bind cache configuration section.

func (*Cache) Validate

func (c *Cache) Validate(valid *validation.Validate) error

Validate cache configuration section.

type CmdBinder added in v0.3.0

type CmdBinder interface {
	BindCmd(cmd *cobra.Command, v *viper.Viper)
}

CmdBinder is an interface that can be implemented by configuration sections to bind to command line arguments.

type Configurable

type Configurable interface {
	Core() *Configuration
	Loaded(conf *Configuration)
}

Configurable is an interface that can be implemented by extended configuration.

type Configuration

type Configuration struct {

	// Cache configuration section.
	Cache *Cache
	// Log configuration section.
	Log *Log
	// contains filtered or unexported fields
}

Configuration for the application.

func New

func New() *Configuration

New returns a new configuration.

func (*Configuration) AddModifier added in v0.30.0

func (c *Configuration) AddModifier(fn Modifier)

AddModifier registers a function that will be called during Load to modify/configure the underlying configuration engine. Use this to set defaults, bind environment variables, or otherwise fine-tune settings based on runtime info.

func (*Configuration) Bind

func (c *Configuration) Bind(_ string, v *viper.Viper)

Bind configuration section to the configuration backend/instance.

func (*Configuration) Core

func (c *Configuration) Core() *Configuration

Core returns the core configuration.

func (*Configuration) Load

func (c *Configuration) Load(cmd *cobra.Command, config any, environment string) error

Load loads the configuration from the provided path.

func (*Configuration) Loaded

func (c *Configuration) Loaded(*Configuration)

Loaded receives loaded core configuration.

func (*Configuration) Ready

func (c *Configuration) Ready() bool

Ready returns true if the configuration has been loaded.

func (*Configuration) SetConfigDirName

func (c *Configuration) SetConfigDirName(dirName string)

SetConfigDirName sets the name of the directory where the config file is located under common config locations.

func (*Configuration) SetConfigFile

func (c *Configuration) SetConfigFile(path string)

SetConfigFile explicitly defines the path, name and extension of the config file.

func (*Configuration) SetConfigName

func (c *Configuration) SetConfigName(name string)

SetConfigName sets name for the config file. Does not include extension.

func (*Configuration) Validate

func (c *Configuration) Validate(validate *validation.Validate) error

Validate the configuration.

type Log added in v0.21.0

type Log struct {
	// Type of the logger (defaults to `console`, allowed also `file` or other registered logging targets).
	Type string `mapstructure:"type" validate:"omitempty"`
	// Level of the logging output (defaults to `debug` in development environment and `info` in staging and production).
	Level string `mapstructure:"level" validate:"omitempty,oneof=debug info warn error dpanic panic fatal"`
	// Format of the logging output (defaults to `console` in development environment and `ecsjson` in staging and production).
	Format string `mapstructure:"format" validate:"omitempty,oneof=console json ecsjson"`
	// Output location (defaults to stderr)
	Output string `mapstructure:"output"`
	// Stacktrace enables stack traces for error level and above regardless of environment.
	// Can be set via LOG_STACKTRACE environment variable.
	Stacktrace bool `mapstructure:"stacktrace"`

	// Secondary logging output configuration.
	Secondary *Logger `mapstructure:"secondary" validate:"omitempty"`
}

Log configuration section.

func (*Log) Bind added in v0.21.0

func (c *Log) Bind(prefix string, v *viper.Viper)

Bind logger configuration section.

func (*Log) Validate added in v0.21.0

func (c *Log) Validate(valid *validation.Validate) error

Validate logger configuration section.

type Logger added in v0.22.0

type Logger struct {
	// Type of the logger (allowed values are `console`, `file` and other registered logging targets).
	Type string `mapstructure:"type" validate:"required"`
	// Level of the logging output (defaults to `info`).
	Level string `mapstructure:"level" validate:"omitempty,oneof=debug info warn error dpanic panic fatal"`
	// Format of the logging output (defaults to `console` in development environment and `ecsjson` in staging and production).
	Format string `mapstructure:"format" validate:"omitempty,oneof=console json ecsjson"`
	// Output location (type sepcific output location).
	Output string `mapstructure:"output" validate:"omitempty"`
}

Logger represents the logging output configuration.

type Modifier added in v0.30.0

type Modifier func(v *viper.Viper)

Modifier is a function that receives the underlying configuration backend/instance and may call SetDefault, BindEnv, or adjust other settings. It is exposed so other packages (for example server) can accept these as options without importing the configuration backend directly.

type Validatable

type Validatable interface {
	Validate(v *validation.Validate) error
}

Validatable is an interface that can be implemented by configuration section to validate the configuration.

Jump to

Keyboard shortcuts

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