Documentation
¶
Index ¶
- Variables
- func ApplyPatches(path string, patches map[int64]Patch, autoNewLine bool, logger *slog.Logger) error
- func ApplySinglePatch(file io.ReadSeeker, w io.Writer, p Patch, span ByteSpan, curOffset *int64, ...) error
- func CopySpan(file io.ReadSeeker, w io.Writer, start, end int64, curOffset *int64, ...) error
- func CreateBackup(logger *slog.Logger, path string) error
- func HasSpaceChars(str string) bool
- func IsEmptyStr(str string) bool
- func MakeSectionEnd(name string, comment string, ensureNewLine bool) string
- func MakeSectionStart(name string, comment string, ensureNewLine bool) string
- func ProcessPatches(in io.ReadSeeker, size int64, w io.Writer, spans map[int64]ByteSpan, ...) error
- func ReadLine(r Reader) ([]byte, int64, error)
- func ReadLineWithEOL(reader Reader) ([]byte, error)
- func ScanLineOffsets(path string, patches map[int64]Patch, logger *slog.Logger) (map[int64]ByteSpan, error)
- func ScanLineOffsetsReader(r Reader, patches map[int64]Patch, logger *slog.Logger) (map[int64]ByteSpan, error)
- func SkipSpaces(line string, pos int) int
- func SkipSpacesBack(line string, pos int) int
- func ToUpperSnake(s string) string
- func UntilSpace(line string, pos int) int
- func UntilSpaceBack(line string, pos int) int
- type ByteSpan
- type LineType
- type MatchedSectionData
- type ParsedLine
- type Parser
- type ParserStream
- type Patch
- type Reader
- type SectionData
- type Span
- type VariableData
- type VariableValPartData
Constants ¶
This section is empty.
Variables ¶
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 ¶
CreateBackup creates a timestamped backup of the file.
func HasSpaceChars ¶
IsEmptyStr checks if a string contains any whitespace characters.
func IsEmptyStr ¶
IsEmptyStr checks if a string contains only whitespace characters.
func MakeSectionEnd ¶
MakeSectionEnd creates a section end marker with optional inline comment. Example: "# [SECTION_END: my_section] reason".
func MakeSectionStart ¶
MakeSectionStart creates a section start marker with optional inline comment. Example: "# [SECTION: my_section] some comment".
func ProcessPatches ¶
func ReadLine ¶
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 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 ¶
SkipSpaces returns the first index after spaces (starts skipping from pos) Returns len(line) if all remaining characters are spaces.
func SkipSpacesBack ¶
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 UntilSpace ¶
UntilSpace returns the index of the first space starting from pos Returns len(line) if no space is found.
func UntilSpaceBack ¶
UntilSpaceBack returns the index of the first space encountered moving left from pos Returns -1 if no space is found.
Types ¶
type MatchedSectionData ¶
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 SectionData ¶
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.