dotenv

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package dotenv parses a standard `.env` file into ordered key/value entries.

This is the STANDARD dotenv grammar an operator's existing `.env` uses — the onboarding source the source-of-truth ADR fixes as the scaffold path and the import path's dotenv leg. It is deliberately NOT the inverse of compose.EncodeRaw: that encoder emits Compose's `format: raw` (no quoting, no escaping), whereas a hand-written `.env` uses quotes and backslash escapes, so the two are different grammars and live in different packages.

The parser is pure and fail-loud: a malformed line, a duplicate key, or a name outside the canonical grammar is refused BY NAME rather than parsed into something the operator did not write. `#` begins a comment only at the start of a line (after optional leading whitespace); a `#` after an unquoted value is part of the value, never a silently stripped inline comment — silently truncating a value that legitimately contains ` #` is exactly the transformation a secrets tool must not perform. Quote the value to carry a leading `#` or surrounding whitespace.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Key   string
	Value string
}

Entry is one parsed `KEY=value` pair, in file order.

func Parse

func Parse(data []byte) ([]Entry, error)

Parse reads a dotenv document into ordered entries. It refuses — naming the offending line and key — on a malformed line, an invalid key name, a bad quote/escape, or a duplicate key.

type Refusal

type Refusal struct {
	Key    string
	Reason string
}

Refusal names one entry Encode could not represent.

func Encode

func Encode(entries []Entry) ([]byte, []Refusal, error)

Encode renders entries as a dotenv document that Parse reads back byte-exact. Values that the unquoted grammar would alter - surrounding whitespace (Parse trims it), a leading quote, a `#`, a backslash, a control character or a newline - are double-quoted with the escapes parseDoubleQuoted understands; every other value is written bare. This is deliberately NOT the Compose renderer's raw encoding: that one is consumed by Compose's `format: raw`, which performs no unquoting, whereas a document written for humans and for `values import --from-dotenv` must survive the round trip. A NUL byte cannot be carried by either grammar and is refused by name.

Jump to

Keyboard shortcuts

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