Documentation
¶
Overview ¶
Package config provides a simple, structured, and extensible way to manage application configuration in Go.
It builds upon the Viper library and adds powerful features like validation, default value registration, environment and flag integration, and structured config registration.
Key Features:
- Register typed configuration structs with default values.
- Parse YAML configuration files and bind fields to CLI flags and environment variables.
- Automatically generate flags based on struct field tags.
- Validate configuration using custom logic (via `Validate()` method).
- Write current configuration back to disk and trigger onChange handlers.
- Load configuration from disk on each Read() call.
- Automatically fallbacks to default config creation if no file is found.
All configuration structs must implement the `Config` interface:
type Config interface {
Validate() error
}
Example:
package config
import (
"fmt"
"github.com/valentin-kaiser/go-core/config"
"github.com/valentin-kaiser/go-core/flag"
)
type ServerConfig struct {
Host string `yaml:"host" usage:"The host of the server"`
Port int `yaml:"port" usage:"The port of the server"`
}
func (c *ServerConfig) Validate() error {
if c.Host == "" {
return fmt.Errorf("host cannot be empty")
}
if c.Port <= 0 {
return fmt.Errorf("port must be greater than 0")
}
return nil
}
func Get() *ServerConfig {
c, ok := config.Get().(*ServerConfig)
if !ok {
return &ServerConfig{}
}
return c
}
func init() {
cfg := &ServerConfig{
Host: "localhost",
Port: 8080,
}
// Register config - path parameter is ignored, flag.Path will be used
if err := config.Register("", "server", cfg); err != nil {
fmt.Println("Error registering config:", err)
return
}
// Parse flags (including --path and config-specific flags)
flag.Init()
// Read config using the parsed --path flag - loads from disk each time
if err := config.Read(); err != nil {
fmt.Println("Error reading config:", err)
return
}
// Register onChange handler to be called when Write() is used
config.OnChange(func(o, n config.Config) error {
fmt.Println("Configuration changed")
return nil
})
// Update configuration - this will trigger onChange handlers
if err := config.Write(cfg); err != nil {
fmt.Println("Error writing config:", err)
}
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Changed ¶
Changed checks if two configuration values are different by comparing their reflection values. It returns true if the configurations differ, false if they are the same. This function handles nil values correctly and performs deep comparison of the underlying values.
func Manager ¶ added in v1.5.3
func Manager() *manager
Manager returns the singleton configuration manager instance
func Read ¶
func Read() error
Read reads the configuration from the file, validates it and applies it This function always loads the configuration from disk If the file does not exist, it creates a new one with the default values The config path is resolved from flag.Path when this function is called
func Reset ¶
func Reset()
Reset clears the config package state Everything must be re-registered after calling this function