@basementuniverse/jsonpad-cli 1.0.0 → 1.2.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/CHANGELOG.md +69 -1
- package/README.md +242 -15
- package/REFERENCE.md +1292 -3
- package/build/cli.js +8 -0
- package/build/client.js +185 -0
- package/build/commands/config.js +134 -0
- package/build/commands/history.js +205 -0
- package/build/commands/identities.js +372 -0
- package/build/commands/indexes.js +255 -0
- package/build/commands/items.js +329 -0
- package/build/commands/listen.js +173 -0
- package/build/commands/lists.js +237 -0
- package/build/commands/transfer.js +246 -0
- package/build/commands/whoami.js +121 -0
- package/build/completion.js +170 -0
- package/build/config.js +158 -0
- package/build/context.js +24 -8
- package/build/errors.js +56 -0
- package/build/indexes.js +24 -1
- package/build/input.js +167 -0
- package/build/output.js +191 -0
- package/build/program.js +85 -20
- package/build/realtime.js +33 -0
- package/build/reference.js +34 -15
- package/build/resources.js +224 -0
- package/package.json +5 -3
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { InvalidArgumentError, Option } from 'commander';
|
|
2
|
+
import { apiError, CliError, EXIT_NOT_FOUND } from './errors.js';
|
|
3
|
+
import { collectList, readJsonInput } from './input.js';
|
|
4
|
+
import { formatNumber, printPage, printRecords, resolveOutputFormat, } from './output.js';
|
|
5
|
+
export const MAX_PAGE_SIZE = 100;
|
|
6
|
+
function parsePositiveInteger(value) {
|
|
7
|
+
const number = Number(value);
|
|
8
|
+
if (!Number.isInteger(number) || number < 1) {
|
|
9
|
+
throw new InvalidArgumentError('Must be a whole number, 1 or more.');
|
|
10
|
+
}
|
|
11
|
+
return number;
|
|
12
|
+
}
|
|
13
|
+
function parseLimit(value) {
|
|
14
|
+
const number = parsePositiveInteger(value);
|
|
15
|
+
if (number > MAX_PAGE_SIZE) {
|
|
16
|
+
throw new InvalidArgumentError(`Must be ${MAX_PAGE_SIZE} or less.`);
|
|
17
|
+
}
|
|
18
|
+
return number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Add --page, --limit, --order and --direction to a command that fetches a
|
|
22
|
+
* page of records
|
|
23
|
+
*/
|
|
24
|
+
export function addPagingOptions(command, order = {}) {
|
|
25
|
+
const orderOption = new Option('--order <field>', order.description ?? 'The field to order by');
|
|
26
|
+
if (order.choices) {
|
|
27
|
+
orderOption.choices(order.choices);
|
|
28
|
+
}
|
|
29
|
+
return command
|
|
30
|
+
.option('--page <number>', 'The page to fetch (default 1)', parsePositiveInteger)
|
|
31
|
+
.option('--limit <number>', `How many to fetch per page, up to ${MAX_PAGE_SIZE} (default 20)`, parseLimit)
|
|
32
|
+
.addOption(orderOption)
|
|
33
|
+
.addOption(new Option('--direction <direction>', 'The order direction').choices([
|
|
34
|
+
'asc',
|
|
35
|
+
'desc',
|
|
36
|
+
]));
|
|
37
|
+
}
|
|
38
|
+
export function pagingParameters(options) {
|
|
39
|
+
return removeUndefined({
|
|
40
|
+
page: options.page,
|
|
41
|
+
limit: options.limit,
|
|
42
|
+
order: options.order,
|
|
43
|
+
direction: options.direction,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Add --name and --no-name for a boolean, so that leaving both out means
|
|
48
|
+
* "don't change it" or "don't filter by it"
|
|
49
|
+
*/
|
|
50
|
+
export function addBooleanOption(command, flag, description, negatedDescription) {
|
|
51
|
+
return command
|
|
52
|
+
.option(`--${flag}`, description)
|
|
53
|
+
.option(`--no-${flag}`, negatedDescription);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Add --tags, which replaces a resource's tags
|
|
57
|
+
*/
|
|
58
|
+
export function addTagsOption(command) {
|
|
59
|
+
return command.option('--tags <tags>', 'Comma-separated tags, replacing any it has (repeatable; pass "" to remove them all)', collectList);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Add --tagged, which filters by tags
|
|
63
|
+
*/
|
|
64
|
+
export function addTaggedOption(command) {
|
|
65
|
+
return command.option('--tagged <tags>', 'Only those with one of these comma-separated tags (repeat the option to require every group)', (value, previous = []) => [...previous, value]);
|
|
66
|
+
}
|
|
67
|
+
export function removeUndefined(object) {
|
|
68
|
+
return Object.fromEntries(Object.entries(object).filter(([, value]) => value !== undefined));
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* A nullable field set with --field <value> and cleared with --no-field, which
|
|
72
|
+
* commander gives as false
|
|
73
|
+
*/
|
|
74
|
+
export function nullable(value) {
|
|
75
|
+
return value === false ? null : value;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Build a request body from --data (a JSON object) and field options, which
|
|
79
|
+
* take precedence over the same fields in --data
|
|
80
|
+
*/
|
|
81
|
+
export async function readBody(context, data, fields) {
|
|
82
|
+
let body = {};
|
|
83
|
+
if (data !== undefined) {
|
|
84
|
+
const parsed = await readJsonInput(context, '--data', data);
|
|
85
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
86
|
+
throw new CliError('--data must be a JSON object');
|
|
87
|
+
}
|
|
88
|
+
body = parsed;
|
|
89
|
+
}
|
|
90
|
+
return { ...body, ...removeUndefined(fields) };
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Make a request, turning an API error into a CliError with the right exit
|
|
94
|
+
* code
|
|
95
|
+
*/
|
|
96
|
+
export async function request(context, send) {
|
|
97
|
+
try {
|
|
98
|
+
return await send();
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
throw apiError(error, context.auth?.apiUrl);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* A JSON pointer as the SDK wants it: without its leading slash, since the SDK
|
|
106
|
+
* adds one
|
|
107
|
+
*/
|
|
108
|
+
export function sdkPointer(pointer) {
|
|
109
|
+
return pointer === undefined ? undefined : pointer.replace(/^\/+/, '');
|
|
110
|
+
}
|
|
111
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
112
|
+
/**
|
|
113
|
+
* Find an identity's id from what was passed on the command line: its id,
|
|
114
|
+
* group/name, or the name of an identity with no group
|
|
115
|
+
*
|
|
116
|
+
* The API only looks identities up by id, and its name and group filters
|
|
117
|
+
* match partially, so the matches are narrowed down to exact ones here
|
|
118
|
+
*/
|
|
119
|
+
export async function resolveIdentityId(context, jsonpad, reference) {
|
|
120
|
+
if (UUID.test(reference)) {
|
|
121
|
+
return reference;
|
|
122
|
+
}
|
|
123
|
+
// Names can't contain a slash, but groups can
|
|
124
|
+
const slash = reference.lastIndexOf('/');
|
|
125
|
+
const group = slash === -1 ? null : reference.slice(0, slash);
|
|
126
|
+
const name = reference.slice(slash + 1);
|
|
127
|
+
const page = await request(context, () => jsonpad.fetchIdentities({
|
|
128
|
+
name,
|
|
129
|
+
...(group ? { group } : {}),
|
|
130
|
+
limit: MAX_PAGE_SIZE,
|
|
131
|
+
}));
|
|
132
|
+
const matches = page.data.filter((identity) => identity.name === name && (identity.group || null) === group);
|
|
133
|
+
if (matches.length === 0) {
|
|
134
|
+
throw new CliError(group
|
|
135
|
+
? `There's no identity named "${name}" in the group "${group}"`
|
|
136
|
+
: `There's no identity named "${name}" without a group. Use group/name for an identity in a group, or its id`, EXIT_NOT_FOUND);
|
|
137
|
+
}
|
|
138
|
+
return matches[0].id;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Add --all and --max to a command that fetches a page of records
|
|
142
|
+
*/
|
|
143
|
+
export function addAllOption(command) {
|
|
144
|
+
return command
|
|
145
|
+
.option('--all', 'Fetch every page, not just one. The output is NDJSON unless --output says otherwise')
|
|
146
|
+
.option('--max <number>', 'With --all, stop after this many', parsePositiveInteger);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Fetch every page of records, a page at a time, with the most records a page
|
|
150
|
+
* can have. Stops after max records, if given
|
|
151
|
+
*
|
|
152
|
+
* Pages are fetched by number, so records added or deleted while this runs
|
|
153
|
+
* can be skipped or repeated. Order by creation date for the most stable
|
|
154
|
+
* results
|
|
155
|
+
*/
|
|
156
|
+
export async function* fetchAll(context, fetchPage, max) {
|
|
157
|
+
let fetched = 0;
|
|
158
|
+
for (let page = 1;; page++) {
|
|
159
|
+
const response = await request(context, () => fetchPage({ page, limit: MAX_PAGE_SIZE }));
|
|
160
|
+
for (const record of response.data) {
|
|
161
|
+
if (max !== undefined && fetched >= max) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
fetched++;
|
|
165
|
+
yield record;
|
|
166
|
+
}
|
|
167
|
+
if (response.data.length < MAX_PAGE_SIZE ||
|
|
168
|
+
page * MAX_PAGE_SIZE >= response.total ||
|
|
169
|
+
(max !== undefined && fetched >= max)) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
export function checkAllOptions(options) {
|
|
175
|
+
if (options.all &&
|
|
176
|
+
(options.page !== undefined || options.limit !== undefined)) {
|
|
177
|
+
throw new CliError("--page and --limit fetch one page, so they can't be used with --all. Use --max to limit how many are fetched");
|
|
178
|
+
}
|
|
179
|
+
if (!options.all && options.max !== undefined) {
|
|
180
|
+
throw new CliError('--max only works with --all');
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Fetch and output a page of records, or with --all, every page
|
|
185
|
+
*
|
|
186
|
+
* With --all, NDJSON and ids are output as each page arrives. JSON and tables
|
|
187
|
+
* are output once everything has been fetched
|
|
188
|
+
*/
|
|
189
|
+
export async function printPages(context, options, fetchPage, output) {
|
|
190
|
+
checkAllOptions(options);
|
|
191
|
+
if (!options.all) {
|
|
192
|
+
const format = resolveOutputFormat(context, options);
|
|
193
|
+
const page = await request(context, () => fetchPage(pagingParameters(options)));
|
|
194
|
+
printPage(context, format, page, output);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const format = resolveOutputFormat(context, options, 'ndjson');
|
|
198
|
+
const records = fetchAll(context, parameters => fetchPage({ ...pagingParameters(options), ...parameters }), options.max);
|
|
199
|
+
if (format === 'ndjson' || format === 'id') {
|
|
200
|
+
for await (const record of records) {
|
|
201
|
+
printRecords(context, format, [record], output);
|
|
202
|
+
}
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const all = [];
|
|
206
|
+
for await (const record of records) {
|
|
207
|
+
all.push(record);
|
|
208
|
+
}
|
|
209
|
+
printRecords(context, format, all, output);
|
|
210
|
+
if (format === 'table' && all.length > 0) {
|
|
211
|
+
context.error(context.colours.dim(`${formatNumber(all.length)} total`));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Parse a date option, e.g. --start-at, into an ISO 8601 string
|
|
216
|
+
*/
|
|
217
|
+
export function parseDate(value) {
|
|
218
|
+
const date = new Date(value);
|
|
219
|
+
if (Number.isNaN(date.getTime())) {
|
|
220
|
+
throw new InvalidArgumentError('Must be a date, e.g. 2026-09-01 or 2026-09-01T12:00:00Z.');
|
|
221
|
+
}
|
|
222
|
+
return date.toISOString();
|
|
223
|
+
}
|
|
224
|
+
export { parsePositiveInteger };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basementuniverse/jsonpad-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Command line tool for JSONPad",
|
|
5
5
|
"author": "Gordon Larrigan <gordonlarrigan@gmail.com> (https://gordonlarrigan.com)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,8 +39,10 @@
|
|
|
39
39
|
"record-parity": "npm run build && node scripts/record-parity.ts"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@basementuniverse/jsonpad-sdk": "^1.
|
|
43
|
-
"
|
|
42
|
+
"@basementuniverse/jsonpad-realtime-sdk": "^1.6.0",
|
|
43
|
+
"@basementuniverse/jsonpad-sdk": "^1.14.1",
|
|
44
|
+
"commander": "^15.0.0",
|
|
45
|
+
"xmlhttprequest-ssl": "~2.1.2"
|
|
44
46
|
},
|
|
45
47
|
"devDependencies": {
|
|
46
48
|
"@types/node": "^22.20.3",
|