config

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

config package provides a way to load configuration from a file and environment variables. To enable loading from environment variables, you need to set the build tag "viper_bind_struct" because of https://github.com/spf13/viper/pull/1429#issuecomment-1870976604

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load[T Settings](path string, fallbacks ...Fallback) (cfg T, err error)

Load loads the configuration from the provided path or fallback path. Returns an error if the configuration cannot be loaded or unmarshalled into the provided struct.

You can provide a slice of fallback functions that will be used to get the configuration path if an empty path is provided. The first fallback function that returns a path is used. If no fallback functions are provided, the default fallback is used (~/.config/<binary-name>/config.yaml).

All environment variables with the scheme "<binary-name>_<field-name>(_<recursive-field-name>)" will be considered.

The configuration is unmarshalled into the provided struct. Its IsEmpty method is called to check if the loaded configuration is empty. Most of the time, you want to implement it like this:

func (c Config) IsEmpty() bool {
	return c == (Config{})
}

func SetBinaryName

func SetBinaryName(name string)

SetBinaryName replaces the default binary name with the provided one. This function is not safe for concurrent use.

func SetFs

func SetFs(filesystem afero.Fs)

SetFs replaces the default filesystem with the provided one. This may only be used for testing purposes. This function is not safe for concurrent use.

Types

type Fallback

type Fallback func() (string, error)

Fallback is a function that returns the path to the configuration file if an empty path is provided

type Settings

type Settings interface {
	// IsEmpty returns true if the configuration is empty
	IsEmpty() bool
}

Settings is an interface that must be implemented by a configuration struct

Jump to

Keyboard shortcuts

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