Documentation
¶
Overview ¶
Package edit provides format-preserving editors for the config files that coding agents keep: JSON/JSONC, TOML and YAML. Only the requested key changes; comments, ordering and indentation are left as they are.
Index ¶
- func Atomically(fn func() error, paths ...string) error
- func DelEnvFile(path string, keys ...string) error
- func DelJSON(path string, keyPaths ...string) error
- func DelTOMLKey(path, name, key string) error
- func DelTOMLTable(path, name string) error
- func DelTOMLTop(path string, keys ...string) error
- func DelYAML(path string, keyPaths ...string) error
- func DelYAMLTop(path string, keys ...string) error
- func EditYAMLStrings(path string, keyPaths []string, fn func(string) string) error
- func GetEnvFile(path, key string) (string, bool)
- func GetJSON(path, keyPath string) (string, bool)
- func GetTOMLTable(path, name string) (map[string]string, error)
- func GetTOMLTop(path, key string) (string, bool)
- func GetYAML(path, keyPath string) (string, bool)
- func GetYAMLList(path, keyPath string) []string
- func GetYAMLMap(path, keyPath string) map[string]string
- func GetYAMLText(path, keyPath string) (string, bool)
- func GetYAMLTop(path, key string) (string, bool)
- func IsLink(path string) bool
- func JSONToYAML(src, dst string) error
- func Read(path string) ([]byte, error)
- func Remove(path string) error
- func SetEnvFile(path string, kvs ...KV) error
- func SetJSON(path string, kvs ...KV) error
- func SetTOMLKey(path, name, key string, value any) error
- func SetTOMLTable(path, name string, kvs ...KV) error
- func SetTOMLTables(path string, prefixes []string, tables []Table) error
- func SetTOMLTop(path string, kvs ...KV) error
- func SetYAML(path string, kvs ...KV) error
- func SetYAMLTop(path string, kvs ...KV) error
- func TOMLTables(path string) ([]string, error)
- func Target(path string) (string, error)
- func WriteAtomic(path string, data []byte) error
- type KV
- type Raw
- type Table
- type YAMLText
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Atomically ¶ added in v0.1.437
Atomically runs fn, which may write the files at paths in several steps, and puts each of them back as it was — its bytes and mode, or its absence — when fn fails, so an edit that fails part way leaves no file half made. A file that can't be read beforehand is left to fn as it is.
func DelEnvFile ¶
DelEnvFile removes keys from a dotenv file.
func DelJSON ¶
DelJSON removes key paths from a JSON/JSONC file, preserving everything else. Missing keys are ignored; an object left empty collapses to {}.
func DelTOMLKey ¶ added in v0.1.49
DelTOMLKey removes one key of table `name`, and the table with it when nothing else is left in it.
func DelTOMLTable ¶
DelTOMLTable removes table `name` (header and body) if present. Child table and array-table blocks are outside that range and remain.
func DelTOMLTop ¶
DelTOMLTop removes top-level (pre-table) keys, each with every line of its value.
func DelYAML ¶ added in v0.1.32
DelYAML removes key paths from a YAML file. A missing file stays missing.
func DelYAMLTop ¶
DelYAMLTop removes top-level scalar keys from a YAML file.
func EditYAMLStrings ¶ added in v0.1.514
EditYAMLStrings calls fn on every string under the key paths — mapping keys and values, sequence items, at any depth — and puts fn's answer in its place, comments and quoting kept. The file is written only when an answer differs, so an fn that answers what it is given only reads.
func GetEnvFile ¶
GetEnvFile reads one key from a dotenv file.
func GetTOMLTable ¶
GetTOMLTable returns the scalar keys of one table, or (nil, nil) when the file or table is absent. Read and parse errors are returned with the path. Strings are decoded; other scalars (booleans, numbers, dates, and times) retain their literal text. Arrays and inline tables are omitted. An array table with the requested name is a type error, not an absent table.
func GetTOMLTop ¶
GetTOMLTop reads a top-level (pre-table) key from a TOML file. A string is decoded; any other value is its text as written, without a trailing comment, an array or inline table over several lines included. A file TOML's parser refuses is read line by line, as well as that goes.
func GetYAML ¶ added in v0.1.32
GetYAML reads a dot-separated key path to a scalar from a YAML file.
func GetYAMLList ¶ added in v0.1.525
GetYAMLList reads the scalar items of the list at a key path.
func GetYAMLMap ¶ added in v0.1.32
GetYAMLMap reads the scalar entries of the mapping at a key path.
func GetYAMLText ¶ added in v0.1.525
GetYAMLText reads the value at a key path as the YAML it is written in — a list flow ("[a, b]") or block as it is — for SetYAML to put back as a YAMLText.
func GetYAMLTop ¶
GetYAMLTop reads a top-level scalar key from a YAML file.
func JSONToYAML ¶ added in v0.1.32
JSONToYAML writes a JSON or JSONC file out as block-style YAML, key order kept, for agents that moved from one to the other.
func Remove ¶ added in v0.1.503
Remove takes away a file magpie has emptied. A symlink stays and the file it points at is emptied instead, when there is one: deleting the link would leave the old text in its target. A file with other hard links is emptied and kept for the same reason, so every name comes out empty: with AGENTS.md and CLAUDE.md one file under two names, clearing AGENTS.md empties CLAUDE.md too, where deleting the one name would leave the text magpie is taking back under the other.
func SetEnvFile ¶
SetEnvFile sets keys in a dotenv file, replacing lines in place and appending new ones. Other lines are kept byte for byte.
func SetJSON ¶
SetJSON sets one or more key paths in a JSON/JSONC file, preserving comments, key order and indentation. Missing files and parents are created.
func SetTOMLKey ¶ added in v0.1.49
SetTOMLKey sets one key of table `name` and leaves the table's other lines as they are. Replacing a value preserves its surrounding whitespace and trailing comment. New keys go after the last complete value; missing tables are appended, while an array table with the same name is rejected.
func SetTOMLTable ¶
SetTOMLTable replaces table `name` with the given keys, or appends it. An existing array table with that name is rejected.
func SetTOMLTables ¶ added in v0.1.72
SetTOMLTables replaces every ordinary or array table whose name begins with one of the prefixes by the given tables, appended at the end in one write: a set of tables magpie owns as a whole (one per model of its catalog), which would otherwise be rewritten once per table. With no tables it only removes.
func SetTOMLTop ¶
SetTOMLTop sets top-level keys in a TOML file, as strings. An existing key is replaced as a whole, its value's every line with it; new keys go right after the last existing top-level key's value, or first in the file when there is none. Every key is located in the file as it was read, so the edit is one write that either happens entirely or not at all.
func SetYAML ¶ added in v0.1.32
SetYAML sets key paths in a YAML file; a value may be a scalar, a map, a slice, a struct with yaml tags or YAMLText. Missing files and parents are created.
func SetYAMLTop ¶
SetYAMLTop sets top-level scalar keys in a YAML file.
func TOMLTables ¶
TOMLTables lists explicit ordinary table headers in file order, excluding array-table headers. A name is the header's key path: a quoted part keeps its quotes, and whitespace around the dots is not part of it, so `[ a . b ]` is the name `a.b`. Missing files return (nil, nil); errors include the path.
func Target ¶ added in v0.1.503
Target is the file a write to path should replace: path itself, or, when path is a symlink, the file at the end of its links — the path it names even when nothing is there yet. Renaming over the link instead would turn it into a file of its own and leave its target as it was.
func WriteAtomic ¶
WriteAtomic writes data to path via a temp file + rename so a crash can never leave a half-written config behind. File mode is preserved. When path is a symlink (a config kept in a dotfiles repo) the file it points at is written and the link stays; a file with other hard links is written in place, see writeInPlace. Once written, temp files earlier writes of path left behind go, see removeStaleTemps.
Types ¶
type Raw ¶ added in v0.1.57
type Raw string
Raw is a TOML value written as it is: an array or an inline table the caller has already spelled out.