Documentation
¶
Overview ¶
Package blocktext implements the marker-delimited "managed block" primitive. It is used by every Cartographer feature that must own a slice of a hand-curated user file (comments, ordering, everything else preserved) without parsing the file's own format: the Codex MCP server entry and per-hook registrations in config.toml (D58, TOML `#` comment markers). Callers pass their own marker strings — this package only ever does substring search/replace on raw text, never anything format-aware. (The similar, independently-implemented instructions block in internal/provisioning, D56, predates this package and uses HTML comment markers; it was left as-is — see D58 for why.)
Index ¶
- Variables
- func BeginStable(begin string) string
- func CheckFile(path, begin, end string) error
- func Remove(baseDir, path, begin, end string, dryRun bool) (bool, error)
- func ReplaceBetween(content, begin, end, replacement string) (string, bool)
- func Upsert(content, begin, end, block string) (string, error)
- func Write(baseDir, path, begin, end, body string) error
- type Outcome
Constants ¶
This section is empty.
Variables ¶
var ErrCorruptedBlock = errors.New("marker block is corrupted (not exactly one begin followed by one end)")
ErrCorruptedBlock is returned (wrapped, naming the file) by Write and Remove when the file does not hold exactly one begin line followed by one end line: a begin whose end was lost, a second begin before the end, an end before the begin. The file is left untouched: cutting from the first begin to the next end would delete the user's text in between, and appending a second block would duplicate the entries. `cartographer doctor` reports the malformed block.
Functions ¶
func BeginStable ¶ added in v0.28.0
BeginStable is the part of a begin marker that identifies it: everything up to the first " — " (em dash). The tail is display text, older versions wrote it in a different language.
func CheckFile ¶ added in v0.28.0
CheckFile reports ErrCorruptedBlock (wrapped) when the file at path holds a corrupted block, and nil when it is absent, unreadable, or sound. For a caller that must not edit the file before Write would refuse it anyway.
func Remove ¶
Remove strips the block delimited by begin/end (markers included) from the file at path, if present. If that leaves the file empty or whitespace-only, the file itself is removed (covers a file dedicated to a single block). Returns whether a block was found (and, unless dryRun, removed); (false, nil) if the file doesn't exist or doesn't contain the markers. If dryRun is true, computes and returns what would happen without writing anything.
baseDir and the refusal are those of Write, but the refusal applies only when a block was found: a file with nothing of ours in it is a no-op and never an error. A dry run refuses too when it would have changed something, so it reports what the real run would do.
func ReplaceBetween ¶
ReplaceBetween is the legacy locator with no integrity check (first begin, first end after it, anywhere in a line); new writers use ReplaceBlock. It returns content with the text between begin and end (markers included, plus one trailing newline if present right after end) replaced by replacement, and true — or content unchanged and false if begin/end don't both appear, in order.
The begin marker is recognized by its stable prefix: everything up to the first " — " (em dash). The human-readable tail after the dash is display text, not marker identity — older versions wrote it in a different language, and matching the full current string would miss their blocks and duplicate the block on the next write.
func Upsert ¶ added in v0.28.0
Upsert puts block (the whole block, markers and a final newline included) into content: it replaces the one begin..end block when there is one, appends it when there is none, and returns ErrCorruptedBlock (content untouched) for any other shape. Unlike the file-based Write it adds no blank separator line: a missing trailing newline is added once and the block follows directly, so ReplaceBlock(..., "") restores the original text byte for byte when it ended with a newline (every crontab does). A text that did not gets that one newline: removal cannot know whether the newline was the user's or ours, and guessing would eat a user's own blank-line ending.
func Write ¶
Write creates or rewrites the block delimited by begin/end (each marker on its own line) in the file at path, with body as its content:
- file absent → created with just the block (parent dirs made as needed);
- file present with both markers → only the text between them (markers included) is replaced; every other byte in the file is left untouched;
- file present without the markers (pre-existing, unrelated content) → the block is appended at the end, separated from existing content by a blank line.
Idempotent: calling Write twice in a row with the same body produces the same file.
baseDir is the client directory path lives under (home, or the project dir in workspace scope). Write refuses — with an error matching safewrite.ErrSymlinkDestination, before reading or writing anything — a path whose file, or any directory component below baseDir, is a symlink or otherwise not a plain file or directory. baseDir itself may be a link.
Types ¶
type Outcome ¶ added in v0.28.0
type Outcome int
Outcome says what ReplaceBlock found.
func ReplaceBlock ¶ added in v0.28.0
ReplaceBlock is the integrity-checked form of ReplaceBetween (#1502). Markers are whole lines: a begin line starts (at column 0) with the stable prefix of begin, an end line is end, both ignoring trailing blanks and "\r". A marker quoted in the middle of a line is text. Exactly one begin followed by exactly one end is Found and the text from the begin line through the end line (plus its terminator) is replaced; no marker line at all is Absent; every other shape is Corrupted and content comes back unchanged.