conf

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package conf provides cascading application configuration via environment variables and hardcoded defaults.

The recommended usage is to instantiate using conf.Basic(), passing default values:

config := conf.Basic(map[string]string{
	"LISTEN_PORT": "8080",
	"SQLITE_DB": "./devDatabase.db",
	"MYSQL_DB": "mysql://test:test@localhost/test",
})

port := fmt.Sprintf(":%d", int(config.Get("LISTEN_PORT")))
mysqlUrl := config.Get("MYSQL_DB")

In this case, the config object will return values set in environment variables, if available. Otherwise, it will output a one-time warning on stderr for each requested configuration setting, and return the hardcoded default value.

This is exactly equivalent to the following:

config := conf.Chain(conf.Env(), conf.Warn(conf.Map(defaultValues)))

If your project is a library and you'd like to use this, you should require a ConfChain to be passed, and add your default values via Use. If your project is not a library, feel free to skip instantiation and make use of Get, Use and UseMap, all of which use the default Conf.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(key string) string

Get returns the value from the default configuration object.

func Use

func Use(otherConfig Conf) error

Use adds another configuration source to the default configuration.

func UseMap

func UseMap(defaultValues map[string]string) error

UseMap adds a map of default values to the default configuration.

Types

type Conf

type Conf interface {
	Get(key string) string
}

Conf provides the configured value for the given key

func Env

func Env() Conf

Env creates a Conf that returns values from environment variables (os.Getenv)

type ConfChain

type ConfChain interface {
	Conf
	// Use appends
	Use(Conf) error
}

ConfChain is a more powerful Conf instance that allows adding more default values in an existing instance. This can be useful when the Conf object is passed around.

func Basic

func Basic(defaultValues map[string]string) ConfChain

Basic returns a Conf which attempts to return values set in environment variables. If no environment variable is set, the Conf will warn about this on stderr while returning the passed default value.

func Chain

func Chain(others ...Conf) ConfChain

Chain provides cascading configuration by taking the first non-empty value from a slice of other Conf instances.

func Map

func Map(m map[string]string) ConfChain

Map will return the settings from the given map of default values. It is primarly used for providing default values as the end of a Chain.

func Remap

func Remap(keySwap map[string]string, conf Conf) ConfChain

Remap returns a Conf that changes the requested key prior to requesting a value from an underlying Conf

func Warn

func Warn(conf Conf) ConfChain

Warn wraps another Conf object. For each setting, a warning is output on os.Stderr when it is requested the first time. This is useful to wrap, a Conf from Map when default values are to be discouraged, but tolerated.

Jump to

Keyboard shortcuts

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