facet edit
facet edit <dir> [OPS] takes one op or an array of ops as JSON, applies
them to the workspace and writes the changed files. The ops come as the
argument or, without one, on standard input. --dry-run computes and prints
everything but writes nothing; --today YYYY-MM-DD sets the day used to
resolve dates (default: the local date).
Each op becomes the same minimal text edit a view makes (How Facet writes): only the lines that must change are touched.
| Op | Fields |
|---|---|
set |
node, key, value (a string, or null to remove the property) |
state |
node, state (todo, doing, done, cancelled, waiting) |
rename |
node, title |
move |
node, parent, index (optional, default last) |
add |
parent, index (optional, default last), node (below) |
delete |
node |
id |
node |
node and parent are refs.
A field an op does not have is refused, so a typo does not pass unnoticed.
The new node of add is an object:
| Field | Holds |
|---|---|
kind |
"item" (a list item) or "heading" |
title |
the title |
state |
optional: a checkbox state, which makes the item a task |
requirement |
optional: a requirement name, written as req~name: |
properties |
optional: an array of { "key": …, "value": … } strings |
index counts the parent’s children before the edit, from 0; items always
come before sub-headings. move can take a node to another file.
id gives the node an id:: made from its title when it has none and
returns it: lowercase ASCII letters, digits and -, at most 40 characters
(cut at a dash), with -2, -3 appended if the id is taken. If the node
already has an id, id returns it and changes nothing; if that id is used
twice, it is refused until you fix the duplicate.
Several ops in one call
Section titled “Several ops in one call”Ops run in order, each against the text the previous ones left, so a ref in
a later op names the node as it is then: an id op can give a node the id
the next op uses, and a line ref must count the lines added or removed by
earlier ops. If any op is refused, nothing is written.
facet edit examples/workspace --dry-run '[ {"op": "id", "node": "trade-fair/plan.md:32"}, {"op": "set", "node": "!final-guest-count-to-caterer", "key": "owner", "value": "@kofi"}, {"op": "state", "node": "!stand-signed", "state": "done"}]'The output lists, per op, the ref of the node after it (the new node for
add, none for delete), the id for id, and the text edits it made, plus
the files changed. Edit positions are 1-based, the end is exclusive, and
they refer to the text as it was before that op:
{ "ok": true, "results": [ { "op": "id", "ref": "!final-guest-count-to-caterer", "id": "final-guest-count-to-caterer", "edits": [ { "path": "trade-fair/plan.md", "start": { "line": 32, "column": 83 }, "end": { "line": 32, "column": 83 }, "newText": " [id:: final-guest-count-to-caterer]" } ] }, { "op": "set", "ref": "!final-guest-count-to-caterer", "edits": [ { "path": "trade-fair/plan.md", "start": { "line": 33, "column": 1 }, "end": { "line": 33, "column": 1 }, "newText": " owner:: @kofi\n" } ] }, { "op": "state", "ref": "!stand-signed", "edits": [ { "path": "trade-fair/plan.md", "start": { "line": 8, "column": 4 }, "end": { "line": 8, "column": 5 }, "newText": "x" } ] } ], "files": ["trade-fair/plan.md"], "dryRun": true}Adding a node from standard input
Section titled “Adding a node from standard input”Ops on standard input avoid quoting trouble in scripts:
echo '{"op": "add", "parent": "trade-fair/plan.md:4", "index": 1, "node": {"kind": "item", "title": "Book stand lighting", "state": "todo", "properties": [{"key": "owner", "value": "@kofi"}, {"key": "duration", "value": "2 days"}]}}' \ | facet edit examples/workspace --dry-runThe new task goes second under the Booth heading, and the result names it by
its new line, trade-fair/plan.md:7:
- [ ] Book stand lighting [owner:: @kofi] [duration:: 2 days]Refusals and exit codes
Section titled “Refusals and exit codes”A refusal prints { "ok": false, "index": 1, "error": "…" }, where index
is the refused op (from 0) and error names the field, for example
ops[0].node: unknown id "!nope". index is absent when the input itself is
malformed: not JSON, an empty array, an unknown op or a field of the wrong
type.
| Code | When |
|---|---|
| 0 | every op succeeded (and the files were written, unless --dry-run) |
| 1 | an op was refused; nothing was written |
| 2 | malformed input, a folder that cannot be read, or a failed write |
The output always goes to standard output as JSON, errors included. Files
are written only after every op succeeded. If the disk refuses a write, the
files written before it stay written and the error says cannot write,
listing them.