env

package
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

Env Source

The env source reads config from environment variables

Format

We expect environment variables to be in the standard format of FOO=bar

Keys are converted to lowercase and split on underscore.

Example
DATABASE_ADDRESS=127.0.0.1
DATABASE_PORT=3306

Becomes

{
    "database": {
        "address": "127.0.0.1",
        "port": 3306
    }
}

Prefixes

Environment variables can be namespaced so we only have access to a subset. Two options are available:

WithPrefix(p ...string)
WithStrippedPrefix(p ...string)

The former will preserve the prefix and make it a top level key in the config. The latter eliminates the prefix, reducing the nesting by one.

Example:

Given ENVs of:

APP_DATABASE_ADDRESS=127.0.0.1
APP_DATABASE_PORT=3306
VAULT_ADDR=vault:1337

and a source initialized as follows:

src := env.NewSource(
    env.WithPrefix("VAULT"),
    env.WithStrippedPrefix("APP"),
)

The resulting config will be:

{
    "database": {
        "address": "127.0.0.1",
        "port": 3306
    },
    "vault": {
        "addr": "vault:1337"
    }
}

New Source

Specify source with data

src := env.NewSource(
	// optionally specify prefix
	env.WithPrefix("GO_ADMIN"),
)

Load Source

Load the source into config

// Create new config
conf := config.NewConfig()

// Load env source
conf.Load(src)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultPrefixes = []string{}
)

Functions

func NewSource

func NewSource(opts ...source.Option) source.Source

NewSource returns a config source for parsing ENV variables. Underscores are delimiters for nesting, and all keys are lowercased.

Example:

"DATABASE_SERVER_HOST=localhost" will convert to

{
    "database": {
        "server": {
            "host": "localhost"
        }
    }
}

func WithPrefix

func WithPrefix(p ...string) source.Option

WithPrefix sets the environment variable prefixes to scope to. These prefixes will not be removed. Each prefix will be considered a top level config entry.

func WithStrippedPrefix

func WithStrippedPrefix(p ...string) source.Option

WithStrippedPrefix sets the environment variable prefixes to scope to. These prefixes will be removed from the actual config entries.

Types

This section is empty.

Jump to

Keyboard shortcuts

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