dotenv

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package dotenv provides a line-preserving scanner and emitter for dotenv files. Unlike map-based parsers, it retains original bytes, ordering, comments, blank lines, and line terminators so that unmodified lines pass through byte-for-byte and only resolved values are rewritten.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Emit

func Emit(w io.Writer, lines []Line, style QuoteStyle) error

Emit writes lines to w. Non-KeyVal lines and unresolved KeyVal lines are written verbatim (Raw+Ending). Resolved KeyVal lines are rewritten as Key=quote(Value, style)+Ending. A resolved value containing a raw newline is rejected regardless of style.

Types

type Line

type Line struct {
	Raw      string // exact original bytes of the line, WITHOUT the line terminator
	Ending   string // the terminator that followed Raw: "\n", "\r\n", or "" (last line without a trailing newline)
	Kind     LineKind
	Key      string // set only for KindKeyVal
	Value    string // raw value substring for KindKeyVal (may contain '='), as it appeared in the source
	Resolved bool   // false from Scan; the render pipeline sets true after replacing Value with a resolved secret
}

Line is one physical line of a dotenv file.

func Scan

func Scan(r io.Reader) ([]Line, error)

Scan reads r fully and splits it into ordered, byte-faithful Lines. Each line's terminator is preserved in Ending ("\n", "\r\n", or "" for a final line lacking a newline). No trimming, quote-stripping, or resolution occurs.

type LineKind

type LineKind int

LineKind classifies a physical line of a dotenv file.

const (
	// KindBlank is an empty line or one containing only whitespace.
	KindBlank LineKind = iota
	// KindComment is a line whose first non-whitespace byte is '#'.
	KindComment
	// KindKeyVal is a well-formed KEY=VALUE line.
	KindKeyVal
	// KindOther is any non-blank, non-comment line without an '='.
	KindOther
)

type QuoteStyle added in v0.2.0

type QuoteStyle int

QuoteStyle selects how resolved values are quoted on emit.

const (
	// QuoteMinimal is the default, byte-faithful quoting: values are emitted
	// bare unless they contain a shell-significant byte, in which case they are
	// double-quoted with '\' and '"' escaped.
	QuoteMinimal QuoteStyle = iota
	// QuoteShell produces output safe for a POSIX shell that consumes the file
	// via `source`/`.`: values are single-quoted (everything inside single
	// quotes is literal) unless they are a "boring" token.
	QuoteShell
)

Jump to

Keyboard shortcuts

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