configdotenv

package module
v0.2.12 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 4 Imported by: 0

README

config-dotenv

Read dotenv (.env) configuration through config — with no added dependency

Go Reference Pipeline Coverage phpboyscout Go toolkit

Part of the phpboyscout Go toolkit — small, framework-free Go modules extracted from go-tool-base. Documented with the parent module at config.go.phpboyscout.uk


config has one codec of its own, YAML, which also reads JSON because YAML 1.2 is a superset of it. Every other file format is a sibling module like this one — and this one adds nothing to your dependency graph, because a .env file is simple enough to read with the standard library.

import (
	"gitlab.com/phpboyscout/go/config"
	configdotenv "gitlab.com/phpboyscout/go/config-dotenv"
)

store, err := config.NewStore(ctx,
	config.WithFiles(fsys, "/etc/app.yaml"),                      // YAML, the default
	config.WithBackend(configdotenv.New(fsys, "/etc/app.env")),   // outranks it
)

Keys nest on underscores

A dotenv key is lower-cased and its underscores become dotted-key separators:

DATABASE_HOST=localhost
DATABASE_PORT=5432

reads as database.host and database.port, so a dotenv layer merges into a nested YAML or JSON one by the same paths. This matches how the environment backend maps an unmatched variable, and it is deterministic — a key with a literal underscore in its name cannot be expressed, by design. An export prefix is accepted, and surrounding single or double quotes are stripped.

No interpolation

A ${VAR} in a value is left as written, never expanded. Expanding it would read the process environment through a side door the Store does not own — exactly the unprefixed-environment leak the environment backend's required prefix exists to prevent. If you want environment values, add config.WithEnv("PREFIX") as an ordinary layer, where the prefix applies and provenance names the variable.

Read-only

Values are strings; the read path casts. The format is read-only.

What it costs

Only the config graph — a .env file is parsed with the standard library, so there is no parser module. An allowlist test states it. No filesystem library: you supply the config.FS.

License

See LICENSE.

Documentation

Overview

Package configdotenv reads dotenv (.env) configuration through the config codec seam.

It is a sibling of the config core, not part of it, and adds no dependency at all — a dotenv file is simple enough to read with the standard library.

config.NewStore(ctx,
	config.WithFiles(fsys, "/etc/app.yaml"),        // YAML, the default
	config.WithBackend(configdotenv.New(fsys, "/etc/app.env")),
)

Keys nest on underscores

A dotenv key is lower-cased and its underscores become dotted-key separators, so `DATABASE_HOST=localhost` reads as `database.host`, and a dotenv layer merges into a nested YAML or JSON one by the same paths. This matches how the environment backend maps an unmatched variable, and it is deterministic — a key with a literal underscore in its name cannot be expressed, by design.

No interpolation

A `${VAR}` in a value is left as written, never expanded. Expanding it would read the process environment through a side door the Store does not own — exactly the unprefixed-environment leak the environment backend's required prefix exists to prevent. A consumer who wants environment values adds config.WithEnv as an ordinary layer, where the prefix applies and provenance names the variable.

Read-only

Values are strings; the read path casts. The format is read-only.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(filesystem config.FS, path string) config.Backend

New reads a dotenv document.

Types

type Codec added in v0.2.0

type Codec struct{}

Codec decodes KEY=value lines into layer values. New uses it, and it is exported so a backend adapter can reuse it as a value decoder — a key whose value is a dotenv document, decoded via that adapter's WithValueCodec option. It is a config.Codec.

func (Codec) Decode added in v0.2.0

func (Codec) Decode(path string, src []byte) ([]map[string]any, error)

Jump to

Keyboard shortcuts

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