envfile

package
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package envfile reads and writes the .env format.

There is no specification for it. Every tool that reads one agrees on `KEY=value` and disagrees about the rest, so the subset implemented here is the intersection of what the common readers accept, and each departure from the loosest possible reading is a deliberate choice to fail rather than to guess:

  • Blank lines and lines whose first non-space character is # are skipped.
  • A leading `export ` is allowed and ignored, so a file written to be sourced by a shell can be read here unchanged.
  • A bare value ends at the end of the line. An unquoted # does NOT begin a comment, because a password of pa55word#1 is far more common than an inline comment, and dropping half a credential is a failure nobody sees until something cannot authenticate.
  • A single-quoted value is literal. A double-quoted value understands \n, \r, \t, \\ and \", which is what makes a certificate fit on one line. Either kind may span several lines, because a certificate that does not fit on one is the other way people write it.
  • A key that appears twice is an error rather than a last-one-wins, since the two lines disagree and only the author knows which was meant.
  • Text after a closing quote is an error, except a comment. The value has already ended, so keeping the part before the quote would be a guess about which half of the line was meant.
  • A byte order mark on the first line is dropped. Several editors write one and none of them mention it, and a key that begins with an invisible character matches nothing anywhere.
  • Carriage returns are dropped with the line ending, so a file written on Windows reads the same as one written anywhere else.

Nothing here talks to the network or the filesystem. It turns text into variables and variables into text, which is what makes both directions testable and what keeps the format in one place instead of in two commands.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(w io.Writer, vars []Var, header string) error

Format writes variables as a file, sorted by key.

Sorted rather than in the order the server returned them: a file that is written again after a change should differ only where the values differ, so that a diff is about the change and not about the order.

func Quote

func Quote(value string) string

Quote renders one value so that reading it back returns what went in.

Quotes are added only where they are needed. A file full of unnecessary quotes is harder to read and harder to diff, and most values are a word.

Types

type ParseError

type ParseError struct {
	Line   int
	Reason string
	Text   string
}

ParseError is a line that could not be read, named by line number.

func (*ParseError) Error

func (e *ParseError) Error() string

type Var

type Var struct {
	Key   string
	Value string

	// Line is where it was found, for an error that can be acted on.
	Line int
}

Var is one variable in a file.

func Parse

func Parse(r io.Reader) ([]Var, error)

Parse reads a file into variables, in the order they appear.

Order is preserved rather than sorted: a file is something a person wrote, and reporting the third variable as the third one is the difference between an error they can find and an error they have to hunt for.

Jump to

Keyboard shortcuts

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