@vigilbase/cli
Version:
Vigilbase CLI — security scanners, agent tools, and Vigilbase One from the terminal.
65 lines (64 loc) • 3.37 kB
JavaScript
import { z } from 'zod';
import { flagArgs, listV1, moreCommand } from '../lib/one-v1.js';
import { getToolRun, listToolRuns } from '../vendor/api-contract/one/v1/support-services.js';
import { limitValue, parseArgument, textValue, toolNameValue, unknownSubcommand, validateArguments } from './legacy-validation.js';
import { ok, outputJson, requireOneClient, withSpinner, } from './helpers.js';
/** --limit reads pages until it has this many runs; without it, one page of the API's 50. */
const MAX_LIMIT = 1000;
/** The next-page command: the cursor is bound to --tool, so it repeats it (and --limit when given). */
function moreLine(ctx, nextCursor, toolName, limit) {
if (nextCursor)
ctx.writer.stdout(moreCommand(['runs', 'list', ...flagArgs('--tool', toolName), ...flagArgs('--limit', limit)], nextCursor));
}
/** `vigilbase runs`: One v1 one.toolRuns.list and one.toolRuns.get. */
export const handleRuns = async (ctx) => {
const subcommand = (ctx.rest[0] ?? 'list').toLowerCase();
let limit, toolName, cursor, runId;
if (subcommand === 'list' || subcommand === 'ls') {
validateArguments(ctx, ctx.rest[0] === undefined ? 0 : 1, ['tool', 'limit', 'cursor']);
limit = ctx.values.limit === undefined ? undefined : limitValue(ctx.values.limit, 50, MAX_LIMIT);
toolName = ctx.values.tool === undefined ? undefined : toolNameValue(ctx.values.tool);
cursor = textValue('--cursor', ctx.values.cursor, 512);
}
else if (subcommand === 'get' || subcommand === 'show') {
validateArguments(ctx, 2, [], `vigilbase runs ${subcommand} <UUID>`);
runId = parseArgument(z.string().uuid(), ctx.rest[1], 'A valid UUID is required. Copy the full run ID from `vigilbase runs list`.');
}
else if (z.string().uuid().safeParse(subcommand).success) {
// `vigilbase runs <uuid>` is a shortcut for get.
validateArguments(ctx, 1);
runId = subcommand;
}
else
throw unknownSubcommand('runs', ctx.rest[0] ?? '');
const one = await requireOneClient(ctx);
if (!runId) {
const page = await withSpinner(ctx, 'Loading tool runs', () => listV1(one, listToolRuns, { query: { toolName } }, { limit, cursor, defaultScope: 'page' }));
if (ctx.mode === 'json') {
outputJson(ctx, page);
return ok();
}
if (!page.items.length)
ctx.writer.stdout('No tool runs.');
for (const run of page.items) {
ctx.writer.stdout(`${run.id} ${run.toolName.padEnd(18)} ${run.status.padEnd(10)} ${run.grade ?? '-'} ${run.target}`);
}
moreLine(ctx, page.nextCursor, toolName, limit);
return ok();
}
const id = runId;
const { body: run } = await withSpinner(ctx, `Loading run ${id}`, () => one.callV1(getToolRun, { params: { runId: id }, query: {} }));
if (ctx.mode === 'json') {
outputJson(ctx, run);
return ok();
}
ctx.writer.stdout(`id ${run.id}`);
ctx.writer.stdout(`tool ${run.toolName}`);
ctx.writer.stdout(`target ${run.target}`);
ctx.writer.stdout(`status ${run.status}`);
if (run.grade)
ctx.writer.stdout(`grade ${run.grade}${run.gradeScore === null ? '' : ` ${run.gradeScore}`}`);
if (run.completedAt)
ctx.writer.stdout(`completed ${run.completedAt}`);
return ok();
};