файл пакета→ .shtukbase/packages/alarms/actions/raise.ts
actions/raise.ts
36 строк, 1.7 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // `shtuk run alarms raise --watch disk-low --title "…" [--by /automations/watch] [--severity stops|slows|notice]
- // [--drill] [--body "…"]`
- // Opens an alarm unless one of the same watch is already open: then it prints that alarm's path and writes nothing
- // (exit 0). Prints the path of the alarm either way.
- import { Project } from '../../collections/checks/lib/speclib.ts';
- import { phase, recordsOf, text } from '../../agents/lib/factlib.ts';
- import { args, create, now, slug } from '../../agents/lib/write.ts';
- function fail(msg: string): never {
- console.error(msg);
- process.exit(2);
- }
- const a = args(process.argv.slice(2));
- const watch = typeof a.watch === 'string' ? a.watch : fail('--watch is required: a short name of the watcher, for example disk-low');
- const title = typeof a.title === 'string' ? a.title : fail('--title is required: what happened, in one phrase');
- const drill = a.drill === true;
- const p = new Project(process.cwd());
- const same = recordsOf(p, 'ALARM').find((r) => phase(r.fm.status) === 'open' && text(r.fm.watch) === watch && (r.fm.drill === true) === drill);
- if (same) {
- console.log(same.id);
- console.error(`watcher ${watch} already has an open alarm ${same.id}: no new one`);
- process.exit(0);
- }
- const t = now();
- const path = create(process.cwd(), p.coll('ALARM') ?? '/alarms', `${t.slice(0, 10)}-${slug(watch)}-${t.slice(11, 16).replace(':', '')}`, {
- type: 'Alarm',
- title,
- status: 'open',
- watch,
- severity: typeof a.severity === 'string' ? a.severity : undefined,
- foundBy: typeof a.by === 'string' ? a.by : undefined,
- foundAt: t,
- drill: drill || undefined,
- }, typeof a.body === 'string' ? a.body : title);
- console.log(path);