Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -99,6 +99,7 @@ async function main() {
' cueme -p|--protocol',
' cueme proto <agent>',
' cueme proto apply <agent>',
' cueme proto rm|remove <agent>',
' cueme proto init',
' cueme proto ls',
' cueme proto path <agent>',
Expand Down Expand Up @@ -258,6 +259,17 @@ async function main() {
return;
}

if (action === 'rm' || action === 'remove') {
const agent = pos[1];
if (!agent) {
process.stderr.write('error: missing <agent>\n');
process.exitCode = 2;
return;
}
process.stdout.write(protoRemove(String(agent)) + '\n');
return;
}

if (action === 'path') {
const agent = pos[1];
if (!agent) {
Expand Down
43 changes: 43 additions & 0 deletions src/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand All @@ -417,6 +459,7 @@ module.exports = {
END_MARKER,
getPlatformKey,
protoApply,
protoRemove,
protoInit,
protoLs,
protoPath,
Expand Down