ShtukbaseStandard
файл пакета→ .shtukbase/packages/alarms/actions/close.ts

actions/close.ts

32 строки, 1.6 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // `shtuk run alarms close --watch disk-low --by "watcher: the check passes again" [--dismiss] [--drill]`
  2. // `shtuk run alarms close /alarms/<name>.md --by "…" [--dismiss]`
  3. // Closes the open alarms of a watch (or one alarm by its path): status closed (dismissed with --dismiss), closedBy,
  4. // closedAt. Prints the path of each alarm it closed; none open is not an error.
  5. import { join } from 'node:path';
  6. import { Project } from '../../collections/checks/lib/speclib.ts';
  7. import { phase, recordsOf, text } from '../../agents/lib/factlib.ts';
  8. import { args, now, update } from '../../agents/lib/write.ts';
  9. function fail(msg: string): never {
  10. console.error(msg);
  11. process.exit(2);
  12. }
  13. const a = args(process.argv.slice(2));
  14. const by = typeof a.by === 'string' ? a.by : fail('--by is required: who or what closed it, and why');
  15. const p = new Project(process.cwd());
  16. const open = recordsOf(p, 'ALARM').filter((r) => phase(r.fm.status) === 'open');
  17. const page = a._[0] ? '/' + a._[0].replace(/^\/+/, '').replace(/(\.md)?$/, '.md') : null;
  18. const drill = a.drill === true;
  19. const hit = page
  20. ? open.filter((r) => r.id === page)
  21. : typeof a.watch === 'string'
  22. ? open.filter((r) => text(r.fm.watch) === a.watch && (r.fm.drill === true) === drill)
  23. : fail('--watch <watcher> or the alarm address is required');
  24. if (page && !hit.length) fail(`${page}: no open alarm with this address`);
  25. const t = now();
  26. for (const r of hit) {
  27. update(join(p.root, r.id.replace(/^\/+/, '')), { status: a.dismiss === true ? 'dismissed' : 'closed', closedBy: by, closedAt: t });
  28. console.log(r.id);
  29. }
  30. if (!hit.length) console.error(`watcher ${a.watch} has no open alarms`);