@vigilbase/cli
Version:
Vigilbase CLI — security scanners, agent tools, and Vigilbase One from the terminal.
55 lines (54 loc) • 2.82 kB
JavaScript
import { z } from 'zod';
const record = z.record(z.string(), z.unknown());
const rows = z.array(record);
const object = (shape) => z.object(shape).passthrough();
const data = (schema) => object({ data: schema });
/** Commands with no One v1 operation yet; the rest call v1 (src/lib/one-v1.ts), whose schemas come from the contract. */
const responseSchemas = {
'customers investigations': data(object({ investigations: rows, nextCursor: z.string().nullable() })),
'customers investigation': data(record),
'customers projects': data(object({ projects: rows })),
'orgs list': object({ organizations: rows }), 'orgs ls': object({ organizations: rows }),
'orgs show': data(record), 'orgs members': z.union([object({ members: rows }), data(rows)]),
'orgs roles': object({ roles: rows, permissionGroups: rows, permissionLabels: record }),
'monitoring health': data(object({ metrics_backend: record, alerting: record })),
'monitoring probes': data(object({ probes: rows })), 'monitoring probe-health': data(object({ probes: rows })),
};
/** Only command-selected One paths reach the bearer-authenticated transport. */
export async function executeOneOperation(client, operation) {
const query = new URLSearchParams();
for (const [key, value] of Object.entries(operation.query ?? {})) {
if (value !== undefined)
query.set(key, String(value));
}
const suffix = query.size ? `?${query.toString()}` : '';
const payload = await client.requestJson(`${operation.path}${suffix}`);
const parsed = z.record(z.string(), z.unknown()).parse(payload);
if (parsed.success === false || parsed.ok === false) {
throw new Error('One API reported an unsuccessful operation.');
}
const schema = responseSchemas[operation.label];
if (!schema || !schema.safeParse(parsed).success) {
throw new Error(`Invalid One API response for ${operation.label}.`);
}
return parsed;
}
function safeText(value) {
return String(value).replace(/[\u0000-\u001f\u007f-\u009f]/g, ' ');
}
/** Render all fields, including pagination and degraded-health metadata. */
export function formatOnePayload(value, indent = '') {
if (value === null || typeof value !== 'object')
return [`${indent}${safeText(value)}`];
const entries = Array.isArray(value)
? value.map((item, index) => [String(index + 1), item])
: Object.entries(value);
if (entries.length === 0)
return [`${indent}${Array.isArray(value) ? '(none)' : '(empty)'}`];
return entries.flatMap(([key, item]) => {
if (item !== null && typeof item === 'object') {
return [`${indent}${safeText(key)}:`, ...formatOnePayload(item, `${indent} `)];
}
return [`${indent}${safeText(key)}: ${safeText(item)}`];
});
}