@vigilbase/cli
Version:
Vigilbase CLI — security scanners, agent tools, and Vigilbase One from the terminal.
51 lines (50 loc) • 2.33 kB
JavaScript
import { validateArguments } from './legacy-validation.js';
import { openUrlInBrowser } from '../lib/open-url.js';
import { asRecord, ok, outputJson, requireOneClient, withSpinner, } from './helpers.js';
export const handleBilling = async (ctx) => {
const subcommand = (ctx.rest[0] ?? 'summary').toLowerCase();
if (!['summary', 'show', 'status', 'portal', 'open'].includes(subcommand))
throw new Error('Usage: vigilbase billing [summary|portal]');
validateArguments(ctx, ctx.rest[0] === undefined ? 0 : 1, ['portal', 'open'].includes(subcommand) ? ['isNoOpen'] : []);
const one = await requireOneClient(ctx);
if (subcommand === 'summary' ||
subcommand === 'show' ||
subcommand === 'status') {
const payload = await withSpinner(ctx, 'Loading billing summary', () => one.getAccountBilling());
if (ctx.mode === 'json') {
outputJson(ctx, payload);
return ok();
}
const billing = asRecord(payload.billing);
if (!billing) {
ctx.writer.stdout('No billing profile.');
return ok();
}
const customer = asRecord(billing.customer);
ctx.writer.stdout(`customer ${String(customer?.name || customer?.id || '-')}`);
ctx.writer.stdout(`stripe ${billing.hasStripeCustomer ? 'linked' : 'not linked'}`);
ctx.writer.stdout(`services ${String(billing.serviceCount ?? 0)}`);
ctx.writer.stdout(`endpoints ${String(billing.endpointCount ?? 0)}`);
return ok();
}
if (subcommand === 'portal' || subcommand === 'open') {
const payload = await withSpinner(ctx, 'Creating billing portal session', () => one.createBillingPortal());
const url = payload.url;
if (!url) {
throw new Error('Billing portal did not return a URL.');
}
if (ctx.mode === 'json') {
outputJson(ctx, payload);
return ok();
}
ctx.writer.stdout(url);
if (!ctx.values.isNoOpen) {
const opened = openUrlInBrowser(url);
if (opened.isOpened) {
ctx.writer.stderr('browser opened — Stripe Customer Portal');
}
}
return ok();
}
throw new Error('Usage: vigilbase billing [summary|portal] [--json] [--no-open]');
};