файл пакета→ .shtukbase/packages/alarms/actions/close.ts
actions/close.ts
32 строки, 1.6 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // `shtuk run alarms close --watch disk-low --by "watcher: the check passes again" [--dismiss] [--drill]`
- // `shtuk run alarms close /alarms/<name>.md --by "…" [--dismiss]`
- // Closes the open alarms of a watch (or one alarm by its path): status closed (dismissed with --dismiss), closedBy,
- // closedAt. Prints the path of each alarm it closed; none open is not an error.
- import { join } from 'node:path';
- import { Project } from '../../collections/checks/lib/speclib.ts';
- import { phase, recordsOf, text } from '../../agents/lib/factlib.ts';
- import { args, now, update } from '../../agents/lib/write.ts';
- function fail(msg: string): never {
- console.error(msg);
- process.exit(2);
- }
- const a = args(process.argv.slice(2));
- const by = typeof a.by === 'string' ? a.by : fail('--by is required: who or what closed it, and why');
- const p = new Project(process.cwd());
- const open = recordsOf(p, 'ALARM').filter((r) => phase(r.fm.status) === 'open');
- const page = a._[0] ? '/' + a._[0].replace(/^\/+/, '').replace(/(\.md)?$/, '.md') : null;
- const drill = a.drill === true;
- const hit = page
- ? open.filter((r) => r.id === page)
- : typeof a.watch === 'string'
- ? open.filter((r) => text(r.fm.watch) === a.watch && (r.fm.drill === true) === drill)
- : fail('--watch <watcher> or the alarm address is required');
- if (page && !hit.length) fail(`${page}: no open alarm with this address`);
- const t = now();
- for (const r of hit) {
- update(join(p.root, r.id.replace(/^\/+/, '')), { status: a.dismiss === true ? 'dismissed' : 'closed', closedBy: by, closedAt: t });
- console.log(r.id);
- }
- if (!hit.length) console.error(`watcher ${a.watch} has no open alarms`);