@agledger/cli 2.0.0 → 2.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/README.md +77 -25
- package/SKILL.md +15 -14
- package/bin/run.js +2 -2
- package/dist/api-client.d.ts +7 -0
- package/dist/api-client.js +44 -8
- package/dist/base.d.ts +97 -12
- package/dist/base.js +90 -37
- package/dist/cli/command.d.ts +58 -0
- package/dist/cli/command.js +44 -0
- package/dist/cli/flags.d.ts +76 -0
- package/dist/cli/flags.js +12 -0
- package/dist/cli/help.d.ts +6 -0
- package/dist/cli/help.js +107 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.js +3 -0
- package/dist/cli/main.d.ts +22 -0
- package/dist/cli/main.js +223 -0
- package/dist/cli/parse.d.ts +20 -0
- package/dist/cli/parse.js +138 -0
- package/dist/commands/api.d.ts +106 -18
- package/dist/commands/api.js +103 -12
- package/dist/commands/auth.d.ts +35 -6
- package/dist/commands/auth.js +1 -1
- package/dist/commands/config.d.ts +43 -8
- package/dist/commands/config.js +9 -8
- package/dist/commands/discover.d.ts +35 -6
- package/dist/commands/docs.d.ts +41 -7
- package/dist/commands/docs.js +4 -4
- package/dist/commands/help-json.d.ts +39 -7
- package/dist/commands/help-json.js +27 -45
- package/dist/commands/list-commands.d.ts +36 -7
- package/dist/commands/list-commands.js +2 -2
- package/dist/commands/login.d.ts +66 -10
- package/dist/commands/login.js +30 -9
- package/dist/commands/logout.d.ts +41 -7
- package/dist/commands/logout.js +8 -8
- package/dist/commands/verify.d.ts +131 -11
- package/dist/commands/verify.js +214 -25
- package/dist/oidc.d.ts +47 -0
- package/dist/oidc.js +162 -3
- package/dist/util/config.d.ts +16 -1
- package/dist/util/config.js +50 -14
- package/dist/util/field-parser.d.ts +3 -1
- package/dist/util/field-parser.js +11 -5
- package/package.json +9 -21
- package/dist/hooks/command-not-found.d.ts +0 -3
- package/dist/hooks/command-not-found.js +0 -63
- package/oclif.manifest.json +0 -857
package/dist/cli/main.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { allFlags, ExitError } from './command.js';
|
|
3
|
+
import { ConfigFileError } from '../util/config.js';
|
|
4
|
+
import { commandHelp, rootHelp, versionLine } from './help.js';
|
|
5
|
+
import { UsageError, beforeTerminator } from './parse.js';
|
|
6
|
+
/**
|
|
7
|
+
* Every command, loaded on demand so `agledger verify` does not pay for the
|
|
8
|
+
* modules of the commands it does not run. `tests/commands.test.ts` holds
|
|
9
|
+
* this list to the files in `src/commands`.
|
|
10
|
+
*/
|
|
11
|
+
export const COMMANDS = {
|
|
12
|
+
api: () => import('../commands/api.js'),
|
|
13
|
+
auth: () => import('../commands/auth.js'),
|
|
14
|
+
config: () => import('../commands/config.js'),
|
|
15
|
+
discover: () => import('../commands/discover.js'),
|
|
16
|
+
docs: () => import('../commands/docs.js'),
|
|
17
|
+
'help-json': () => import('../commands/help-json.js'),
|
|
18
|
+
'list-commands': () => import('../commands/list-commands.js'),
|
|
19
|
+
login: () => import('../commands/login.js'),
|
|
20
|
+
logout: () => import('../commands/logout.js'),
|
|
21
|
+
verify: () => import('../commands/verify.js'),
|
|
22
|
+
};
|
|
23
|
+
/** Load one command's class by id, or undefined for an id that names none. */
|
|
24
|
+
export async function loadCommand(id) {
|
|
25
|
+
const load = Object.hasOwn(COMMANDS, id) ? COMMANDS[id] : undefined;
|
|
26
|
+
return load ? (await load()).default : undefined;
|
|
27
|
+
}
|
|
28
|
+
export function loadConfig() {
|
|
29
|
+
const pjson = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8'));
|
|
30
|
+
return { bin: 'agledger', name: pjson.name, version: pjson.version, description: pjson.description };
|
|
31
|
+
}
|
|
32
|
+
/** Levenshtein distance, iterative two-row form. */
|
|
33
|
+
function distance(a, b) {
|
|
34
|
+
if (a === b)
|
|
35
|
+
return 0;
|
|
36
|
+
if (a.length === 0)
|
|
37
|
+
return b.length;
|
|
38
|
+
if (b.length === 0)
|
|
39
|
+
return a.length;
|
|
40
|
+
let previous = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
41
|
+
for (let i = 0; i < a.length; i++) {
|
|
42
|
+
const current = [i + 1];
|
|
43
|
+
for (let j = 0; j < b.length; j++) {
|
|
44
|
+
const substitute = (previous[j] ?? 0) + (a[i] === b[j] ? 0 : 1);
|
|
45
|
+
current.push(Math.min((current[j] ?? 0) + 1, (previous[j + 1] ?? 0) + 1, substitute));
|
|
46
|
+
}
|
|
47
|
+
previous = current;
|
|
48
|
+
}
|
|
49
|
+
return previous[b.length] ?? 0;
|
|
50
|
+
}
|
|
51
|
+
/** Closest command id, when it is close enough to be worth suggesting. */
|
|
52
|
+
function nearest(id, candidates) {
|
|
53
|
+
let best;
|
|
54
|
+
let bestScore = Infinity;
|
|
55
|
+
for (const candidate of candidates) {
|
|
56
|
+
const score = distance(id.toLowerCase(), candidate.toLowerCase());
|
|
57
|
+
if (score < bestScore) {
|
|
58
|
+
bestScore = score;
|
|
59
|
+
best = candidate;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// Scale with the length of what was typed so short ids don't match everything.
|
|
63
|
+
return best !== undefined && bestScore <= Math.max(2, Math.floor(id.length / 2)) ? best : undefined;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Unknown commands printed only "command X not found", with no nearest match
|
|
67
|
+
* and no pointer to how to find the real one. An agent that guessed a verb had
|
|
68
|
+
* nothing to recover from, which matters here because the CLI deliberately has
|
|
69
|
+
* few commands: almost everything goes through `agledger api`, and a wrong
|
|
70
|
+
* guess is the expected first move.
|
|
71
|
+
*/
|
|
72
|
+
function commandNotFound(id) {
|
|
73
|
+
const suggestion = nearest(id, Object.keys(COMMANDS));
|
|
74
|
+
const parts = [
|
|
75
|
+
suggestion ? `Did you mean \`agledger ${suggestion}\`?` : undefined,
|
|
76
|
+
'Run `agledger list-commands` to see every command.',
|
|
77
|
+
// The real surface area is the API passthrough, not the command list.
|
|
78
|
+
'Most operations go through `agledger api <METHOD> <PATH>`, which reaches every route.',
|
|
79
|
+
].filter(Boolean);
|
|
80
|
+
return JSON.stringify({
|
|
81
|
+
error: true,
|
|
82
|
+
code: 'COMMAND_NOT_FOUND',
|
|
83
|
+
message: `Command "${id}" not found.`,
|
|
84
|
+
...(suggestion ? { didYouMean: suggestion } : {}),
|
|
85
|
+
suggestion: parts.join(' '),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/** End the process once what was written to stdout and stderr has gone out. */
|
|
89
|
+
function finish(code) {
|
|
90
|
+
process.exitCode = code;
|
|
91
|
+
process.stdout.write('', () => {
|
|
92
|
+
process.stderr.write('', () => process.exit(code));
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/** Flags a user may type to ask for help, and the word that does the same. */
|
|
96
|
+
const HELP_WORD = 'help';
|
|
97
|
+
/**
|
|
98
|
+
* Whether argv asks for help: `--help` or `-h` before any `--`, and not as the
|
|
99
|
+
* value of a flag that takes one (`--data --help` sends the text `--help`).
|
|
100
|
+
*/
|
|
101
|
+
function requestsHelp(argv, flags) {
|
|
102
|
+
const takesValue = (name) => flags[name]?.type === 'option';
|
|
103
|
+
const byChar = new Map(Object.entries(flags).flatMap(([name, def]) => (def.char ? [[def.char, name]] : [])));
|
|
104
|
+
for (let i = 0; i < argv.length; i++) {
|
|
105
|
+
const token = argv[i] ?? '';
|
|
106
|
+
if (token === '--')
|
|
107
|
+
return false;
|
|
108
|
+
if (token === '--help' || token === '-h')
|
|
109
|
+
return true;
|
|
110
|
+
if (token.startsWith('--')) {
|
|
111
|
+
if (!token.includes('=') && takesValue(token.slice(2)))
|
|
112
|
+
i++;
|
|
113
|
+
}
|
|
114
|
+
else if (token.length === 2 && token.startsWith('-')) {
|
|
115
|
+
const name = byChar.get(token.slice(1));
|
|
116
|
+
if (name !== undefined && takesValue(name))
|
|
117
|
+
i++;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
function writeUsageError(commandId, message) {
|
|
123
|
+
process.stderr.write(JSON.stringify({
|
|
124
|
+
error: true,
|
|
125
|
+
code: 'USAGE_ERROR',
|
|
126
|
+
message,
|
|
127
|
+
suggestion: `Run \`agledger ${commandId} --help\` to see usage.`,
|
|
128
|
+
}) + '\n');
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The entry point: `--version`, `--help`, command lookup and dispatch. Exit
|
|
132
|
+
* codes: a command's own through `exit()`, 2 for a command line the parser
|
|
133
|
+
* refuses or a command that does not exist, 1 for anything unexpected.
|
|
134
|
+
*
|
|
135
|
+
* Global flags may come before the command (`agledger --json api GET /health`):
|
|
136
|
+
* the command is the first word that names one, and what surrounds it is the
|
|
137
|
+
* command's own argv.
|
|
138
|
+
*/
|
|
139
|
+
export async function main(argv) {
|
|
140
|
+
const config = loadConfig();
|
|
141
|
+
const flagArgs = beforeTerminator(argv);
|
|
142
|
+
const quiet = flagArgs.includes('--quiet');
|
|
143
|
+
const first = argv[0];
|
|
144
|
+
// A reader that closed the pipe (`| head -1`) has what it wanted.
|
|
145
|
+
process.stdout.on('error', (err) => {
|
|
146
|
+
if (err.code === 'EPIPE')
|
|
147
|
+
process.exit(0);
|
|
148
|
+
throw err;
|
|
149
|
+
});
|
|
150
|
+
if (first === '--version') {
|
|
151
|
+
process.stdout.write(`${versionLine(config)}\n`);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const isCommandWord = (a) => Object.hasOwn(COMMANDS, a) || a === HELP_WORD;
|
|
155
|
+
const index = first === undefined ? -1 : first.startsWith('-') ? flagArgs.findIndex(isCommandWord) : 0;
|
|
156
|
+
const commandId = index === -1 ? undefined : argv[index];
|
|
157
|
+
const rest = index === -1 ? [...argv] : [...argv.slice(0, index), ...argv.slice(index + 1)];
|
|
158
|
+
const loadAll = async () => Object.fromEntries(await Promise.all(Object.keys(COMMANDS).map(async (name) => [name, (await loadCommand(name))])));
|
|
159
|
+
if (commandId === undefined) {
|
|
160
|
+
// A bare `agledger` or `agledger --help` gets the root help; anything else names no command.
|
|
161
|
+
if (first !== undefined && !requestsHelp(argv, {})) {
|
|
162
|
+
if (!quiet)
|
|
163
|
+
process.stderr.write(commandNotFound(first) + '\n');
|
|
164
|
+
finish(2);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
process.stdout.write(rootHelp(config, await loadAll()));
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
// `agledger help [command]`
|
|
171
|
+
const target = commandId === HELP_WORD ? rest.find((a) => !a.startsWith('-')) : commandId;
|
|
172
|
+
if (target === undefined) {
|
|
173
|
+
process.stdout.write(rootHelp(config, await loadAll()));
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const command = await loadCommand(target);
|
|
177
|
+
if (command === undefined) {
|
|
178
|
+
if (!quiet)
|
|
179
|
+
process.stderr.write(commandNotFound(target) + '\n');
|
|
180
|
+
finish(2);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (commandId === HELP_WORD || requestsHelp(rest, allFlags(command))) {
|
|
184
|
+
process.stdout.write(commandHelp(config, target, command));
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
await new command(rest, config).run();
|
|
189
|
+
}
|
|
190
|
+
catch (err) {
|
|
191
|
+
if (err instanceof ExitError) {
|
|
192
|
+
if (err.exitCode !== 0)
|
|
193
|
+
finish(err.exitCode);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (err instanceof UsageError) {
|
|
197
|
+
if (!quiet)
|
|
198
|
+
writeUsageError(commandId, err.message);
|
|
199
|
+
finish(2);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (err instanceof ConfigFileError) {
|
|
203
|
+
if (!quiet) {
|
|
204
|
+
process.stderr.write(JSON.stringify({
|
|
205
|
+
error: true,
|
|
206
|
+
code: 'CONFIG_ERROR',
|
|
207
|
+
message: err.message,
|
|
208
|
+
suggestion: `Fix or remove ${err.path}. It is never overwritten while it cannot be read, so the profiles in it are kept.`,
|
|
209
|
+
}) + '\n');
|
|
210
|
+
}
|
|
211
|
+
finish(2);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (!quiet) {
|
|
215
|
+
process.stderr.write(JSON.stringify({
|
|
216
|
+
error: true,
|
|
217
|
+
code: 'UNKNOWN_ERROR',
|
|
218
|
+
message: err instanceof Error ? err.message : String(err),
|
|
219
|
+
}) + '\n');
|
|
220
|
+
}
|
|
221
|
+
finish(1);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ArgDef, FlagDef } from './flags.js';
|
|
2
|
+
/** A command line the parser refuses: an unknown flag, a missing value or argument. */
|
|
3
|
+
export declare class UsageError extends Error {
|
|
4
|
+
}
|
|
5
|
+
/** The arguments up to a `--`, past which nothing is a flag. */
|
|
6
|
+
export declare function beforeTerminator(argv: readonly string[]): readonly string[];
|
|
7
|
+
export interface ParseResult {
|
|
8
|
+
args: Record<string, string | undefined>;
|
|
9
|
+
flags: Record<string, string | string[] | boolean | undefined>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Turn argv into positional arguments and flags, as the definitions describe.
|
|
13
|
+
*
|
|
14
|
+
* Node's `parseArgs` does the tokenizing (long and short flags, `--flag=value`,
|
|
15
|
+
* `-Fvalue`, the `--` terminator). It runs non-strict, with tokens, because the
|
|
16
|
+
* strict mode refuses an option value that starts with a dash, such as
|
|
17
|
+
* `--data -1`. Everything else (unknown flags, missing values, required
|
|
18
|
+
* arguments, env fallbacks, defaults) is decided here.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parse(argv: readonly string[], argDefs: Record<string, ArgDef>, flagDefs: Record<string, FlagDef>): ParseResult;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util';
|
|
2
|
+
/** A command line the parser refuses: an unknown flag, a missing value or argument. */
|
|
3
|
+
export class UsageError extends Error {
|
|
4
|
+
}
|
|
5
|
+
/** The arguments up to a `--`, past which nothing is a flag. */
|
|
6
|
+
export function beforeTerminator(argv) {
|
|
7
|
+
const end = argv.indexOf('--');
|
|
8
|
+
return end === -1 ? argv : argv.slice(0, end);
|
|
9
|
+
}
|
|
10
|
+
/** A `-F`/`-f` field as it is written: a path (letters, digits, `_`, `.`, `-`, `[]`) then `=`. */
|
|
11
|
+
const FIELD_SHAPE = /^[A-Za-z_][\w.[\]-]*=/;
|
|
12
|
+
/**
|
|
13
|
+
* Turn argv into positional arguments and flags, as the definitions describe.
|
|
14
|
+
*
|
|
15
|
+
* Node's `parseArgs` does the tokenizing (long and short flags, `--flag=value`,
|
|
16
|
+
* `-Fvalue`, the `--` terminator). It runs non-strict, with tokens, because the
|
|
17
|
+
* strict mode refuses an option value that starts with a dash, such as
|
|
18
|
+
* `--data -1`. Everything else (unknown flags, missing values, required
|
|
19
|
+
* arguments, env fallbacks, defaults) is decided here.
|
|
20
|
+
*/
|
|
21
|
+
export function parse(argv, argDefs, flagDefs) {
|
|
22
|
+
const names = Object.keys(flagDefs);
|
|
23
|
+
const byChar = new Map();
|
|
24
|
+
const config = {};
|
|
25
|
+
for (const name of names) {
|
|
26
|
+
const def = flagDefs[name];
|
|
27
|
+
if (!def)
|
|
28
|
+
continue;
|
|
29
|
+
config[name] = {
|
|
30
|
+
type: def.type === 'boolean' ? 'boolean' : 'string',
|
|
31
|
+
...(def.char ? { short: def.char } : {}),
|
|
32
|
+
};
|
|
33
|
+
if (def.char)
|
|
34
|
+
byChar.set(def.char, name);
|
|
35
|
+
}
|
|
36
|
+
const { tokens } = parseArgs({
|
|
37
|
+
args: [...argv],
|
|
38
|
+
options: config,
|
|
39
|
+
allowPositionals: true,
|
|
40
|
+
strict: false,
|
|
41
|
+
tokens: true,
|
|
42
|
+
});
|
|
43
|
+
const flags = {};
|
|
44
|
+
const positionals = [];
|
|
45
|
+
// The repeatable flag still taking bare values: `-F a=1 b=2` reads b=2 as a
|
|
46
|
+
// second -F unless the flag is non-greedy. A bare value joins it only when it
|
|
47
|
+
// is shaped KEY=VALUE, so `-F a=1 GET /health` still has its two positionals.
|
|
48
|
+
// Any other flag, or the `--` terminator, ends the run.
|
|
49
|
+
let greedy;
|
|
50
|
+
for (const token of tokens) {
|
|
51
|
+
if (token.kind === 'option-terminator') {
|
|
52
|
+
greedy = undefined;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (token.kind === 'positional') {
|
|
56
|
+
if (greedy !== undefined && FIELD_SHAPE.test(token.value)) {
|
|
57
|
+
flags[greedy].push(token.value);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
positionals.push(token.value);
|
|
61
|
+
greedy = undefined;
|
|
62
|
+
}
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const rawName = token.rawName;
|
|
66
|
+
const name = Object.hasOwn(flagDefs, token.name) ? token.name : byChar.get(token.name);
|
|
67
|
+
const def = name === undefined ? undefined : flagDefs[name];
|
|
68
|
+
if (name === undefined || def === undefined) {
|
|
69
|
+
throw new UsageError(`Nonexistent flag: ${rawName}`);
|
|
70
|
+
}
|
|
71
|
+
if (def.type === 'boolean') {
|
|
72
|
+
if (token.inlineValue === true)
|
|
73
|
+
throw new UsageError(`Flag ${rawName} does not take a value`);
|
|
74
|
+
flags[name] = true;
|
|
75
|
+
greedy = undefined;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (token.value === undefined)
|
|
79
|
+
throw new UsageError(`Flag ${rawName} expects a value`);
|
|
80
|
+
if (def.options && !def.options.includes(token.value)) {
|
|
81
|
+
throw new UsageError(`Expected ${token.value} to be one of: ${def.options.join(', ')}`);
|
|
82
|
+
}
|
|
83
|
+
if (def.multiple) {
|
|
84
|
+
const list = flags[name] ?? [];
|
|
85
|
+
list.push(token.value);
|
|
86
|
+
flags[name] = list;
|
|
87
|
+
greedy = def.multipleNonGreedy ? undefined : name;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
flags[name] = token.value;
|
|
91
|
+
greedy = undefined;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
for (const name of names) {
|
|
95
|
+
const def = flagDefs[name];
|
|
96
|
+
if (!def)
|
|
97
|
+
continue;
|
|
98
|
+
if (def.type === 'boolean') {
|
|
99
|
+
flags[name] ??= def.default ?? false;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (flags[name] === undefined && def.env !== undefined) {
|
|
103
|
+
const fromEnv = process.env[def.env];
|
|
104
|
+
if (fromEnv !== undefined)
|
|
105
|
+
flags[name] = def.multiple ? [fromEnv] : fromEnv;
|
|
106
|
+
}
|
|
107
|
+
if (flags[name] === undefined && def.default !== undefined) {
|
|
108
|
+
flags[name] = def.multiple ? [def.default] : def.default;
|
|
109
|
+
}
|
|
110
|
+
if (flags[name] === undefined && def.required) {
|
|
111
|
+
throw new UsageError(`Missing required flag ${name}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const args = {};
|
|
115
|
+
const argNames = Object.keys(argDefs);
|
|
116
|
+
const missing = [];
|
|
117
|
+
argNames.forEach((name, i) => {
|
|
118
|
+
const def = argDefs[name];
|
|
119
|
+
const value = positionals[i];
|
|
120
|
+
if (value === undefined) {
|
|
121
|
+
if (def?.required)
|
|
122
|
+
missing.push(name);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (def?.options && !def.options.includes(value)) {
|
|
126
|
+
throw new UsageError(`Expected ${value} to be one of: ${def.options.join(', ')}`);
|
|
127
|
+
}
|
|
128
|
+
args[name] = value;
|
|
129
|
+
});
|
|
130
|
+
if (missing.length > 0) {
|
|
131
|
+
throw new UsageError(`Missing ${missing.length} required arg${missing.length === 1 ? '' : 's'}: ${missing.join(', ')}`);
|
|
132
|
+
}
|
|
133
|
+
const extra = positionals.slice(argNames.length);
|
|
134
|
+
if (extra.length > 0) {
|
|
135
|
+
throw new UsageError(`Unexpected argument${extra.length === 1 ? '' : 's'}: ${extra.join(', ')}`);
|
|
136
|
+
}
|
|
137
|
+
return { args, flags };
|
|
138
|
+
}
|
package/dist/commands/api.d.ts
CHANGED
|
@@ -2,8 +2,9 @@ import { BaseCommand } from '../base.js';
|
|
|
2
2
|
/**
|
|
3
3
|
* Universal pass-through to the AGLedger API.
|
|
4
4
|
*
|
|
5
|
-
* Design principle: the CLI is a thin
|
|
6
|
-
* every route the API exposes
|
|
5
|
+
* Design principle: the CLI is a thin pass-through to the API. It has no
|
|
6
|
+
* per-route code, so it reaches every route the API exposes, and it does no
|
|
7
|
+
* flag-to-body mapping beyond parsing the -F and -f fields.
|
|
7
8
|
*
|
|
8
9
|
* Params go to query string for GET/DELETE, to JSON body for POST/PUT/PATCH.
|
|
9
10
|
* Body sources compose in order: --data (raw JSON) → --input @file / stdin → -F/-f fields.
|
|
@@ -13,26 +14,113 @@ export default class Api extends BaseCommand {
|
|
|
13
14
|
static description: string;
|
|
14
15
|
static examples: string[];
|
|
15
16
|
static args: {
|
|
16
|
-
method:
|
|
17
|
-
|
|
17
|
+
method: {
|
|
18
|
+
readonly description: "HTTP method (GET, POST, PUT, PATCH, DELETE)";
|
|
19
|
+
readonly required: true;
|
|
20
|
+
};
|
|
21
|
+
path: {
|
|
22
|
+
readonly description: "Full API path including version prefix (e.g. /v1/records, /health, /federation/v1/peer)";
|
|
23
|
+
readonly required: true;
|
|
24
|
+
};
|
|
18
25
|
};
|
|
19
26
|
static flags: {
|
|
20
|
-
json:
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
'
|
|
33
|
-
|
|
27
|
+
json: {
|
|
28
|
+
readonly description: "Force JSON output (default when stdout is piped)";
|
|
29
|
+
readonly default: false;
|
|
30
|
+
} & {
|
|
31
|
+
readonly type: 'boolean';
|
|
32
|
+
};
|
|
33
|
+
quiet: {
|
|
34
|
+
readonly description: "Suppress output (exit code only)";
|
|
35
|
+
readonly default: false;
|
|
36
|
+
} & {
|
|
37
|
+
readonly type: 'boolean';
|
|
38
|
+
};
|
|
39
|
+
'api-key': {
|
|
40
|
+
readonly description: "AGLedger API key";
|
|
41
|
+
readonly env: "AGLEDGER_API_KEY";
|
|
42
|
+
} & {
|
|
43
|
+
readonly type: 'option';
|
|
44
|
+
};
|
|
45
|
+
'api-url': {
|
|
46
|
+
readonly description: "AGLedger API base URL";
|
|
47
|
+
readonly env: "AGLEDGER_API_URL";
|
|
48
|
+
} & {
|
|
49
|
+
readonly type: 'option';
|
|
50
|
+
};
|
|
51
|
+
profile: {
|
|
52
|
+
readonly description: "Stored profile to use for credentials (falls back to the active profile)";
|
|
53
|
+
} & {
|
|
54
|
+
readonly type: 'option';
|
|
55
|
+
};
|
|
56
|
+
verbose: {
|
|
57
|
+
readonly description: "Report which credential was used, and each OIDC cert exchange, as JSON lines on stderr. Never prints a key, token or cert.";
|
|
58
|
+
readonly default: false;
|
|
59
|
+
} & {
|
|
60
|
+
readonly type: 'boolean';
|
|
61
|
+
};
|
|
62
|
+
data: {
|
|
63
|
+
readonly description: "Request body as a JSON string. For GET/DELETE, used as query parameters.";
|
|
64
|
+
} & {
|
|
65
|
+
readonly type: 'option';
|
|
66
|
+
};
|
|
67
|
+
input: {
|
|
68
|
+
readonly description: "Read body JSON from file (or \"-\" for stdin). Shape matches --data.";
|
|
69
|
+
} & {
|
|
70
|
+
readonly type: 'option';
|
|
71
|
+
};
|
|
72
|
+
field: {
|
|
73
|
+
readonly char: "F";
|
|
74
|
+
readonly multiple: true;
|
|
75
|
+
readonly description: "Set a typed field as key=value: true/false/null, numbers that print back as typed (0123 stays a string), or JSON for {...}/[...]. Anything else is a string. Use -f for a value that must stay a string, such as an all-digit identifier. Supports nested (a.b.c=v) and array append (arr[]=v). Repeatable.";
|
|
76
|
+
} & {
|
|
77
|
+
readonly type: 'option';
|
|
78
|
+
};
|
|
79
|
+
'raw-field': {
|
|
80
|
+
readonly char: "f";
|
|
81
|
+
readonly multiple: true;
|
|
82
|
+
readonly description: "Set a field as key=value, taking the value verbatim as a string. Use this for identifiers that look numeric (externalTaskId, correlationId, platformRef): the Server does not coerce a JSON body, so -F id=4821 sends a number and is refused. Same path syntax as -F. Repeatable.";
|
|
83
|
+
} & {
|
|
84
|
+
readonly type: 'option';
|
|
85
|
+
};
|
|
86
|
+
query: {
|
|
87
|
+
readonly description: "Extra query parameters as JSON. Merges over -F fields and --data for GET/DELETE routing.";
|
|
88
|
+
} & {
|
|
89
|
+
readonly type: 'option';
|
|
90
|
+
};
|
|
91
|
+
paginate: {
|
|
92
|
+
readonly description: "Follow cursor pagination, stream all results as NDJSON. Requires GET on a paginated route.";
|
|
93
|
+
readonly default: false;
|
|
94
|
+
} & {
|
|
95
|
+
readonly type: 'boolean';
|
|
96
|
+
};
|
|
97
|
+
'idempotency-key': {
|
|
98
|
+
readonly description: "Dedup key for POST, max 256 chars. One is generated per call and shown by --verbose, and a TIMEOUT or dropped-connection error carries it as `idempotencyKey`. Pass this only when retrying a call that may already have reached the Server: reuse the first attempt's key and it replays the original result instead of creating a second record, where a rerun without it can create a duplicate. Bound to method, route and body, so a retry with a changed body is rejected. A 409 `IDEMPOTENCY_KEY_IN_FLIGHT` says the first attempt is still running: wait for its Retry-After and rerun with the same key. A 409 `IDEMPOTENT_WRITE_COMMITTED` says it already landed: follow its `refreshUrl` rather than retrying. A 503 `SERVER_SHUTTING_DOWN` is retryable with the same key.";
|
|
99
|
+
} & {
|
|
100
|
+
readonly type: 'option';
|
|
101
|
+
};
|
|
102
|
+
output: {
|
|
103
|
+
readonly description: "Write the response body to this file instead of stdout. A text response (the `html`, `csv` or `ndjson` audit export) is written byte for byte as the Server sent it; a JSON response is written pretty-printed.";
|
|
104
|
+
} & {
|
|
105
|
+
readonly type: 'option';
|
|
106
|
+
};
|
|
107
|
+
'dry-run': {
|
|
108
|
+
readonly description: "Show the request that would be sent without calling the API.";
|
|
109
|
+
readonly default: false;
|
|
110
|
+
} & {
|
|
111
|
+
readonly type: 'boolean';
|
|
112
|
+
};
|
|
34
113
|
};
|
|
35
114
|
run(): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* A text response (`format=html|csv|ndjson` on an audit export, `/llms.txt`)
|
|
117
|
+
* reaches the client as `{ _raw, _contentType }`. Printing that wrapper as JSON
|
|
118
|
+
* would hand back an escaped string, so the text goes out as the Server sent
|
|
119
|
+
* it, to stdout or to `--output`. JSON responses print as before.
|
|
120
|
+
*/
|
|
121
|
+
private emit;
|
|
122
|
+
/** Refuse an --output path that could not be written, before anything is sent. */
|
|
123
|
+
private requireWritableOutput;
|
|
36
124
|
/**
|
|
37
125
|
* Merge --data, --input, -F/-f, and --query into a single params object.
|
|
38
126
|
* Precedence (low → high): --data → --input → -F/-f fields → --query.
|