-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrecall-xray-cli.ts
More file actions
100 lines (95 loc) · 3.31 KB
/
Copy pathrecall-xray-cli.ts
File metadata and controls
100 lines (95 loc) · 3.31 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
/**
* Input-validation helpers for the `remnic xray` CLI command (issue
* #570, PR 3).
*
* Pulled out of `cli.ts` so the validation paths can be unit-tested in
* isolation — the full CLI handler is hard to exercise without booting
* an orchestrator. CLAUDE.md rules 14 + 51 require that `--format`,
* `--budget`, `--namespace`, and `--out` reject missing-value /
* unknown / non-positive arguments with a listed-options error, rather
* than silently defaulting.
*/
import {
parseXrayFormat,
type RecallXrayFormat,
} from "./recall-xray-renderer.js";
import {
isRecallDisclosure,
RECALL_DISCLOSURE_LEVELS,
type RecallDisclosure,
} from "./types.js";
export interface ParsedXrayCliOptions {
format: RecallXrayFormat;
/** Positive integer override, or undefined when not specified. */
budget?: number;
/** Trimmed namespace, or undefined when not specified. */
namespace?: string;
/** Trimmed, tilde-unexpanded output path, or undefined when stdout. */
outPath?: string;
/** Disclosure depth for X-ray telemetry, or undefined when not specified. */
disclosure?: RecallDisclosure;
}
/**
* Validate and coerce `--disclosure <level>`. Must be one of the
* canonical levels; throws a listed-options error otherwise.
*/
export function parseXrayDisclosureFlag(value: unknown): RecallDisclosure | undefined {
if (value === undefined || value === null) return undefined;
if (typeof value !== "string" || !isRecallDisclosure(value)) {
throw new Error(
`--disclosure expects one of ${RECALL_DISCLOSURE_LEVELS.join(", ")}; got ${JSON.stringify(value)}`,
);
}
return value;
}
/**
* Validate and coerce `--budget <chars>`. Must be a positive integer;
* throws a listed-options error otherwise.
*/
export function parseXrayBudgetFlag(value: unknown): number | undefined {
if (value === undefined || value === null) return undefined;
const parsed = typeof value === "number" ? value : Number(value);
if (
!Number.isFinite(parsed) ||
parsed <= 0 ||
!Number.isInteger(parsed)
) {
throw new Error(
`--budget expects a positive integer; got ${JSON.stringify(value)}`,
);
}
return parsed;
}
/**
* Parse and validate the full option bag for `remnic xray`. Extracted
* so the CLI handler in `cli.ts` can stay thin and the validation can
* be unit-tested without booting an orchestrator.
*/
export function parseXrayCliOptions(
rawQuery: unknown,
options: Record<string, unknown>,
): { query: string } & ParsedXrayCliOptions {
if (typeof rawQuery !== "string" || rawQuery.trim().length === 0) {
throw new Error("xray: <query> is required and must be non-empty");
}
const format = parseXrayFormat(options.format);
const budget = parseXrayBudgetFlag(options.budget);
const namespace =
typeof options.namespace === "string" &&
options.namespace.trim().length > 0
? options.namespace.trim()
: undefined;
const outPath =
typeof options.out === "string" && options.out.trim().length > 0
? options.out.trim()
: undefined;
const disclosure = parseXrayDisclosureFlag(options.disclosure);
return {
query: rawQuery,
format,
...(budget !== undefined ? { budget } : {}),
...(namespace !== undefined ? { namespace } : {}),
...(outPath !== undefined ? { outPath } : {}),
...(disclosure !== undefined ? { disclosure } : {}),
};
}