common

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SectionStartRe = regexp.MustCompile(`^# \[SECTION:\s*([^]]+)\](.*)$`)
	SectionEndRe   = regexp.MustCompile(`^# \[SECTION_END:\s*([^]]+)\](.*)$`)
)

Functions

func ApplyPatches

func ApplyPatches(path string, patches map[int64]Patch, autoNewLine bool, logger *slog.Logger) error

Orchestrator: reads spans, streams file, applies patches, writes temp, renames.

func ApplySinglePatch

func ApplySinglePatch(file io.ReadSeeker, w io.Writer, p Patch, span ByteSpan, curOffset *int64, logger *slog.Logger) error

ApplySinglePatch: writes insertion + either original line or skip.

func CopySpan

func CopySpan(file io.ReadSeeker, w io.Writer, start, end int64, curOffset *int64, logger *slog.Logger) error

CopySpan: copies file[start:end] verbatim into w.

func CreateBackup

func CreateBackup(logger *slog.Logger, path string) error

CreateBackup creates a timestamped backup of the file.

func HasSpaceChars

func HasSpaceChars(str string) bool

IsEmptyStr checks if a string contains any whitespace characters.

func IsEmptyStr

func IsEmptyStr(str string) bool

IsEmptyStr checks if a string contains only whitespace characters.

func MakeSectionEnd

func MakeSectionEnd(
	name string,
	comment string,
	ensureNewLine bool,
) string

MakeSectionEnd creates a section end marker with optional inline comment. Example: "# [SECTION_END: my_section] reason".

func MakeSectionStart

func MakeSectionStart(
	name string,
	comment string,
	ensureNewLine bool,
) string

MakeSectionStart creates a section start marker with optional inline comment. Example: "# [SECTION: my_section] some comment".

func ProcessPatches

func ProcessPatches(
	in io.ReadSeeker,
	size int64,
	w io.Writer,
	spans map[int64]ByteSpan,
	patches map[int64]Patch,
	logger *slog.Logger,
) error

func ReadLine

func ReadLine(r Reader) ([]byte, int64, error)

ReadLine: returns complete line (no trailing newline) and the total byte length *including* the newline if present. Uses Reader.ReadByte and Reader.Peek to detect CRLF.

func ReadLineWithEOL

func ReadLineWithEOL(reader Reader) ([]byte, error)

func ScanLineOffsets

func ScanLineOffsets(path string, patches map[int64]Patch, logger *slog.Logger) (map[int64]ByteSpan, error)

ScanLineOffsets is a wrapper for ScanLineOffsetsReader.

func ScanLineOffsetsReader

func ScanLineOffsetsReader(r Reader, patches map[int64]Patch, logger *slog.Logger) (map[int64]ByteSpan, error)

Pass 1: scan file, compute byte offsets for every line. Only store offsets for lines that appear in `patches`.

func SkipSpaces

func SkipSpaces(line string, pos int) int

SkipSpaces returns the first index after spaces (starts skipping from pos) Returns len(line) if all remaining characters are spaces.

func SkipSpacesBack

func SkipSpacesBack(line string, pos int) int

SkipSpacesBack returns the last index (moving left from pos) that is NOT a space Returns -1 if all characters from 0 to pos are spaces.

func ToUpperSnake added in v0.0.3

func ToUpperSnake(s string) string

func UntilSpace

func UntilSpace(line string, pos int) int

UntilSpace returns the index of the first space starting from pos Returns len(line) if no space is found.

func UntilSpaceBack

func UntilSpaceBack(line string, pos int) int

UntilSpaceBack returns the index of the first space encountered moving left from pos Returns -1 if no space is found.

Types

type ByteSpan

type ByteSpan Span[int64]

ByteSpan = [start,end] byte offsets.

type LineType

type LineType int
const (
	LineTypeRaw     LineType = iota
	LineTypeComment          // Only if line starts with #
	LineTypeVar
	LineTypeVal // If value wasn't terminated on the previous line
	LineTypeSectionStart
	LineTypeSectionEnd
)

type MatchedSectionData

type MatchedSectionData struct {
	Name    string
	Comment string
}

MatchedSectionData holds information about a section marker.

func MatchSectionEnd

func MatchSectionEnd(line string) (bool, MatchedSectionData)

MatchSectionEnd checks if a line is a section end marker Returns true and section data if matched, false otherwise.

func MatchSectionStart

func MatchSectionStart(line string) (bool, MatchedSectionData)

MatchSectionStart checks if a line is a section start marker Returns true and section data if matched, false otherwise.

type ParsedLine

type ParsedLine struct {
	Type    LineType
	RawLine string

	Variable        *VariableData
	VariableValPart *VariableValPartData
	SectionData     *SectionData // if line is within a section

	SectionStartEndInlineComment string

	// if value wasn't terminated > 0. 0 if nothing to terminate or terminated the same line
	UnterminatedValueLines int
}

ParsedLine represents a parsed line from .env file.

type Parser

type Parser interface {
	ParseLine(line string) (ParsedLine, error)
}

type ParserStream

type ParserStream interface {
	GetLineIdx() int64
	Next() (ParsedLine, error) // parses until io.EOF
}

type Patch

type Patch struct {
	LineIdx      int64 // target line
	ShouldInsert bool
	Insert       string // insert before the target line

	ShouldInsertAfter bool // same as should insert, but next line
	InsertAfter       string

	RemoveLine bool // removes the target line
}

Patch represents changes that u need to put into the file Warning: Insert And InsertAfter don't automatically add new lines.

type Reader

type Reader interface {
	io.Reader
	ReadBytes(delim byte) ([]byte, error)
	ReadByte() (byte, error)
	ReadLine() (line []byte, isPrefix bool, err error)
	Peek(n int) ([]byte, error)
}

type SectionData

type SectionData struct {
	Variables map[string]struct{} // key value quick lookup for sections
	Name      string
}

type Span

type Span[T comparable] struct {
	Start T
	End   T
}

type VariableData

type VariableData struct {
	Key          string
	Value        string
	Prefix       string // Everything before the value (export, whitespace, key, =, etc)
	Suffix       string // Everything after the value (whitespace, comments)
	IsTerminated bool
	IsQuoted     bool
	Quote        byte
}

VariableData contains parsed variable information.

type VariableValPartData

type VariableValPartData struct {
	Value        string // What value did variable have
	Suffix       string // Everything after the value (whitespace, comments)
	IsTerminated bool   // If variable was terminated on that line
	Quote        byte
}

Jump to

Keyboard shortcuts

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