envfile

package
v1.28.1 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package envfile provides helpers for reading and updating .env files while preserving comments, blank lines, and line order.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyPhpConstUpdates added in v1.0.0

func ApplyPhpConstUpdates(path string, updates map[string]string) error

ApplyPhpConstUpdates rewrites define() values in a PHP file for the given keys. Existing define() calls are updated in-place. Keys that don't exist in the file are appended before "/* That's all" comment, or at the end of the file if not found.

func ApplyUpdates

func ApplyUpdates(path string, updates map[string]string) error

ApplyUpdates rewrites the .env at path, replacing values for any key in updates. Keys not already present are appended at the end in stable (sorted) order so idempotent calls produce identical bytes regardless of Go's map-range nondeterminism. Comments and blank lines are preserved. The write is skipped when the resulting contents match the existing file, so dev-side watchers (vite, IDE indexers, opcache) don't see mtime churn on idempotent calls. The file's existing mode is preserved.

Keys must not contain '=' or any newline character; values must not contain newline characters. These checks reject the env_overrides injection vector where a malicious .lerd.yaml value containing "\nADMIN_TOKEN=stolen" would otherwise split a single .env line into two and silently introduce an unrelated key.

func ExampleValues added in v1.28.1

func ExampleValues(exampleContent string) map[string]string

ExampleValues maps each key in an .env.example to its raw value, everything after the first "=", copied verbatim so placeholders and quoting survive. The first occurrence of a key wins, matching the first-wins placement MergeMissing uses when inserting a duplicated example key.

func ReadKey added in v0.6.0

func ReadKey(path, key string) string

ReadKey returns the value of a single key from the .env file at path, or an empty string if the key is absent or the file cannot be read.

func ReadKeys added in v1.5.0

func ReadKeys(path string) ([]string, error)

ReadKeys returns all non-comment key names from the .env file at path, in the order they appear.

func ReadPhpConst added in v1.0.0

func ReadPhpConst(path string) (map[string]string, error)

ReadPhpConst reads a WordPress-style wp-config.php file and returns a map of the defined PHP constants (define('KEY', 'value') calls). Only string and numeric constants are captured; boolean/null defines are ignored.

func ReadValues added in v1.26.0

func ReadValues(path string) map[string]string

ReadValues reads path once and returns all non-comment key/value pairs, with each value unquoted the same way ReadKey unquotes a single key. On a duplicate key the first occurrence wins, matching ReadKey, which returns its first match. Returns an empty (non-nil) map when the file is missing, so callers can range freely. Prefer this over repeated ReadKey calls when checking several keys from one file: ReadKey re-opens and rescans the whole file on every call.

func ReferencesContainer added in v1.26.0

func ReferencesContainer(content, serviceName string) bool

ReferencesContainer reports whether content references the lerd container hostname "lerd-<serviceName>" as a whole token, so bare "postgres" is not matched by a "lerd-postgres-18" reference (and vice versa).

func SyncPrimaryDomain added in v1.10.0

func SyncPrimaryDomain(projectPath, domain string, secured bool) error

SyncPrimaryDomain updates APP_URL and VITE_REVERB_HOST/SCHEME/PORT in the project's .env to reflect the current primary domain and TLS state. Only keys that already exist in the .env are touched. Silently does nothing if no .env exists.

func UpdateAppURL

func UpdateAppURL(projectPath, scheme, domain string) error

UpdateAppURL sets APP_URL in the project's .env to scheme://domain. Silently does nothing if no .env exists.

Types

type MergeResult added in v1.28.1

type MergeResult struct {
	Merged     string
	Added      []string
	AddedLines []int
}

MergeResult is the outcome of merging missing .env.example keys into an existing .env. Merged is the full proposed file content, ready to render in a diff or write to disk; Added lists the keys that were inserted, in the order they appear in .env.example; AddedLines gives the 1-based line numbers in Merged that are newly inserted (key lines and their carried comments), so a viewer can decorate exactly the added lines without re-diffing.

func MergeMissing added in v1.28.1

func MergeMissing(exampleContent, envContent string, include map[string]bool) MergeResult

MergeMissing computes a proposed .env that adds the keys present in exampleContent but missing from envContent, placing each one next to the neighbours it has in .env.example rather than appending them all at the end.

Placement is anchor-based: walking the example in order, the last example key that also exists in the .env is the current anchor, and a missing key is queued to be inserted right after that anchor's line. This keeps a missing DB_PORT inside the DB_* block instead of orphaning it at the bottom. Keys missing before any shared key land at the top, in example order.

When include is non-nil, only missing keys for which include[key] is true are added; a nil include adds every missing key. Values, quoting, and attached comments are copied verbatim from the example so placeholders survive. Keys in the .env that are absent from the example are never touched.

Jump to

Keyboard shortcuts

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