@kantadev/cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +223 -0
- package/bin/kanta-mcp.js +8 -0
- package/bin/kanta.js +7 -0
- package/package.json +58 -0
- package/server.json +43 -0
- package/src/cli/commands/account.js +22 -0
- package/src/cli/commands/admin/index.js +136 -0
- package/src/cli/commands/credits.js +25 -0
- package/src/cli/commands/db/companies.js +133 -0
- package/src/cli/commands/db/export.js +137 -0
- package/src/cli/commands/db/index.js +190 -0
- package/src/cli/commands/find.js +57 -0
- package/src/cli/commands/jobs.js +106 -0
- package/src/cli/commands/login.js +120 -0
- package/src/cli/commands/verify.js +44 -0
- package/src/cli/context.js +134 -0
- package/src/cli/format.js +39 -0
- package/src/cli/index.js +42 -0
- package/src/core/auth.js +79 -0
- package/src/core/client.js +293 -0
- package/src/core/config.js +93 -0
- package/src/core/dbFilter.js +72 -0
- package/src/core/errors.js +65 -0
- package/src/core/identity.js +30 -0
- package/src/mcp/register.js +31 -0
- package/src/mcp/server.js +72 -0
- package/src/mcp/tools/admin.js +77 -0
- package/src/mcp/tools/customer.js +165 -0
- package/src/mcp/tools/database.js +209 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { action, getClient, globalOpts, rootProgram, writeOut, CliExit } from '../../context.js';
|
|
2
|
+
import { printJson, printTable } from '../../format.js';
|
|
3
|
+
import { buildPersonFilter } from '../../../core/dbFilter.js';
|
|
4
|
+
|
|
5
|
+
const POLL_INTERVAL_MS = 2000;
|
|
6
|
+
|
|
7
|
+
// Shared export flow for people and companies.
|
|
8
|
+
// config: { filter, kind: 'people'|'companies', max, output, yes, json }
|
|
9
|
+
// deps: { confirm, sleep, write } (all optional; default to real impls)
|
|
10
|
+
export async function runExportFlow(config, client, deps = {}) {
|
|
11
|
+
const confirm = deps.confirm || (async () => false);
|
|
12
|
+
const sleep = deps.sleep || ((ms) => new Promise(r => setTimeout(r, ms)));
|
|
13
|
+
const write = deps.write || writeOut;
|
|
14
|
+
|
|
15
|
+
const isCompany = config.kind === 'companies';
|
|
16
|
+
const filter = config.filter || {};
|
|
17
|
+
const max = parseInt(config.max, 10);
|
|
18
|
+
if (!max || max < 1) throw new CliExit('--max <n> is required and must be >= 1.');
|
|
19
|
+
|
|
20
|
+
const countFn = isCompany ? client.companiesCount : client.dbCount;
|
|
21
|
+
const exportFn = isCompany ? client.companiesExport : client.dbExport;
|
|
22
|
+
|
|
23
|
+
const countRes = await countFn.call(client, filter);
|
|
24
|
+
const count = Number(countRes.count || 0);
|
|
25
|
+
const exact = countRes.exact !== false;
|
|
26
|
+
process.stdout.write(
|
|
27
|
+
`Matches: ${count.toLocaleString()}${exact ? '' : '+'}. ` +
|
|
28
|
+
`Exporting up to ${max.toLocaleString()} rows costs up to ${max.toLocaleString()} db credits ` +
|
|
29
|
+
`(charged per row actually delivered; admin keys are not charged).\n`,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
if (!config.yes) {
|
|
33
|
+
const ok = await confirm('Proceed?');
|
|
34
|
+
if (!ok) {
|
|
35
|
+
process.stdout.write('Aborted.\n');
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let job;
|
|
41
|
+
try {
|
|
42
|
+
job = await exportFn.call(client, filter, max, max);
|
|
43
|
+
} catch (err) {
|
|
44
|
+
if (err && err.code === 'confirm_credits_required' && err.extra && err.extra.estimated_max != null) {
|
|
45
|
+
job = await exportFn.call(client, filter, max, err.extra.estimated_max);
|
|
46
|
+
} else {
|
|
47
|
+
throw err;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const jobId = job.job_id || job.id;
|
|
52
|
+
let status = await client.getExport(jobId);
|
|
53
|
+
while (status.status === 'pending' || status.status === 'running') {
|
|
54
|
+
await sleep(POLL_INTERVAL_MS);
|
|
55
|
+
status = await client.getExport(jobId);
|
|
56
|
+
}
|
|
57
|
+
if (status.status === 'failed') {
|
|
58
|
+
throw new CliExit(`export failed: ${status.error || 'unknown'}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const csv = await client.downloadExport(jobId);
|
|
62
|
+
write(config.output, csv);
|
|
63
|
+
|
|
64
|
+
const parts = [`Delivered ${Number(status.delivered || 0).toLocaleString()} rows`];
|
|
65
|
+
if (status.suppressed) parts.push(`${Number(status.suppressed).toLocaleString()} suppressed`);
|
|
66
|
+
if (status.truncated) parts.push('truncated (credit balance ran out)');
|
|
67
|
+
const msg = `${parts.join(', ')} -> ${config.output}`;
|
|
68
|
+
process.stdout.write(msg + '\n');
|
|
69
|
+
return msg;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const EXPORT_TABLE = [
|
|
73
|
+
{ key: 'id', label: 'ID' },
|
|
74
|
+
{ key: 'kind', label: 'KIND' },
|
|
75
|
+
{ key: 'status', label: 'STATUS' },
|
|
76
|
+
{ key: 'requested_max', label: 'MAX' },
|
|
77
|
+
{ key: 'delivered', label: 'DELIVERED' },
|
|
78
|
+
{ key: 'suppressed', label: 'SUPPRESSED' },
|
|
79
|
+
{ key: 'created_at', label: 'CREATED' },
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
export function registerDbExport(db, { personFilterOptions, buildFilter } = {}) {
|
|
83
|
+
const attachFilters = personFilterOptions || ((c) => c);
|
|
84
|
+
const filterOf = buildFilter || buildPersonFilter;
|
|
85
|
+
|
|
86
|
+
attachFilters(db.command('export'))
|
|
87
|
+
.description('Export matching people to CSV (prints cost, confirms, polls, downloads)')
|
|
88
|
+
.requiredOption('--max <n>', 'maximum rows to export')
|
|
89
|
+
.requiredOption('-o, --output <file>', 'CSV output path')
|
|
90
|
+
.option('-y, --yes', 'skip the cost confirmation prompt')
|
|
91
|
+
.action(
|
|
92
|
+
action(async (opts, cmd) => {
|
|
93
|
+
const client = getClient(cmd);
|
|
94
|
+
const deps = rootProgram(cmd)._deps;
|
|
95
|
+
await runExportFlow(
|
|
96
|
+
{ filter: filterOf(opts), kind: 'people', max: opts.max, output: opts.output, yes: opts.yes },
|
|
97
|
+
client,
|
|
98
|
+
{ confirm: deps.confirm, sleep: deps.sleep },
|
|
99
|
+
);
|
|
100
|
+
}),
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
db.command('exports')
|
|
104
|
+
.description('List your recent Database exports')
|
|
105
|
+
.action(
|
|
106
|
+
action(async (_opts, cmd) => {
|
|
107
|
+
const client = getClient(cmd);
|
|
108
|
+
const res = await client.listExports();
|
|
109
|
+
const list = res.exports || res || [];
|
|
110
|
+
if (globalOpts(cmd).json) return printJson(list);
|
|
111
|
+
printTable(list, EXPORT_TABLE);
|
|
112
|
+
}),
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
db.command('export-status <id>')
|
|
116
|
+
.description('Show one export job status')
|
|
117
|
+
.action(
|
|
118
|
+
action(async (id, _opts, cmd) => {
|
|
119
|
+
const client = getClient(cmd);
|
|
120
|
+
const res = await client.getExport(id);
|
|
121
|
+
if (globalOpts(cmd).json) return printJson(res);
|
|
122
|
+
process.stdout.write(`${JSON.stringify(res, null, 2)}\n`);
|
|
123
|
+
}),
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
db.command('download <id>')
|
|
127
|
+
.description('Download a completed export CSV')
|
|
128
|
+
.requiredOption('-o, --output <file>', 'CSV output path')
|
|
129
|
+
.action(
|
|
130
|
+
action(async (id, opts, cmd) => {
|
|
131
|
+
const client = getClient(cmd);
|
|
132
|
+
const csv = await client.downloadExport(id);
|
|
133
|
+
writeOut(opts.output, csv);
|
|
134
|
+
process.stdout.write(`Wrote ${opts.output}\n`);
|
|
135
|
+
}),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { action, getClient, globalOpts, requireAdmin, writeOut, CliExit, MAX_ROWS } from '../../context.js';
|
|
2
|
+
import { printJson, printTable, toCsv } from '../../format.js';
|
|
3
|
+
import { buildPersonFilter } from '../../../core/dbFilter.js';
|
|
4
|
+
import { registerDbExport } from './export.js';
|
|
5
|
+
import { registerDbCompanies } from './companies.js';
|
|
6
|
+
|
|
7
|
+
// Attach the shared person-filter flags (spec §5) to a command.
|
|
8
|
+
export function personFilterOptions(cmd) {
|
|
9
|
+
return cmd
|
|
10
|
+
.option('--query <text>', 'free-text over name / title / company')
|
|
11
|
+
.option('--seniority <label...>', 'seniority label(s) (c-suite, vp, director, …)')
|
|
12
|
+
.option('--function <name...>', 'job function(s)')
|
|
13
|
+
.option('--title <text>', 'title contains (prefix match)')
|
|
14
|
+
.option('--title-exact <text...>', 'exact normalized title(s)')
|
|
15
|
+
.option('--country <name...>', 'person country/countries')
|
|
16
|
+
.option('--state <name...>', 'person state(s)')
|
|
17
|
+
.option('--company <text>', 'company name (prefix match)')
|
|
18
|
+
.option('--company-domain <domain...>', 'company domain(s)')
|
|
19
|
+
.option('--industry <name...>', 'company industry/industries')
|
|
20
|
+
.option('--employee-range <range...>', 'company employee range(s), e.g. 51-200')
|
|
21
|
+
.option('--has-email', 'only records with an email')
|
|
22
|
+
.option('--has-linkedin', 'only records with a LinkedIn URL')
|
|
23
|
+
.option('--has-phone', 'only records with a phone')
|
|
24
|
+
.option('--verification-status <s...>', 'email verification status (admin only)')
|
|
25
|
+
.option('--filter-file <path>', 'raw JSON filter object, merged under the flags');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function flattenPerson(r) {
|
|
29
|
+
return {
|
|
30
|
+
id: r.id,
|
|
31
|
+
full_name: r.full_name ?? '',
|
|
32
|
+
first_name: r.first_name ?? '',
|
|
33
|
+
last_name: r.last_name ?? '',
|
|
34
|
+
title: r.title ?? '',
|
|
35
|
+
seniority: r.seniority ?? '',
|
|
36
|
+
functions: (r.functions || []).join('; '),
|
|
37
|
+
email: r.email ?? '',
|
|
38
|
+
email_status: r.email_status ?? '',
|
|
39
|
+
linkedin_url: r.linkedin_url ?? '',
|
|
40
|
+
phone: r.phone ?? '',
|
|
41
|
+
company_name: r.company?.name ?? '',
|
|
42
|
+
company_domain: r.company?.domain ?? '',
|
|
43
|
+
company_industry: r.company?.industry ?? '',
|
|
44
|
+
company_employee_range: r.company?.employee_range ?? '',
|
|
45
|
+
city: r.location?.city ?? '',
|
|
46
|
+
state: r.location?.state ?? '',
|
|
47
|
+
country: r.location?.country ?? '',
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function personRow(r) {
|
|
52
|
+
return {
|
|
53
|
+
name: r.full_name ?? '',
|
|
54
|
+
title: r.title ?? '',
|
|
55
|
+
company: r.company?.name ?? '',
|
|
56
|
+
email: r.email ?? '',
|
|
57
|
+
location: [r.location?.city, r.location?.state, r.location?.country].filter(Boolean).join(', '),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const PERSON_TABLE_COLUMNS = [
|
|
62
|
+
{ key: 'name', label: 'NAME' },
|
|
63
|
+
{ key: 'title', label: 'TITLE' },
|
|
64
|
+
{ key: 'company', label: 'COMPANY' },
|
|
65
|
+
{ key: 'email', label: 'EMAIL' },
|
|
66
|
+
{ key: 'location', label: 'LOCATION' },
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
export { personRow, PERSON_TABLE_COLUMNS };
|
|
70
|
+
|
|
71
|
+
export function registerDb(program) {
|
|
72
|
+
const db = program.command('db').description('Kanta Database — search, count, facet, and export B2B contacts');
|
|
73
|
+
|
|
74
|
+
personFilterOptions(db.command('count'))
|
|
75
|
+
.description('Count people matching a filter (free)')
|
|
76
|
+
.action(
|
|
77
|
+
action(async (opts, cmd) => {
|
|
78
|
+
const client = getClient(cmd);
|
|
79
|
+
const res = await client.dbCount(buildPersonFilter(opts));
|
|
80
|
+
if (globalOpts(cmd).json) return printJson(res);
|
|
81
|
+
const n = Number(res.count || 0);
|
|
82
|
+
process.stdout.write(`${n.toLocaleString()}${res.exact === false ? '+' : ''} people\n`);
|
|
83
|
+
}),
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
personFilterOptions(db.command('search'))
|
|
87
|
+
.description('List people matching a filter (free; email/phone masked on non-admin keys)')
|
|
88
|
+
.option('--limit <n>', 'rows per page, 1-100 (also the cap without -o)', '25')
|
|
89
|
+
.option('--cursor <token>', 'pagination cursor from a previous search')
|
|
90
|
+
.option('-o, --output <file>', 'follow pagination and write every match to a CSV file')
|
|
91
|
+
.action(
|
|
92
|
+
action(async (opts, cmd) => {
|
|
93
|
+
const client = getClient(cmd);
|
|
94
|
+
const filter = buildPersonFilter(opts);
|
|
95
|
+
const pageSize = Math.min(Math.max(parseInt(opts.limit, 10) || 25, 1), 100);
|
|
96
|
+
|
|
97
|
+
if (opts.output) {
|
|
98
|
+
// -o means "give me the whole result set" — walk next_cursor until it
|
|
99
|
+
// runs out (or MAX_ROWS as a guard) and write one CSV. Use db export
|
|
100
|
+
// for very large pulls; this is free but sequential.
|
|
101
|
+
const all = [];
|
|
102
|
+
let cursor = opts.cursor;
|
|
103
|
+
do {
|
|
104
|
+
const page = await client.dbSearch(filter, { limit: pageSize, cursor });
|
|
105
|
+
all.push(...(page.records || []));
|
|
106
|
+
cursor = page.next_cursor || null;
|
|
107
|
+
} while (cursor && all.length < MAX_ROWS);
|
|
108
|
+
writeOut(opts.output, toCsv(all.map(flattenPerson)));
|
|
109
|
+
const capped = all.length >= MAX_ROWS ? ` (stopped at the ${MAX_ROWS.toLocaleString()}-row cap — use \`kanta db export\` for more)` : '';
|
|
110
|
+
const noun = all.length === 1 ? 'row' : 'rows';
|
|
111
|
+
process.stdout.write(`Wrote ${all.length.toLocaleString()} ${noun} to ${opts.output}${capped}\n`);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const res = await client.dbSearch(filter, { limit: pageSize, cursor: opts.cursor });
|
|
116
|
+
const records = res.records || [];
|
|
117
|
+
if (globalOpts(cmd).json) return printJson(res);
|
|
118
|
+
printTable(records.map(personRow), PERSON_TABLE_COLUMNS);
|
|
119
|
+
if (res.next_cursor) process.stdout.write(`\nmore: --cursor ${res.next_cursor} (or -o file.csv for all)\n`);
|
|
120
|
+
if (res.total != null) {
|
|
121
|
+
process.stdout.write(`total: ${Number(res.total).toLocaleString()}${res.exact === false ? '+' : ''}\n`);
|
|
122
|
+
}
|
|
123
|
+
}),
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
personFilterOptions(db.command('facets'))
|
|
127
|
+
.description('Term breakdowns for a segment (free)')
|
|
128
|
+
.requiredOption('--field <name...>', 'facet field(s), max 5')
|
|
129
|
+
.action(
|
|
130
|
+
action(async (opts, cmd) => {
|
|
131
|
+
const client = getClient(cmd);
|
|
132
|
+
const res = await client.dbFacets(buildPersonFilter(opts), opts.field);
|
|
133
|
+
const facets = res.facets || res;
|
|
134
|
+
if (globalOpts(cmd).json) return printJson(facets);
|
|
135
|
+
for (const [field, rows] of Object.entries(facets)) {
|
|
136
|
+
process.stdout.write(`\n${field}\n`);
|
|
137
|
+
for (const row of rows || []) {
|
|
138
|
+
process.stdout.write(` ${String(row.count).padStart(9)} ${row.value}\n`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}),
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
db.command('lookup')
|
|
145
|
+
.description('Find one person by email, LinkedIn URL, or name + domain')
|
|
146
|
+
.option('--email <email>')
|
|
147
|
+
.option('--linkedin <url>')
|
|
148
|
+
.option('--name <"First Last">')
|
|
149
|
+
.option('--domain <domain>')
|
|
150
|
+
.action(
|
|
151
|
+
action(async (opts, cmd) => {
|
|
152
|
+
const client = getClient(cmd);
|
|
153
|
+
let params;
|
|
154
|
+
if (opts.email) params = { email: opts.email };
|
|
155
|
+
else if (opts.linkedin) params = { linkedin_url: opts.linkedin };
|
|
156
|
+
else if (opts.name && opts.domain) params = { name: opts.name, domain: opts.domain };
|
|
157
|
+
else throw new CliExit('Provide --email, --linkedin, or --name and --domain.');
|
|
158
|
+
const res = await client.dbLookup(params);
|
|
159
|
+
const record = res && typeof res === 'object' && 'record' in res ? res.record : res ?? null;
|
|
160
|
+
if (globalOpts(cmd).json) return printJson({ record });
|
|
161
|
+
process.stdout.write(record ? `${JSON.stringify(record, null, 2)}\n` : 'no match\n');
|
|
162
|
+
}),
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
registerDbExport(db, { personFilterOptions, buildFilter: buildPersonFilter });
|
|
166
|
+
registerDbCompanies(db);
|
|
167
|
+
|
|
168
|
+
db.command('ca-count')
|
|
169
|
+
.description('California-resident record count (CCPA monitor — admin only)')
|
|
170
|
+
.action(
|
|
171
|
+
action(async (_opts, cmd) => {
|
|
172
|
+
await requireAdmin(cmd);
|
|
173
|
+
const client = getClient(cmd);
|
|
174
|
+
const r = await client.dbCaCount();
|
|
175
|
+
if (globalOpts(cmd).json) return printJson(r);
|
|
176
|
+
const pct = r.threshold ? ((r.california_records / r.threshold) * 100).toFixed(1) : '?';
|
|
177
|
+
process.stdout.write(
|
|
178
|
+
`California records: ${Number(r.california_records).toLocaleString()} / ${Number(r.threshold).toLocaleString()} (${pct}%)\n`,
|
|
179
|
+
);
|
|
180
|
+
if (r.alert_at != null && r.california_records >= r.alert_at) {
|
|
181
|
+
process.stdout.write(
|
|
182
|
+
`WARNING: at/above the alert threshold (${Number(r.alert_at).toLocaleString()}). ` +
|
|
183
|
+
`Register with the CPPA before crossing ${Number(r.threshold).toLocaleString()}.\n`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}),
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
return db;
|
|
190
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
|
+
import { action, getClient, globalOpts, readCsv } from '../context.js';
|
|
3
|
+
import { printJson, printTable } from '../format.js';
|
|
4
|
+
|
|
5
|
+
export function registerFind(program) {
|
|
6
|
+
const find = program
|
|
7
|
+
.command('find')
|
|
8
|
+
.description('Find one email from first name, last name, and company')
|
|
9
|
+
.argument('<first>', 'first name')
|
|
10
|
+
.argument('<last>', 'last name')
|
|
11
|
+
.argument('<company>', 'company name or domain')
|
|
12
|
+
.action(
|
|
13
|
+
action(async (first, last, company, _opts, cmd) => {
|
|
14
|
+
const client = getClient(cmd);
|
|
15
|
+
// Single find is synchronous — the API returns the result inline and
|
|
16
|
+
// only charges a credit on a hit.
|
|
17
|
+
const res = await client.findSingle({ firstName: first, lastName: last, company });
|
|
18
|
+
if (globalOpts(cmd).json) return printJson(res);
|
|
19
|
+
if (!res || !res.email) {
|
|
20
|
+
process.stdout.write('no email found\n');
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
printTable([{ first_name: first, last_name: last, company, ...res }], [
|
|
24
|
+
{ key: 'first_name', label: 'FIRST' },
|
|
25
|
+
{ key: 'last_name', label: 'LAST' },
|
|
26
|
+
{ key: 'company', label: 'COMPANY' },
|
|
27
|
+
{ key: 'email', label: 'EMAIL' },
|
|
28
|
+
{ key: 'confidence', label: 'CONFIDENCE' },
|
|
29
|
+
{ key: 'method', label: 'METHOD' },
|
|
30
|
+
]);
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
find
|
|
35
|
+
.command('bulk')
|
|
36
|
+
.description('Submit a CSV of contacts as a bulk find job')
|
|
37
|
+
.argument('<file>', 'CSV with first_name, last_name, company columns')
|
|
38
|
+
.action(
|
|
39
|
+
action(async (file, _opts, cmd) => {
|
|
40
|
+
const rows = readCsv(file, ['first_name', 'last_name', 'company']);
|
|
41
|
+
const contacts = rows.map(r => ({
|
|
42
|
+
firstName: r.first_name,
|
|
43
|
+
lastName: r.last_name,
|
|
44
|
+
company: r.company,
|
|
45
|
+
}));
|
|
46
|
+
const client = getClient(cmd);
|
|
47
|
+
const res = await client.findBulk(contacts, { filename: basename(file) });
|
|
48
|
+
if (globalOpts(cmd).json) return printJson(res);
|
|
49
|
+
printTable([res], [
|
|
50
|
+
{ key: 'job_id', label: 'JOB ID' },
|
|
51
|
+
{ key: 'total', label: 'TOTAL' },
|
|
52
|
+
{ key: 'status', label: 'STATUS' },
|
|
53
|
+
]);
|
|
54
|
+
process.stdout.write(`\nTrack it: kanta jobs get ${res.job_id}\n`);
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { action, getClient, globalOpts, writeOut, CliExit } from '../context.js';
|
|
2
|
+
import { printJson, printTable, toCsv } from '../format.js';
|
|
3
|
+
|
|
4
|
+
export function registerJobs(program) {
|
|
5
|
+
const jobs = program.command('jobs').description('Manage bulk verify / find jobs');
|
|
6
|
+
|
|
7
|
+
jobs
|
|
8
|
+
.command('list')
|
|
9
|
+
.description('List your recent jobs')
|
|
10
|
+
.option('--status <status>', 'filter by status')
|
|
11
|
+
.action(
|
|
12
|
+
action(async (opts, cmd) => {
|
|
13
|
+
const client = getClient(cmd);
|
|
14
|
+
const { jobs: list } = await client.listJobs({ status: opts.status });
|
|
15
|
+
if (globalOpts(cmd).json) return printJson(list);
|
|
16
|
+
printTable(list, [
|
|
17
|
+
{ key: 'id', label: 'ID' },
|
|
18
|
+
{ key: 'job_type', label: 'TYPE' },
|
|
19
|
+
{ key: 'status', label: 'STATUS' },
|
|
20
|
+
{ key: 'completed', label: 'DONE' },
|
|
21
|
+
{ key: 'total', label: 'TOTAL' },
|
|
22
|
+
{ key: 'filename', label: 'FILE' },
|
|
23
|
+
{ key: 'created_at', label: 'CREATED' },
|
|
24
|
+
]);
|
|
25
|
+
}),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
jobs
|
|
29
|
+
.command('get')
|
|
30
|
+
.description('Show a job status')
|
|
31
|
+
.argument('<id>', 'job id')
|
|
32
|
+
.option('--breakdown', 'include verify status breakdown')
|
|
33
|
+
.action(
|
|
34
|
+
action(async (id, opts, cmd) => {
|
|
35
|
+
const client = getClient(cmd);
|
|
36
|
+
const job = await client.getJob(id);
|
|
37
|
+
let breakdown;
|
|
38
|
+
if (opts.breakdown) breakdown = await client.getJobBreakdown(id);
|
|
39
|
+
if (globalOpts(cmd).json) return printJson(breakdown ? { ...job, breakdown } : job);
|
|
40
|
+
printTable([job], [
|
|
41
|
+
{ key: 'id', label: 'ID' },
|
|
42
|
+
{ key: 'job_type', label: 'TYPE' },
|
|
43
|
+
{ key: 'status', label: 'STATUS' },
|
|
44
|
+
{ key: 'completed', label: 'DONE' },
|
|
45
|
+
{ key: 'total', label: 'TOTAL' },
|
|
46
|
+
]);
|
|
47
|
+
if (breakdown) {
|
|
48
|
+
process.stdout.write('\nBreakdown:\n');
|
|
49
|
+
printTable(
|
|
50
|
+
Object.entries(breakdown.counts).map(([status, count]) => ({ status, count })),
|
|
51
|
+
[{ key: 'status', label: 'STATUS' }, { key: 'count', label: 'COUNT' }],
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
jobs
|
|
58
|
+
.command('results')
|
|
59
|
+
.description('Fetch job results')
|
|
60
|
+
.argument('<id>', 'job id')
|
|
61
|
+
.option('-o, --output <file>', 'write results to a CSV file')
|
|
62
|
+
.action(
|
|
63
|
+
action(async (id, opts, cmd) => {
|
|
64
|
+
const client = getClient(cmd);
|
|
65
|
+
const out = await client.getJobResults(id);
|
|
66
|
+
if (out && out.status && !out.results) {
|
|
67
|
+
throw new CliExit(`Job not complete (status: ${out.status}).`);
|
|
68
|
+
}
|
|
69
|
+
const rows = out.results || [];
|
|
70
|
+
if (opts.output) {
|
|
71
|
+
writeOut(opts.output, toCsv(rows));
|
|
72
|
+
process.stdout.write(`Wrote ${rows.length} rows to ${opts.output}\n`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (globalOpts(cmd).json) return printJson(out);
|
|
76
|
+
printTable(rows);
|
|
77
|
+
}),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
jobs
|
|
81
|
+
.command('cancel')
|
|
82
|
+
.description('Cancel a running verify job')
|
|
83
|
+
.argument('<id>', 'job id')
|
|
84
|
+
.action(simpleAction((client, id) => client.cancelJob(id)));
|
|
85
|
+
|
|
86
|
+
jobs
|
|
87
|
+
.command('resume')
|
|
88
|
+
.description('Resume a paused find job')
|
|
89
|
+
.argument('<id>', 'job id')
|
|
90
|
+
.action(simpleAction((client, id) => client.resumeJob(id)));
|
|
91
|
+
|
|
92
|
+
jobs
|
|
93
|
+
.command('delete')
|
|
94
|
+
.description('Delete a finished job')
|
|
95
|
+
.argument('<id>', 'job id')
|
|
96
|
+
.action(simpleAction((client, id) => client.deleteJob(id)));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function simpleAction(call) {
|
|
100
|
+
return action(async (id, _opts, cmd) => {
|
|
101
|
+
const client = getClient(cmd);
|
|
102
|
+
const res = await call(client, id);
|
|
103
|
+
if (globalOpts(cmd).json) return printJson(res);
|
|
104
|
+
process.stdout.write(JSON.stringify(res) + '\n');
|
|
105
|
+
});
|
|
106
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { createInterface } from 'node:readline';
|
|
2
|
+
import { action, globalOpts, rootProgram, CliExit } from '../context.js';
|
|
3
|
+
import {
|
|
4
|
+
loadConfig,
|
|
5
|
+
setProfile,
|
|
6
|
+
removeProfile,
|
|
7
|
+
configPath,
|
|
8
|
+
maskKey,
|
|
9
|
+
DEFAULT_BASE_URL,
|
|
10
|
+
} from '../../core/config.js';
|
|
11
|
+
import { KantaClient } from '../../core/client.js';
|
|
12
|
+
import { printJson } from '../format.js';
|
|
13
|
+
|
|
14
|
+
function promptHidden(question) {
|
|
15
|
+
return new Promise((resolve) => {
|
|
16
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout, terminal: true });
|
|
17
|
+
const onData = (char) => {
|
|
18
|
+
const s = String(char);
|
|
19
|
+
if (s === '\n' || s === '\r' || s === '') {
|
|
20
|
+
process.stdout.write('\n');
|
|
21
|
+
process.stdin.removeListener('data', onData);
|
|
22
|
+
} else {
|
|
23
|
+
process.stdout.write('*');
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
process.stdout.write(question);
|
|
27
|
+
process.stdin.on('data', onData);
|
|
28
|
+
rl.question('', (answer) => {
|
|
29
|
+
rl.close();
|
|
30
|
+
resolve(answer.trim());
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function readKey() {
|
|
36
|
+
if (process.stdin.isTTY) return promptHidden('Kanta API key: ');
|
|
37
|
+
// piped input
|
|
38
|
+
const chunks = [];
|
|
39
|
+
for await (const c of process.stdin) chunks.push(c);
|
|
40
|
+
return Buffer.concat(chunks).toString('utf8').trim();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function registerLogin(program) {
|
|
44
|
+
program
|
|
45
|
+
.command('login')
|
|
46
|
+
.description('Validate an API key and save it as a profile')
|
|
47
|
+
.option('--profile <name>', 'profile name to save under', 'personal')
|
|
48
|
+
.option('--base-url <url>', 'API base URL', DEFAULT_BASE_URL)
|
|
49
|
+
.action(
|
|
50
|
+
action(async (opts, cmd) => {
|
|
51
|
+
const key = await readKey();
|
|
52
|
+
if (!key) throw new CliExit('No key entered.');
|
|
53
|
+
const baseUrl = opts.baseUrl || DEFAULT_BASE_URL;
|
|
54
|
+
const client = new KantaClient({ key, baseUrl });
|
|
55
|
+
const me = await client.getMe();
|
|
56
|
+
setProfile(opts.profile, { key, base_url: baseUrl });
|
|
57
|
+
if (globalOpts(cmd).json) return printJson({ saved: opts.profile, email: me.email, scope: me.scope });
|
|
58
|
+
process.stdout.write(
|
|
59
|
+
`Saved profile "${opts.profile}" (${me.email}, scope: ${me.scope}) to ${configPath()}\n`,
|
|
60
|
+
);
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
program
|
|
65
|
+
.command('logout')
|
|
66
|
+
.description('Remove a saved profile')
|
|
67
|
+
.option('--profile <name>', 'profile name to remove')
|
|
68
|
+
.action(
|
|
69
|
+
action(async (opts, cmd) => {
|
|
70
|
+
const config = loadConfig();
|
|
71
|
+
const name = opts.profile || config.default_profile;
|
|
72
|
+
if (!name) throw new CliExit('No profile specified and no default set.');
|
|
73
|
+
removeProfile(name);
|
|
74
|
+
if (globalOpts(cmd).json) return printJson({ removed: name });
|
|
75
|
+
process.stdout.write(`Removed profile "${name}".\n`);
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
program
|
|
80
|
+
.command('whoami')
|
|
81
|
+
.description('Show the active profile and identity')
|
|
82
|
+
.action(
|
|
83
|
+
action(async (_opts, cmd) => {
|
|
84
|
+
const o = globalOpts(cmd);
|
|
85
|
+
const id = await rootProgram(cmd)._deps.whoamiFn(o);
|
|
86
|
+
if (o.json) return printJson(id);
|
|
87
|
+
process.stdout.write(
|
|
88
|
+
`profile: ${id.profile || id.source}\nemail: ${id.email}\nscope: ${id.scope}\nplan: ${id.plan}\n`,
|
|
89
|
+
);
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const config = program.command('config').description('Inspect CLI configuration');
|
|
94
|
+
config
|
|
95
|
+
.command('show')
|
|
96
|
+
.description('Show resolved config with the key masked')
|
|
97
|
+
.action(
|
|
98
|
+
action(async (_opts, cmd) => {
|
|
99
|
+
const cfg = loadConfig();
|
|
100
|
+
const masked = {
|
|
101
|
+
path: configPath(),
|
|
102
|
+
default_profile: cfg.default_profile,
|
|
103
|
+
profiles: Object.fromEntries(
|
|
104
|
+
Object.entries(cfg.profiles).map(([n, p]) => [
|
|
105
|
+
n,
|
|
106
|
+
{ key: maskKey(p.key), base_url: p.base_url || DEFAULT_BASE_URL },
|
|
107
|
+
]),
|
|
108
|
+
),
|
|
109
|
+
};
|
|
110
|
+
if (globalOpts(cmd).json) return printJson(masked);
|
|
111
|
+
printJson(masked);
|
|
112
|
+
}),
|
|
113
|
+
);
|
|
114
|
+
config
|
|
115
|
+
.command('path')
|
|
116
|
+
.description('Print the config file path')
|
|
117
|
+
.action(action(async () => {
|
|
118
|
+
process.stdout.write(configPath() + '\n');
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
|
+
import { action, getClient, globalOpts, readCsv, CliExit } from '../context.js';
|
|
3
|
+
import { printJson, printTable } from '../format.js';
|
|
4
|
+
|
|
5
|
+
export function registerVerify(program) {
|
|
6
|
+
const verify = program
|
|
7
|
+
.command('verify')
|
|
8
|
+
.description('Verify a single email address')
|
|
9
|
+
.argument('<email>', 'email address to verify')
|
|
10
|
+
.action(
|
|
11
|
+
action(async (email, _opts, cmd) => {
|
|
12
|
+
const client = getClient(cmd);
|
|
13
|
+
const result = await client.verifySingle(email);
|
|
14
|
+
if (globalOpts(cmd).json) return printJson(result);
|
|
15
|
+
const extra =
|
|
16
|
+
result.credits_remaining !== undefined ? ` (${result.credits_remaining} credits left)` : '';
|
|
17
|
+
process.stdout.write(`${result.email} ${result.status}${extra}\n`);
|
|
18
|
+
}),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
verify
|
|
22
|
+
.command('bulk')
|
|
23
|
+
.description('Submit a CSV of emails as a bulk verify job')
|
|
24
|
+
.argument('<file>', 'CSV file with an "email" column')
|
|
25
|
+
.option('--webhook <url>', 'webhook URL to notify on completion')
|
|
26
|
+
.action(
|
|
27
|
+
action(async (file, opts, cmd) => {
|
|
28
|
+
const rows = readCsv(file, ['email']);
|
|
29
|
+
const emails = rows.map(r => r.email).filter(Boolean);
|
|
30
|
+
if (emails.length === 0) throw new CliExit('No non-empty "email" values in the file.');
|
|
31
|
+
const client = getClient(cmd);
|
|
32
|
+
const res = await client.verifyBulk(emails, {
|
|
33
|
+
filename: basename(file),
|
|
34
|
+
webhook: opts.webhook,
|
|
35
|
+
});
|
|
36
|
+
if (globalOpts(cmd).json) return printJson(res);
|
|
37
|
+
printTable([res], [
|
|
38
|
+
{ key: 'job_id', label: 'JOB ID' },
|
|
39
|
+
{ key: 'total', label: 'TOTAL' },
|
|
40
|
+
{ key: 'status', label: 'STATUS' },
|
|
41
|
+
]);
|
|
42
|
+
}),
|
|
43
|
+
);
|
|
44
|
+
}
|