klenv

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: May 13, 2020 License: MIT Imports: 6 Imported by: 0

README

Env Loader

Env loader loads environment variables in a konfig.Store

Usage

Basic usage loading all environment variables

envLoader := klenv.New(&klenv.Config{})

Loading specific variables

envLoader := klenv.New(&klenv.Config{
	Vars: []string{
		"DEBUG",
		"PORT",
	},
})

Loading specific variables if key matches regexp

envLoader := klenv.New(&klenv.Config{
	Regexp: "^APP_.*"
})

With a replacer and a Prefix for keys

envLoader := klenv.New(&klenv.Config{
	Prefix: "config.",
	Replacer: nstrings.ReplacerToLower,
})

Loading value as string slice

os.Setenv("APP_VAR1", "value1,value2") // will be loaded as string slice
os.Setenv("APP_VAR2", "value") // will be loaded as string

envLoader := klenv.New(&klenv.Config{
	Regexp: "^APP_.*",
	SliceSeparator: ",",
})

...

fmt.Printf("%+v\n", store.Get("VAR1")) // Output: []string{"value1","value2}
fmt.Printf("%+v\n", store.Get("VAR2")) // Output: value

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Name is the name of the loader
	Name string
	// StopOnFailure tells whether a failure to load configs should closed the config and all registered closers
	StopOnFailure bool
	// Regexp will load the environment variable if it matches the given regexp
	Regexp string
	// Vars will load vars only present in the vars slice
	Vars []string
	// Prefix will add a prefix to the environment variables when adding them in the config store
	Prefix string
	// Replacer is used to replace chars in env vars keys
	Replacer nstrings.Replacer
	// MaxRetry is the maximum number of time the load method can be retried when it fails
	MaxRetry int
	// RetryDelay is the time betweel each retry
	RetryDelay time.Duration
	// SliceSeparator contains separator for values like `item1,item2,item3`.
	// Such values will be loaded as string slice if separator is not empty.
	SliceSeparator string
}

Config is the config a an EnvLoader

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

Loader is the structure representing the environment loader

func New

func New(cfg *Config) *Loader

New return a new environment loader with the given config

func (*Loader) Load

func (l *Loader) Load(s konfig.Values) error

Load implements konfig.Loader, it loads environment variables into the konfig.Store based on config passed to the loader

func (*Loader) MaxRetry

func (l *Loader) MaxRetry() int

MaxRetry returns the maximum number to retry a load when an error occurs

func (*Loader) Name

func (l *Loader) Name() string

Name returns the name of the loader

func (*Loader) RetryDelay

func (l *Loader) RetryDelay() time.Duration

RetryDelay returns the delay between each load retry

func (*Loader) StopOnFailure added in v0.2.0

func (l *Loader) StopOnFailure() bool

StopOnFailure returns whether a load failure should stop the config and the registered closers

Jump to

Keyboard shortcuts

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