Repository navigation
Expand file tree
/
Copy pathbuild.ts
More file actions
559 lines (511 loc) · 20.8 KB
/
Copy pathbuild.ts
File metadata and controls
559 lines (511 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import { Command } from 'commander'
import { CLI_CONTRACT } from '../contract/commands'
import type { CommandSpec, CommandVariantSpec } from '../contract/types'
import { V2_OPERATIONS, type V2OperationName } from '../generated/v2-api'
import { callsOperations } from './called-operations'
import { deriveCommandPath } from './derive'
import { executeOperation } from './execute'
import { retypeApiError } from './naming'
import { addOperationOptions } from './options'
import { warnRenamedCommand } from './renamed'
import {
flagNameFor,
flagSpecFor,
isProfileWorkspacePath,
PROFILE_INJECTED_FIELD,
RESERVED_PROGRAM_FLAGS,
trailingListField,
} from './request'
import type { OperationSpec } from './types'
const GROUP_ALIASES: Readonly<Record<string, string>> = {
'audit-logs': 'audit-log',
credentials: 'credential',
'custom-tools': 'custom-tool',
'permission-groups': 'permission-group',
files: 'file',
knowledge: 'kb',
logs: 'log',
'mcp-servers': 'mcp-server',
sandboxes: 'sandbox',
secrets: 'secret',
skills: 'skill',
tables: 'table',
workflows: 'workflow',
workspaces: 'workspace',
}
/**
* States the workspace-key restriction the way every generated command states it.
*
* The suffix lives here, once, because a fully hand-written command renders its
* own `.description()` and never reaches the generated path — three commands
* (`secrets set`, `credentials create`, `credentials connect`/`reconnect`) sat
* beside siblings that carried the warning and silently read as accepting a
* workspace key. Taking the `OperationSpec` rather than a boolean means a
* caller has to name the operation it actually calls, so the two cannot drift.
*/
export function describeOperation(operationSpec: OperationSpec, described: string): string {
return operationSpec.workspaceKeyUnsupported
? `${described} (OAuth login or personal API key required)`
: described
}
function argumentSyntax(command: Command): string {
return command.registeredArguments
.map((argument) => {
const name = `${argument.name()}${argument.variadic ? '...' : ''}`
return argument.required ? `<${name}>` : `[${name}]`
})
.join(' ')
}
function commandPath(command: Command): string {
const names: string[] = []
let current: Command | null = command
while (current) {
names.unshift(current.name())
current = current.parent
}
return names.join(' ')
}
const UNKNOWN_OPTION_TOKEN = /^error: unknown option '(.+?)'/
/**
* Whether an unknown-option token reads as a resource id rather than a flag.
*
* `generateShortId` draws from a 64-character alphabet holding exactly one
* `-`, so one id in 64 opens with a dash and commander parses it as an option
* instead of the positional it was typed as — `audit-logs get` and
* `custom-tools get/update/delete` all take such an id. A flag on this surface
* is either a single-character short (`-w`) or a lowercase kebab-case long
* (`--dry-run`), so a lone dash followed by two or more characters of which at
* least one is an uppercase letter or a digit is not a flag any caller meant
* to type. `--organisation` and every other misspelt flag keeps the plain
* error and commander's own suggestion.
*/
function looksLikeAnId(token: string): boolean {
return (
token.length > 2 &&
!token.startsWith('--') &&
/^-[A-Za-z0-9_-]*[A-Z0-9][A-Za-z0-9_-]*$/.test(token)
)
}
/**
* Appends a worked example to the parse errors a positional argument causes,
* and the command's own flags to an unknown option.
*
* Covers the argument being absent and the argument being swallowed as an
* option because its id opens with a dash; the second needs the `--` escape,
* which commander never mentions. A guessed flag gets the real list: commander
* suggests only a near spelling, so `--row-ids` for `--row` got a bare error
* and a caller had to look the command up before retrying.
*/
function addParseErrorGuidance(command: Command): Command {
const outputError = command.configureOutput().outputError
if (!outputError) throw new Error('Commander output formatter is not configured')
command.configureOutput({
outputError: (message, write) => {
outputError(message, write)
if (message.startsWith('error: missing required argument ')) {
const syntax = argumentSyntax(command)
const example = syntax ? `${commandPath(command)} ${syntax}` : commandPath(command)
write(`Example: ${example}\n`)
return
}
const token = UNKNOWN_OPTION_TOKEN.exec(message)?.[1]
if (!token) return
if (looksLikeAnId(token) && command.registeredArguments.length > 0) {
write(`Example: ${commandPath(command)} -- ${token}\n`)
return
}
const flags = command.options.filter((option) => !option.hidden).map((option) => option.flags)
write(
flags.length > 0
? `Options for ${commandPath(command)}: ${flags.join(', ')}\n`
: `${commandPath(command)} takes no options.\n`
)
},
})
return command
}
/**
* Refuses a leaf option the root program would swallow.
*
* A collision is invisible at runtime — either the root's handler answers and
* exits `0`, or the root simply keeps the value and the leaf reads `undefined`.
* Either way the command reports success while doing nothing the caller asked
* for. Raising here means the CLI cannot start with one, which the command-tree
* tests exercise on every operation, so a contract that introduces one fails in
* CI rather than in somebody's pipeline.
*/
function assertNoReservedFlags(command: Command, operation: V2OperationName): void {
for (const option of command.options) {
for (const flag of [option.long, option.short]) {
if (flag && RESERVED_PROGRAM_FLAGS.has(flag)) {
throw new Error(
`${operation} declares ${flag}, which the root program already owns; give the flag another name`
)
}
}
}
}
/**
* Commands allowed to re-declare a flag the root owns, by full command path.
*
* `profiles add` declares `-w, --workspace` so that its own `--help` names the
* workspace it takes. Its action never reads the leaf option: it reads
* `globalsOf(command).workspace`, which is the root's value — the very value
* the root swallowed. The declaration is help text, so the collision is
* deliberate and inert.
*/
const RESERVED_FLAG_EXEMPTIONS: ReadonlySet<string> = new Set(['profiles add'])
/**
* Sweeps the assembled program for a flag the root already owns.
*
* `assertNoReservedFlags` runs while a generated leaf is configured, so it sees
* nothing that is attached by hand (`attachSecretCommands`,
* `attachProtocolCommands`, `attachCredentialCommands`) or added to a leaf
* after it is built. Walking the finished tree is what covers those.
*/
export function assertNoReservedProgramFlags(program: Command): void {
const walk = (command: Command, prefix: string[]): void => {
const path = [...prefix, command.name()]
const name = path.join(' ')
if (!RESERVED_FLAG_EXEMPTIONS.has(name)) {
for (const option of command.options) {
for (const flag of [option.long, option.short]) {
if (flag && RESERVED_PROGRAM_FLAGS.has(flag)) {
throw new Error(
`"sim ${name}" declares ${flag}, which the root program already owns; give the flag another name`
)
}
}
}
}
for (const child of command.commands) walk(child, path)
}
for (const child of program.commands) walk(child, [])
}
/**
* Refuses `--help` typed after a command that does not exist.
*
* Commander answers a help flag before it looks at the operands, so `sim
* workspaces zzzz --help` printed the group's help and exited `0` while the
* same words without the flag exit `1`. A capability probe reading the exit
* code therefore concluded a command exists when it does not.
*
* Only pure dispatchers are guarded. A command that takes arguments or acts on
* its own (`sim files restore <fileId>`, `sim profiles`) legitimately sees an
* operand it did not register as a subcommand, and refusing there would break
* `--help` on argv the CLI accepts.
*/
export function refuseHelpAfterUnknownCommand(program: Command): void {
const walk = (command: Command): void => {
// Neither the action handler nor `unknownCommand` is in commander's
// typings, for the same reason `rawArgs` is not — reaching for them is what
// keeps this message, its "did you mean" suggestion, and its error code
// identical to the non-help path.
const internals = command as Command & {
_actionHandler?: unknown
unknownCommand: () => never
}
const dispatchesOnly =
command.commands.length > 0 &&
!internals._actionHandler &&
command.registeredArguments.length === 0
if (dispatchesOnly) {
const known = new Set<string>(['help'])
for (const child of command.commands) {
known.add(child.name())
for (const alias of child.aliases()) known.add(alias)
}
// `beforeHelp` fires inside `outputHelp()`, before a byte is written, and
// by then commander has already assigned the parsed operands.
command.on('beforeHelp', () => {
const first = command.args[0]
if (first === undefined || first.startsWith('-') || known.has(first)) return
internals.unknownCommand()
})
}
for (const child of command.commands) walk(child)
}
walk(program)
}
function configureOperation(
command: Command,
operation: V2OperationName,
spec: CommandSpec
): Command {
const operationSpec = V2_OPERATIONS[operation] as OperationSpec
// A workspace mutation is followed by reading its operation receipt under `--wait`.
callsOperations(
command,
spec.workspaceOperation ? [operation, 'getWorkspaceOperation'] : [operation],
{ runs: operation }
)
command.allowExcessArguments(false)
for (const alias of spec.aliases ?? []) command.alias(alias)
for (const param of Object.keys(spec.pathFlags ?? {})) {
if (!operationSpec.pathParams.includes(param)) {
throw new Error(`${operation}.${param} is not a path parameter`)
}
}
for (const param of Object.keys(spec.pathArgumentNames ?? {})) {
if (!operationSpec.pathParams.includes(param)) {
throw new Error(`${operation}.${param} is not a path parameter`)
}
if (spec.pathFlags?.[param]) {
throw new Error(`${operation}.${param} cannot be both a path argument and a path flag`)
}
}
if (spec.profileWorkspacePath) {
if (!operationSpec.pathParams.includes(PROFILE_INJECTED_FIELD)) {
throw new Error(`${operation}.profileWorkspacePath requires a workspaceId path parameter`)
}
if (spec.pathFlags?.[PROFILE_INJECTED_FIELD]) {
throw new Error(`${operation}.workspaceId cannot be both profile-injected and a path flag`)
}
}
for (const param of operationSpec.pathParams) {
if (spec.pathFlags?.[param] || isProfileWorkspacePath(spec, param)) continue
command.argument(
`<${spec.pathArgumentNames?.[param] ?? param}>`,
operationSpec.pathParamDocs?.[param]
)
}
if (spec.allWorkspaces) {
const workspace = operationSpec.query?.workspaceId ?? operationSpec.body?.workspaceId
if (!workspace || workspace.required) {
throw new Error(`${operation}.allWorkspaces requires an optional workspaceId field`)
}
}
for (const field of spec.positionals ?? []) {
const descriptor = operationSpec.query?.[field] ?? operationSpec.body?.[field]
if (!descriptor) throw new Error(`${operation}.${field} is not a request field`)
if (spec.requestFields && !spec.requestFields.includes(field)) {
throw new Error(`${operation}.${field} is positional but not exposed`)
}
// A field promoted to a positional keeps the prose it would have carried as
// a flag; the promotion changes where the value is typed, not what it means.
command.argument(
`<${flagNameFor(operation, field)}>`,
flagSpecFor(operation, field).describe ?? descriptor.describe
)
}
const trailing = trailingListField(spec)
if (trailing) {
const trailingFlags = Object.values(spec.flags ?? {}).filter((flag) => flag.trailing)
const descriptor = operationSpec.query?.[trailing] ?? operationSpec.body?.[trailing]
if (trailingFlags.length > 1)
throw new Error(`${operation} declares more than one trailing list`)
if (!descriptor || !flagSpecFor(operation, trailing).list) {
throw new Error(`${operation}.${trailing} is trailing but not a list request field`)
}
const name = flagNameFor(operation, trailing)
command.argument(
`[${name}...]`,
`${flagSpecFor(operation, trailing).describe ?? descriptor.describe} Same as --${name}.`
)
}
if (spec.requestFields) {
for (const field of spec.requestFields) {
if (
!operationSpec.query?.[field] &&
!operationSpec.body?.[field] &&
!operationSpec.headers?.[field]
) {
throw new Error(`${operation}.${field} is not a request field`)
}
}
for (const slot of ['query', 'body', 'headers'] as const) {
for (const [field, descriptor] of Object.entries(operationSpec[slot] ?? {})) {
if (
descriptor.required &&
field !== PROFILE_INJECTED_FIELD &&
!spec.requestFields.includes(field)
) {
throw new Error(`${operation}.${field} is required but not exposed`)
}
}
}
}
// Appended after the whole fallback chain, not inside the summary branch: a
// command with a hand-written `describe` needs the restriction stated just as
// much as one falling back to the spec summary.
command.description(
describeOperation(
operationSpec,
spec.describe ?? operationSpec.summary ?? `${operationSpec.method} ${operationSpec.path}`
)
)
addOperationOptions(command, operation, spec, operationSpec)
assertNoReservedFlags(command, operation)
// The last frame that still knows which operation ran, and so the only one
// that can say `--include-job-runs` where the server said `includeJobRuns`.
command.action((...invocation: unknown[]) =>
executeOperation(operation, spec, operationSpec, invocation).catch((error) => {
throw retypeApiError(error, operation, spec, operationSpec)
})
)
return command
}
function buildLeaf(operation: V2OperationName, spec: CommandSpec, leafName: string): Command {
return addParseErrorGuidance(configureOperation(new Command(leafName), operation, spec))
}
/**
* Registers a command at a path it used to have, hidden and warning on use.
*
* The leaf is built by the same `buildLeaf` the current spelling uses, so a
* renamed command cannot drift from the one it forwards to — there is one
* definition and two ways to reach it.
*
* Commander resolves a subcommand before it fills a positional, so a rename
* that turned a group into a leaf (`files restore create` into `files restore`)
* still parses: `create` is matched as the hidden subcommand rather than being
* read as the file id. The cost is that a resource whose id is literally
* `create` cannot be addressed through the current spelling, which no id
* generated by `@sim/utils/id` ever is.
*/
function addRenamedCommand(
groups: Map<string, Command>,
operation: V2OperationName,
spec: CommandSpec,
from: string,
to: string
): void {
const segments = from.split(' ')
const [groupName, ...rest] = segments
if (rest.length === 0) throw new Error(`${operation}.renamedFrom "${from}" must include a verb`)
let parent = groupFor(groups, groupName)
for (const segment of rest.slice(0, -1)) {
parent = nestedGroup(parent, segment, { hidden: true })
}
const leaf = buildLeaf(operation, spec, rest[rest.length - 1])
leaf.hook('preAction', () => warnRenamedCommand(from, to))
addSubcommand(parent, leaf, { hidden: true })
}
/**
* Attaches a subcommand without letting it rewrite the parent's usage line.
*
* Commander derives that line from `commands.length` alone, so hanging the
* retired `files restore create` under the live `files restore` leaf made its
* help read `sim files restore [options] [command] <fileId>` — a subcommand slot
* with nothing visible to put in it, on the only leaf of the whole surface that
* advertised one. Pinning the line the leaf already had leaves the retired
* spelling working, warning, and out of the help.
*/
function addSubcommand(parent: Command, child: Command, options: { hidden?: boolean } = {}): void {
const wasLeaf = parent.commands.length === 0 && parent.registeredArguments.length > 0
const usage = wasLeaf ? parent.usage() : null
parent.addCommand(child, { hidden: options.hidden })
if (usage !== null) parent.usage(usage)
}
function groupFor(groups: Map<string, Command>, name: string): Command {
const existing = groups.get(name)
if (existing) return existing
const group = new Command(name).description(`Manage ${name.replaceAll('-', ' ')}`)
const alias = GROUP_ALIASES[name]
if (alias) group.alias(alias)
groups.set(name, group)
return group
}
function resourceLabel(name: string): string {
const label = name.endsWith('s') ? name.slice(0, -1) : name
return label.replaceAll('-', ' ')
}
/**
* `hidden` applies only when this call is what creates the group. A rename that
* reaches through a group the current surface also uses (`tables rows`) must
* leave it in help; only a group resurrected solely to host a renamed leaf
* (`tables count`) stays hidden.
*/
function nestedGroup(parent: Command, name: string, options: { hidden?: boolean } = {}): Command {
const existing = parent.commands.find((candidate) => candidate.name() === name)
if (existing) return existing
const created = new Command(name).description(
`Manage ${resourceLabel(parent.name())} ${name.replaceAll('-', ' ')}`
)
addSubcommand(parent, created, { hidden: options.hidden })
return created
}
function addLeafCommand(
groups: Map<string, Command>,
operation: V2OperationName,
spec: CommandSpec,
segments: string[]
): void {
const [groupName, ...rest] = segments
if (rest.length === 0) throw new Error(`${operation} leaf command must include a verb`)
const group = groupFor(groups, groupName)
if (rest.length > 1) {
let parent = group
for (const segment of rest.slice(0, -1)) {
parent = nestedGroup(parent, segment)
}
parent.addCommand(buildLeaf(operation, spec, rest[rest.length - 1]), {
isDefault: spec.defaultSubcommand === true,
})
return
}
group.addCommand(buildLeaf(operation, spec, rest[0]), {
isDefault: spec.defaultSubcommand === true,
})
}
function variantCommandSpec(spec: CommandSpec, variant: CommandVariantSpec): CommandSpec {
return {
...spec,
command: variant.command,
groupDefault: false,
defaultSubcommand: undefined,
aliases: [],
positionals: variant.positionals,
requestFields: variant.requestFields,
variants: [],
describe: variant.describe ?? spec.describe,
}
}
/** Builds every JSON command described by the generated operation table. */
export function buildGeneratedCommands(): Command[] {
const groups = new Map<string, Command>()
const renamed: Array<{
operation: V2OperationName
spec: CommandSpec
from: string
to: string
}> = []
for (const operation of Object.keys(V2_OPERATIONS) as V2OperationName[]) {
const spec = CLI_CONTRACT[operation] ?? {}
const operationSpec = V2_OPERATIONS[operation] as OperationSpec
if (spec.hidden || operationSpec.responseMode !== 'json') continue
const segments = spec.command ? spec.command.split(' ') : deriveCommandPath(operation)
if (spec.groupDefault) {
const [groupName, ...rest] = segments
const group = groupFor(groups, groupName)
if (rest.length > 0) throw new Error(`${operation} groupDefault must name a command group`)
const pathPositionals = operationSpec.pathParams.filter(
(param) => !spec.pathFlags?.[param] && !isProfileWorkspacePath(spec, param)
)
if (pathPositionals.length > 0 || spec.positionals?.length) {
throw new Error(`${operation} groupDefault cannot require positional arguments`)
}
configureOperation(group, operation, spec)
} else {
addLeafCommand(groups, operation, spec, segments)
}
for (const variant of spec.variants ?? []) {
addLeafCommand(
groups,
operation,
variantCommandSpec(spec, variant),
variant.command.split(' ')
)
}
for (const from of spec.renamedFrom ?? []) {
renamed.push({ operation, spec, from, to: segments.join(' ') })
}
}
// Second pass on purpose. Commander resolves a duplicate name to whichever
// was registered first, so registering every current spelling before any
// renamed one makes it impossible for a retired path to shadow a live command
// that happens to reuse its name.
for (const { operation, spec, from, to } of renamed) {
addRenamedCommand(groups, operation, spec, from, to)
}
return [...groups.values()].sort((a, b) => a.name().localeCompare(b.name()))
}