config

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2025 License: BSD-3-Clause Imports: 16 Imported by: 0

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, dynamic watching, 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).
  • Watch configuration files for changes and hot-reload updated values.
  • Write current configuration back to disk.
  • 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/fsnotify/fsnotify"
)

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,
    }

    if err := config.Register("server", cfg); err != nil {
        fmt.Println("Error registering config:", err)
        return
    }

    if err := config.Read(); err != nil {
        fmt.Println("Error reading config:", err)
        return
    }

    config.Watch(func(e fsnotify.Event) {
        if err := config.Read(); err != nil {
            fmt.Println("Error reloading config:", err)
        }
    })

    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

func Changed(o, n any) bool

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 OnChange

func OnChange(f func(o Config, n Config) error)

OnChange registers a function that is called when the configuration changes

func Read

func Read() error

Read reads the configuration from the file, validates it and applies it If the file does not exist, it creates a new one with the default values

func Register

func Register(path, name string, c Config) error

Register registers a configuration struct and parses its tags The name is used as the name of the configuration file and the prefix for the environment variables

func Reset

func Reset()

Reset clears the config package state Everything must be re-registered after calling this function

func Watch

func Watch()

Watch watches the configuration file for changes and calls the provided function when it changes It ignores changes that happen within 1 second of each other This is to prevent multiple calls when the file is saved

func Write

func Write(change Config) error

Write writes the configuration to the file, validates it and applies it If the file does not exist, it creates a new one with the default values

Types

type Config

type Config interface {
	Validate() error
}

Config is the interface that all configuration structs must implement It should contain a Validate method that checks the configuration for errors

func Get

func Get() Config

Get returns the current configuration

Jump to

Keyboard shortcuts

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