UNPKG

@vigilbase/cli

Version:

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

93 lines (90 loc) • 4.86 kB
import { sanitizeTerminal, singleLine } from '../lib/format.js'; import { InvalidArgumentError, limitValue, unknownSubcommand, validateArguments } from './legacy-validation.js'; import { asArray, asRecord, assertCommandFlags, ok, outputJson, withSpinner } from './helpers.js'; /** The same page-path rule as the Docs API (packages/api-contract/src/docs/v1/operations.ts). */ const DOCS_PATH = /^\/?(?:[a-z0-9][a-z0-9-]*(?:\/[a-z0-9][a-z0-9-]*)*)?\/?$/; export const DOCS_HELP = `Usage: vigilbase docs search <words...> [--limit 1..20] [--json] vigilbase docs page <path> [--json] Search and read docs.vigilbase.com through the Docs API (api.vigilbase.com/docs/v1). No login; no credential or developer key is sent. search Pages that contain every word, best first (default 10 results). Words: 2 to 200 characters in all. page One page as Markdown. <path> is the page URL without the host, for example one/api or tools/developers/agent-api; a full https://docs.vigilbase.com/… link works too. Examples vigilbase docs search api keys vigilbase docs page tools/developers/mcp-server`; /** A docs URL or path as the API's `path`: the host and a trailing `.md` are dropped. */ function docsPath(raw) { let value = raw.trim(); const url = /^https?:\/\//i.test(value) ? safeUrl(value) : null; if (url) { if (url.hostname !== 'docs.vigilbase.com') throw new InvalidArgumentError('Only docs.vigilbase.com pages can be read. Pass a path such as one/api.'); value = url.pathname; } value = value.replace(/\.md$/, ''); if (value.length > 300 || !DOCS_PATH.test(value)) { throw new InvalidArgumentError('<path> must be a docs path such as one/api (lowercase letters, digits, hyphens and slashes).'); } return value.replace(/^\/+|\/+$/g, ''); } function safeUrl(value) { try { return new URL(value); } catch { return null; } } export const handleDocs = async (ctx) => { const subcommand = (ctx.rest[0] ?? '').toLowerCase(); if (subcommand === 'search') { if (ctx.rest.length < 2) throw new InvalidArgumentError('Missing <words>. Usage: vigilbase docs search <words...> [--limit 1..20]'); // Words may come as one quoted argument or several; the API reads at most eight. const q = ctx.rest.slice(1).join(' ').replace(/\s+/g, ' ').trim(); if (q.length < 2 || q.length > 200) throw new InvalidArgumentError('Search words must be 2 to 200 characters in all.'); assertCommandFlags(ctx, ['limit']); const limit = limitValue(ctx.values.limit, 10, 20); const payload = await withSpinner(ctx, 'Searching the docs', () => ctx.publicClient.searchDocs(q, limit)); if (ctx.mode === 'json') { outputJson(ctx, { success: true, query: q, results: payload.results }); return ok(); } const results = asArray(payload.results).map(asRecord).filter((item) => item !== null); if (!results.length) { ctx.writer.stdout(`No docs pages match "${singleLine(q, 80)}".`); return ok(); } const lines = results.flatMap((result) => { const heading = asRecord(result.heading); const title = singleLine(String(result.title ?? ''), 120); const under = heading && typeof heading.text === 'string' ? ` › ${singleLine(heading.text, 80)}` : ''; const description = singleLine(String(result.description ?? ''), 200); return [`${title}${under}`, ` ${singleLine(String(result.url ?? ''), 300)}`, ...(description ? [` ${description}`] : []), '']; }); ctx.writer.stdout(lines.join('\n').trimEnd()); return ok(); } if (subcommand === 'page' || subcommand === 'get' || subcommand === 'read') { validateArguments(ctx, 2, [], 'vigilbase docs page <path>'); const path = docsPath(ctx.rest[1]); const page = await withSpinner(ctx, 'Loading the docs page', () => ctx.publicClient.getDocsPage(path)); if (ctx.mode === 'json') { outputJson(ctx, { success: true, ...page }); return ok(); } // Markdown keeps its newlines and tabs; control sequences, bidi overrides and invisible text are removed. const title = singleLine(String(page.title ?? ''), 200); const url = singleLine(String(page.url ?? ''), 300); const markdown = sanitizeTerminal(String(page.markdown ?? '')).trim(); ctx.writer.stdout([`# ${title}`, url, '', markdown].join('\n')); return ok(); } if (!subcommand) throw new InvalidArgumentError('Missing docs command. Usage: vigilbase docs search <words...> | vigilbase docs page <path>'); throw unknownSubcommand('docs', ctx.rest[0] ?? ''); };