UNPKG

@vigilbase/cli

Version:

Vigilbase CLI — security scanners, agent tools, and Vigilbase One from the terminal.

62 lines (61 loc) • 3.33 kB
import { z } from 'zod'; import { flagArgs, listV1, moreCommand } from '../lib/one-v1.js'; import { listAccountWatchdogScans } from '../vendor/api-contract/one/v1/monitoring.js'; import { limitValue, parseArgument, textValue, unknownSubcommand, validateArguments } from './legacy-validation.js'; import { asArray, asRecord, ok, outputJson, requireOneClient, withSpinner, } from './helpers.js'; export const handleWatchdog = async (ctx) => { const subcommand = (ctx.rest[0] ?? 'list').toLowerCase(); let limit, targetId, cursor; if (subcommand === 'scans' || subcommand === 'scan') { validateArguments(ctx, 1, ['target-id', 'limit', 'cursor']); // 20 by default, as before v1; --limit up to 1000 reads several pages. limit = limitValue(ctx.values.limit, 20, 1000); cursor = textValue('--cursor', ctx.values.cursor, 512); targetId = parseArgument(z.string().uuid().optional(), ctx.values['target-id'], '--target-id must be a UUID. Copy the full ID from its list command.'); } else if (['list', 'targets', 'status'].includes(subcommand)) validateArguments(ctx, ctx.rest[0] === undefined ? 0 : 1); else throw unknownSubcommand('watchdog', ctx.rest[0] ?? ''); const one = await requireOneClient(ctx); if (subcommand === 'list' || subcommand === 'targets' || subcommand === 'status') { const payload = await withSpinner(ctx, 'Loading watchdog targets', () => one.getWatchdogTargets()); if (ctx.mode === 'json') { outputJson(ctx, payload); return ok(); } const targets = asArray(payload.targets); if (!targets.length) { ctx.writer.stdout('No watchdog targets.'); return ok(); } for (const raw of targets) { const target = asRecord(raw); if (!target) continue; ctx.writer.stdout(`${String(target.id ?? '-')} ${String(target.domain).padEnd(28)} ${String(target.uptimeStatus || '-')}${typeof target.uptime24h === 'number' ? ` 24h=${Math.round(target.uptime24h * 100) / 100}` : ''}`); } return ok(); } if (subcommand === 'scans' || subcommand === 'scan') { // One v1 one.accountWatchdogScans.list. const page = await withSpinner(ctx, 'Loading watchdog scans', () => listV1(one, listAccountWatchdogScans, { query: { targetId } }, { limit, cursor, defaultScope: 'page' })); if (ctx.mode === 'json') { outputJson(ctx, page); return ok(); } if (!page.items.length) ctx.writer.stdout('No watchdog scans.'); for (const scan of page.items) { ctx.writer.stdout(`${scan.id} ${(scan.status || '-').padEnd(10)} ${scan.scanType || '-'} ${scan.createdAt ?? ''}`); } // The cursor is bound to --target-id: the next-page command repeats it (and --limit when given). if (page.nextCursor) { ctx.writer.stdout(moreCommand(['watchdog', 'scans', ...flagArgs('--target-id', targetId), ...flagArgs('--limit', ctx.values.limit === undefined ? undefined : limit)], page.nextCursor)); } return ok(); } throw new Error('Usage: vigilbase watchdog [list|scans] [--target-id …] [--limit …] [--json]'); };