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

actions/raise.ts

36 строк, 1.7 КБ.

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

  1. // `shtuk run alarms raise --watch disk-low --title "…" [--by /automations/watch] [--severity stops|slows|notice]
  2. // [--drill] [--body "…"]`
  3. // Opens an alarm unless one of the same watch is already open: then it prints that alarm's path and writes nothing
  4. // (exit 0). Prints the path of the alarm either way.
  5. import { Project } from '../../collections/checks/lib/speclib.ts';
  6. import { phase, recordsOf, text } from '../../agents/lib/factlib.ts';
  7. import { args, create, now, slug } from '../../agents/lib/write.ts';
  8. function fail(msg: string): never {
  9. console.error(msg);
  10. process.exit(2);
  11. }
  12. const a = args(process.argv.slice(2));
  13. const watch = typeof a.watch === 'string' ? a.watch : fail('--watch is required: a short name of the watcher, for example disk-low');
  14. const title = typeof a.title === 'string' ? a.title : fail('--title is required: what happened, in one phrase');
  15. const drill = a.drill === true;
  16. const p = new Project(process.cwd());
  17. const same = recordsOf(p, 'ALARM').find((r) => phase(r.fm.status) === 'open' && text(r.fm.watch) === watch && (r.fm.drill === true) === drill);
  18. if (same) {
  19. console.log(same.id);
  20. console.error(`watcher ${watch} already has an open alarm ${same.id}: no new one`);
  21. process.exit(0);
  22. }
  23. const t = now();
  24. const path = create(process.cwd(), p.coll('ALARM') ?? '/alarms', `${t.slice(0, 10)}-${slug(watch)}-${t.slice(11, 16).replace(':', '')}`, {
  25. type: 'Alarm',
  26. title,
  27. status: 'open',
  28. watch,
  29. severity: typeof a.severity === 'string' ? a.severity : undefined,
  30. foundBy: typeof a.by === 'string' ? a.by : undefined,
  31. foundAt: t,
  32. drill: drill || undefined,
  33. }, typeof a.body === 'string' ? a.body : title);
  34. console.log(path);