config

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 15 Imported by: 0

README

Config

Robust configuration management wrapping spf13/viper with type safety and interface-based testability.

Features:

  • Mockable Containable interface
  • Automatic environment variable mapping
  • Built-in observer pattern for file watching
  • Support for YAML, embedded assets, and more

For detailed documentation, usage examples, and testing strategies, see the Config Component Documentation.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoFilesFound = errors.Newf("no configuration files found please run init, or provide a config file using the --config flag")
)

Functions

func LoadEnv

func LoadEnv(fs afero.Fs, logger *log.Logger)

LoadEnv loads environment variables from a .env file if it exists.

Types

type Containable

type Containable interface {
	Get(key string) any
	GetBool(key string) bool
	GetInt(key string) int
	GetFloat(key string) float64
	GetString(key string) string
	GetTime(key string) time.Time
	GetDuration(key string) time.Duration
	GetViper() *viper.Viper
	Has(key string) bool
	IsSet(key string) bool
	Set(key string, value any)
	WriteConfigAs(dest string) error
	Sub(key string) Containable
	AddObserver(o Observable)
	AddObserverFunc(f func(Containable, chan error))
	ToJSON() string
	Dump()
}

func Load

func Load(paths []string, fs afero.Fs, logger *log.Logger, allowEmptyConfig bool) (Containable, error)

func LoadEmbed

func LoadEmbed(paths []string, assets fs.FS, logger *log.Logger) (Containable, error)

func LoadFilesContainer

func LoadFilesContainer(l *log.Logger, fs afero.Fs, configFiles ...string) (Containable, error)

LoadFilesContainer loads configuration from files and returns a Containable. It returns an error if the first file specified does not exist.

type Container

type Container struct {
	ID string
	// contains filtered or unexported fields
}

Container container for configuration.

func NewContainerFromViper

func NewContainerFromViper(l *log.Logger, v *viper.Viper) *Container

NewContainerFromViper creates a new Container from an existing Viper instance.

func NewFilesContainer

func NewFilesContainer(l *log.Logger, fs afero.Fs, configFiles ...string) *Container

NewFilesContainer Initialise configuration container to read files from the FS.

func NewReaderContainer

func NewReaderContainer(l *log.Logger, format string, configReaders ...io.Reader) *Container

NewReaderContainer Initialise configuration container to read config from ioReader.

func (*Container) AddObserver

func (c *Container) AddObserver(o Observable)

AddObserver attach observer to trigger on config update.

func (*Container) AddObserverFunc

func (c *Container) AddObserverFunc(f func(Containable, chan error))

AddObserverFunc attach function to trigger on config update.

func (*Container) Dump

func (c *Container) Dump()

func (*Container) Get

func (c *Container) Get(key string) any

Get interface value from config.

func (*Container) GetBool

func (c *Container) GetBool(key string) bool

GetBool get Bool value from config.

func (*Container) GetDuration

func (c *Container) GetDuration(key string) time.Duration

GetDuration get duration value from config.

func (*Container) GetFloat

func (c *Container) GetFloat(key string) float64

GetFloat get Float value from config.

func (*Container) GetInt

func (c *Container) GetInt(key string) int

GetInt get Bool value from config.

func (*Container) GetObservers

func (c *Container) GetObservers() []Observable

GetObservers retrieve all currently attached Observers.

func (*Container) GetString

func (c *Container) GetString(key string) string

GetString get string value from config.

func (*Container) GetTime

func (c *Container) GetTime(key string) time.Time

GetTime get time value from config.

func (*Container) GetViper

func (c *Container) GetViper() *viper.Viper

GetViper retrieves the underlying Viper configuration.

func (*Container) Has

func (c *Container) Has(key string) bool

Has retrieves the underlying Viper configuration.

func (*Container) IsSet

func (c *Container) IsSet(key string) bool

IsSet checks if the key has been set.

func (*Container) Set

func (c *Container) Set(key string, value any)

Set sets the value for the given key.

func (*Container) Sub

func (c *Container) Sub(key string) Containable

Sub returns a subtree of the parent configuration.

func (*Container) ToJSON

func (c *Container) ToJSON() string

Dump return config as json string.

func (*Container) WriteConfigAs

func (c *Container) WriteConfigAs(dest string) error

WriteConfigAs writes the current configuration to the given path.

type Observable

type Observable interface {
	Run(Containable, chan error)
}

type Observer

type Observer struct {
	// contains filtered or unexported fields
}

func (Observer) Run

func (o Observer) Run(c Containable, errs chan error)

Jump to

Keyboard shortcuts

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