config

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: Apache-2.0 Imports: 8 Imported by: 3

Documentation

Overview

Package config provides a single way to manage the configuration of your application. The configuration can be a yaml file and/or a list of environment variable. To set the config using the environment, this package is using the package github.com/nexucis/lamenv, which is able to determinate what is the environment variable that matched the different attribute tof the struct. By default it is based on the yaml tag provided.

The main entry point of this package is the struct Resolver. This struct will allow you to set the path to your config file if you have one and to give the prefix of all of your environment variable. Note:

  1. A good practice is to prefix your environment variable by the name of your application.
  2. The config file is not mandatory, you can manage all you configuration using the environment variable.
  3. The config by environment is always overriding the config by file.

The Resolver at the end returns an object that implements the interface Validator. Each config/struct can implement this interface in order to provide a single way to verify the configuration and to set the default value. The object returned by the Resolver will loop other different structs that are parts of the config and execute the method Verify if implemented.

Example:

  import (
          "fmt"

          "github.com/perses/common/config"
  )

   type Config struct {
	    Etcd *EtcdConfig `yaml:"etcd"`
   }

   func (c *Config) Verify() error {
     if c.EtcdConfig == nil {
       return fmt.Errorf("etcd config cannot be empty")
     }
   }

   func Resolve(configFile string) (Config, error) {
	    c := Config{}
	    return c, config.NewResolver().
		  SetConfigFile(configFile).
		  SetEnvPrefix("PERSES").
		  Resolve(&c).
		  Verify()
   }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Resolver

type Resolver[T any] interface {
	SetEnvPrefix(prefix string) Resolver[T]
	SetConfigFile(filename string) Resolver[T]
	SetConfigData(data []byte) Resolver[T]
	AddChangeCallback(func(*T)) Resolver[T]
	Resolve(config *T) Validator
}

Resolver is the interface to set the config file path and the environment variable prefix

func NewResolver

func NewResolver[T any]() Resolver[T]

NewResolver creates a new resolver

type Validator

type Validator interface {
	Verify() error
}

Validator is the interface that a config struct can implement in order to provide a way to verify the configuration

Jump to

Keyboard shortcuts

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