envfile

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxValueLength is the maximum allowed value length (64KB).
	MaxValueLength = 64 * 1024
	// MaxFileSize is the maximum allowed .env file size (1MB).
	MaxFileSize = 1024 * 1024
)

Variables

This section is empty.

Functions

func Write

func Write(env *EnvFile) string

Write serializes an EnvFile back to a string, preserving original formatting.

Types

type Conflict

type Conflict struct {
	Key        string
	BaseValue  string // value in common ancestor
	OurValue   string // local value
	TheirValue string // remote value
}

Conflict represents a variable modified by both local and remote.

type DiffEntry

type DiffEntry struct {
	Key   string
	Value string
}

DiffEntry represents a single added or removed variable.

type DiffModified

type DiffModified struct {
	Key      string
	OldValue string
	NewValue string
}

DiffModified represents a changed variable with old and new values.

type DiffResult

type DiffResult struct {
	Added    []DiffEntry
	Removed  []DiffEntry
	Modified []DiffModified
	// Unchanged keys (not included as entries to save memory).
	UnchangedCount int
}

DiffResult represents the diff between two .env files.

func Diff

func Diff(local, remote *EnvFile) *DiffResult

Diff computes the difference between a local and remote EnvFile. local is the current state; remote is the incoming state.

func (*DiffResult) HasChanges

func (d *DiffResult) HasChanges() bool

HasChanges returns true if there are any differences.

func (*DiffResult) Summary

func (d *DiffResult) Summary() string

Summary returns a human-readable summary of changes.

type Entry

type Entry struct {
	// Key is the variable name (empty for comments/blank lines).
	Key string

	// Value is the parsed value.
	Value string

	// RawLine is the original line for round-trip preservation.
	RawLine string

	// Type classifies this entry.
	Type EntryType

	// Quote style used (for round-trip).
	Quote QuoteStyle
}

Entry represents a single line/entry in a .env file.

type EntryType

type EntryType int

EntryType classifies an entry in the .env file.

const (
	EntryKeyValue EntryType = iota
	EntryComment
	EntryBlank
)

type EnvFile

type EnvFile struct {
	Entries []Entry
}

EnvFile represents a parsed .env file preserving structure.

func Parse

func Parse(content string) (*EnvFile, error)

Parse parses a .env file from a string, preserving comments, ordering, and blank lines.

func (*EnvFile) Delete

func (e *EnvFile) Delete(key string) bool

Delete removes a key from the file.

func (*EnvFile) Get

func (e *EnvFile) Get(key string) (string, bool)

Get returns the value for a key, or empty string if not found.

func (*EnvFile) Interpolate

func (e *EnvFile) Interpolate()

Interpolate replaces ${VAR} and $VAR references in double-quoted values with the values of other variables defined in the same file. Single-quoted values are never interpolated (they are literal). This is optional and must be called explicitly after Parse().

func (*EnvFile) Keys

func (e *EnvFile) Keys() []string

Keys returns all variable keys in order.

func (*EnvFile) Set

func (e *EnvFile) Set(key, value string)

Set sets a key to a value. If the key exists, it updates it; otherwise appends.

func (*EnvFile) ToMap

func (e *EnvFile) ToMap() map[string]string

ToMap returns all key-value pairs as a map.

func (*EnvFile) VariableCount

func (e *EnvFile) VariableCount() int

VariableCount returns the number of key-value entries.

type MergeResult

type MergeResult struct {
	// Merged is the resulting env file after auto-merge.
	Merged *EnvFile

	// Conflicts are variables that were modified on both sides.
	Conflicts []Conflict

	// AutoMerged is the count of variables automatically merged.
	AutoMerged int
}

MergeResult is the result of a three-way merge.

func ThreeWayMerge

func ThreeWayMerge(base, ours, theirs *EnvFile) *MergeResult

ThreeWayMerge performs a three-way merge between base, ours, and theirs. base = last synced version, ours = local file, theirs = incoming from peer.

func (*MergeResult) HasConflicts

func (r *MergeResult) HasConflicts() bool

HasConflicts returns true if there are unresolved conflicts.

type QuoteStyle

type QuoteStyle int

QuoteStyle indicates how the value was quoted.

const (
	QuoteNone   QuoteStyle = iota
	QuoteSingle            // 'value'
	QuoteDouble            // "value"
)

type ValidationResult

type ValidationResult struct {
	Errors   []string
	Warnings []string
}

ValidationResult holds all validation findings.

func Validate

func Validate(ef *EnvFile) *ValidationResult

Validate checks an EnvFile for common issues.

func ValidateRaw

func ValidateRaw(content []byte) *ValidationResult

ValidateRaw validates raw .env file content before parsing.

func (*ValidationResult) HasWarnings

func (v *ValidationResult) HasWarnings() bool

HasWarnings returns true if there are warnings.

func (*ValidationResult) IsValid

func (v *ValidationResult) IsValid() bool

IsValid returns true if there are no errors.

Jump to

Keyboard shortcuts

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