diff --git a/src/cli.js b/src/cli.js index 97c272d..45dd5ea 100644 --- a/src/cli.js +++ b/src/cli.js @@ -1,7 +1,7 @@ const { readAllStdin } = require('./io'); const { handleCommand } = require('./handler'); const { parseTagBlocksEnvelope } = require('./envelope'); -const { protoApply, protoInit, protoLs, protoPath, protoRender } = require('./proto'); +const { protoApply, protoRemove, protoInit, protoLs, protoPath, protoRender } = require('./proto'); const pkg = require('../package.json'); const fs = require('fs'); const path = require('path'); @@ -99,6 +99,7 @@ async function main() { ' cueme -p|--protocol', ' cueme proto ', ' cueme proto apply ', + ' cueme proto rm|remove ', ' cueme proto init', ' cueme proto ls', ' cueme proto path ', @@ -258,6 +259,17 @@ async function main() { return; } + if (action === 'rm' || action === 'remove') { + const agent = pos[1]; + if (!agent) { + process.stderr.write('error: missing \n'); + process.exitCode = 2; + return; + } + process.stdout.write(protoRemove(String(agent)) + '\n'); + return; + } + if (action === 'path') { const agent = pos[1]; if (!agent) { diff --git a/src/proto.js b/src/proto.js index aaeefd2..eaddc98 100644 --- a/src/proto.js +++ b/src/proto.js @@ -399,6 +399,48 @@ function protoApply(agent) { return `ok: applied to ${targetPath}`; } +function protoRemove(agent) { + const cfg = readConfigOrThrow({ auto_init: true }); + const targetPath = resolveTargetPath({ cfg, agent }); + + let existing = ''; + try { + existing = fs.readFileSync(targetPath, 'utf8'); + } catch { + return `ok: file does not exist: ${targetPath}`; + } + + const beginMatch = existing.match(BEGIN_MARKER_RE); + const endMatch = existing.match(END_MARKER_RE); + + if (!beginMatch || !endMatch || endMatch.index <= beginMatch.index) { + return `ok: no managed block found in: ${targetPath}`; + } + + const beginIdx = beginMatch.index; + const endIdx = endMatch.index; + const endLen = endMatch[0].length; + + const before = existing.slice(0, beginIdx); + const after = existing.slice(endIdx + endLen); + + const eol = detectEol(existing); + const afterTrim = after.startsWith(eol) ? after.slice(eol.length) : after; + const out = before + afterTrim; + + if (out.trim().length === 0) { + try { + fs.unlinkSync(targetPath); + return `ok: removed managed block and deleted empty file: ${targetPath}`; + } catch (err) { + throw new Error(`error: failed to delete file after removing managed block: ${targetPath}: ${err.message}`); + } + } + + fs.writeFileSync(targetPath, out, 'utf8'); + return `ok: removed managed block from: ${targetPath}`; +} + function protoInit() { const { created, path: p, detected } = initConfigIfMissing(); if (!created) return `ok: exists ${p}`; @@ -417,6 +459,7 @@ module.exports = { END_MARKER, getPlatformKey, protoApply, + protoRemove, protoInit, protoLs, protoPath,