feat: rm (with --cascade) and unalias commands, v0.1.2
ci / release (push) Successful in 1m13s

This commit is contained in:
2026-11-22 19:05:52 +01:00
parent 7c44fedcab
commit 7d1b6aed5f
3 changed files with 129 additions and 1 deletions
+58 -1
View File
@@ -6,7 +6,7 @@ import std/[os, strutils, sequtils, algorithm, tables, times, osproc,
nativesockets, deques]
const
Version* = "0.1.1"
Version* = "0.1.2"
CycleVerbs* = ["part-of", "supersedes"]
DefaultVerb* = "relates-to"
@@ -493,6 +493,49 @@ proc cmdUnlink*(st: var Store, s0, verb, o0: string): string =
proc suffix(note: string): string =
if note != "": " # " & note else: ""
proc cmdUnalias*(st: var Store, id0, alias: string): string =
## Acts on the entity named, tombstones included (no redirect resolving).
if id0 notin st.entities: fail("entity not found: " & id0)
let a = alias.strip.replace(",", " ").oneLine
var e = st.entities[id0]
let idx = e.aliases.findIt(it.toLowerAscii == a.toLowerAscii)
if a == "" or idx < 0: fail("alias not found on " & id0 & ": " & alias.strip)
e.aliases.delete(idx)
st.saveEntity(e)
"aliases of " & id0 & ": " & e.aliases.join(", ")
proc cmdRm*(st: var Store, id: string, cascade: bool): string =
## Deletes entities/<id>.md. Acts on the named id (a tombstone is not resolved).
st.checkWritable
if id notin st.entities: fail("entity not found: " & id)
let e = st.entities[id]
let rels = st.rels.filterIt(it.s == id or it.o == id)
let relLines = rels.mapIt(" " & it.s & " " & it.v & " " & it.o & suffix(it.note))
if e.redirect != "":
let pointing = toSeq(st.entities.pairs).filterIt(it[1].redirect == id).mapIt(it[0])
if rels.len > 0 or pointing.len > 0:
var msg = "refusing to remove tombstone " & id & " (merged into " & e.redirect &
"); still referenced, resolve first:"
if rels.len > 0: msg.add "\nrelations:\n" & relLines.join("\n")
if pointing.len > 0: msg.add "\nredirects:\n" & pointing.mapIt(" " & it & " -> " & id).join("\n")
fail(msg)
elif rels.len > 0 and not cascade:
fail("refusing to remove " & id & ": it has " & $rels.len &
" relation(s) (use --cascade to remove them too):\n" & relLines.join("\n"))
var lines: seq[string]
if rels.len > 0:
st.rels = st.rels.filterIt(not (it.s == id or it.o == id))
st.saveRels
for l in relLines: lines.add "removed relation" & l[1 .. ^1]
let path = st.entityPath(id)
let fp = if fileExists(path): path else: st.dir / "entities" / (e.fileId & ".md")
removeFile(fp)
st.entities.del id
lines.add "removed entities/" & splitFile(fp).name & ".md"
for t, te in st.entities:
if te.redirect == id: st.warnings.add "tombstone " & t & " now redirects to removed " & id
lines.join("\n")
proc cmdRel*(st: Store, id0: string): string =
let id = st.resolve(id0)
var lines: seq[string]
@@ -722,12 +765,16 @@ commands:
new <id> <type> [label] create entity
label <id> <new label> change label
alias <id> <alias> add alias
unalias <id> <alias> remove alias
link <s> [verb] <o> [note] add relation (default verb: relates-to)
unlink <s> <verb> <o> remove relation
rel <id> all relations of an entity
who <verb> <id> subjects having <verb> relation to id
find <text> search ids, labels, aliases, bodies, notes
path <a> <b> [maxdepth=4] shortest undirected path
rm <id> [--cascade] delete entity; refuses if it has relations
(--cascade removes them too); refuses tombstones
still referenced
merge <from> <into> merge entity, leave redirect tombstone
prefs effective preferences for current context
lint check data; exit 1 on errors
@@ -766,6 +813,16 @@ proc dispatch*(dir: string, args: seq[string], cwd: string,
need(args, 2, "link <s> [verb] <o> [note]")
if args.len == 3: result = cmdLink(st, args[1], DefaultVerb, args[2], "")
else: result = cmdLink(st, args[1], args[2], args[3], args[4 .. ^1].join(" "))
of "unalias":
need(args, 2, "unalias <id> <alias>")
result = cmdUnalias(st, args[1], args[2 .. ^1].join(" "))
of "rm":
need(args, 1, "rm <id> [--cascade]")
let rest = args[1 .. ^1]
let casc = "--cascade" in rest
let ids = rest.filterIt(it != "--cascade")
if ids.len != 1: fail("usage: sirmem rm <id> [--cascade]")
result = cmdRm(st, ids[0], casc)
of "unlink":
need(args, 3, "unlink <s> <verb> <o>")
result = cmdUnlink(st, args[1], args[2], args[3])