Files
seal-use/src/cli/parser.ts
T

197 lines
6.2 KiB
TypeScript

import type { WebhookMode } from "../domains/seal/types.js";
import type { ParsedArgs } from "./types.js";
export function parseArgs(argv: string[]): ParsedArgs {
const command: string[] = [];
const options: Record<string, string | boolean> = {};
let json: unknown;
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (!arg) continue;
if (arg === "--json") {
const raw = argv[index + 1];
if (!raw) throw new Error("--json requires a JSON object argument");
json = JSON.parse(raw);
index += 1;
continue;
}
if (arg.startsWith("-")) {
const key = arg.replace(/^-+/, "");
const next = argv[index + 1];
if (!next || next.startsWith("-")) {
options[key] = true;
} else {
options[key] = next;
index += 1;
}
continue;
}
command.push(arg);
}
return { command, options, json };
}
export function jsonObject(value: unknown): Record<string, unknown> {
if (value === undefined) return {};
if (!value || typeof value !== "object" || Array.isArray(value)) {
throw new Error("--json must be an object");
}
return value as Record<string, unknown>;
}
export function stringOption(value: string | boolean | undefined): string | undefined {
return typeof value === "string" ? value : undefined;
}
export function numberOption(value: string | boolean | undefined): number | undefined {
if (typeof value !== "string") return undefined;
const number = Number(value);
if (!Number.isFinite(number)) throw new Error(`Invalid number: ${value}`);
return number;
}
export function splitOption(value: string | boolean | undefined): string[] | undefined {
if (typeof value !== "string") return undefined;
const items = value.split(",").map((item) => item.trim()).filter(Boolean);
return items.length ? items : undefined;
}
export function booleanOption(value: string | boolean | undefined): boolean | undefined {
if (value === undefined) return undefined;
if (typeof value === "boolean") return value;
if (value === "true") return true;
if (value === "false") return false;
throw new Error(`Invalid boolean: ${value}`);
}
export function requiredString(input: Record<string, unknown>, key: string): string {
const value = optionalString(input, key);
if (!value) throw new Error(`Missing required JSON field: ${key}`);
return value;
}
export function requiredStringAlias(input: Record<string, unknown>, keys: string[]): string {
for (const key of keys) {
const value = optionalString(input, key);
if (value) return value;
}
throw new Error(`Missing required JSON field: ${keys.join(" or ")}`);
}
export function optionalString(input: Record<string, unknown>, key: string): string | undefined {
const value = input[key];
return typeof value === "string" && value.trim() ? value.trim() : undefined;
}
export function optionalNumber(input: Record<string, unknown>, key: string): number | undefined {
const value = input[key];
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
}
export function inferCorpIdFromStaffId(staffId: string): string | undefined {
const [corpId] = staffId.split(":", 1);
return corpId || undefined;
}
export function safeFileName(value: string): string {
return value.replace(/[^a-zA-Z0-9._-]/g, "_");
}
export function parseStateMode(value: string): boolean {
const normalized = value.trim().toLowerCase();
if (["stateful", "state", "有状态"].includes(normalized)) return true;
if (["stateless", "无状态"].includes(normalized)) return false;
throw new Error("Invalid config mode. Use stateful or stateless.");
}
export function parseRuleVersionSelector(value: string) {
if (value === "latest") return { latest: true };
const versionNumber = Number(value);
if (Number.isInteger(versionNumber) && versionNumber > 0) {
return { versionNumber };
}
return { versionId: value };
}
export function ruleVersionSearchOptions(value?: string) {
if (!value) return {};
const selector = parseRuleVersionSelector(value);
if ("latest" in selector) {
return {
ruleVersionScope: "version",
latestRuleVersion: true
};
}
if ("versionNumber" in selector) {
return {
ruleVersionScope: "version",
ruleVersionNumber: selector.versionNumber
};
}
return {
ruleVersionScope: "version",
ruleVersionId: selector.versionId
};
}
export function ruleVersionContextOptions(value?: string) {
if (!value) return {};
const selector = parseRuleVersionSelector(value);
if ("latest" in selector) {
return { latestRuleVersion: true };
}
if ("versionNumber" in selector) {
return { ruleVersionNumber: selector.versionNumber };
}
return { ruleVersionId: selector.versionId };
}
export function ruleVersionGetOptions(value?: string) {
if (!value) return {};
const selector = parseRuleVersionSelector(value);
if ("latest" in selector) {
return { latest: true };
}
if ("versionNumber" in selector) {
return { versionNumber: selector.versionNumber };
}
return { versionId: selector.versionId };
}
export function parseWebhookMode(value?: string): WebhookMode | undefined {
if (!value) return undefined;
const normalized = value.trim().toLowerCase().replace(/[\s-]+/g, "_");
const aliases: Record<string, WebhookMode> = {
assisted: "assisted",
辅助: "assisted",
semi_automatic: "semi_automatic",
半自动: "semi_automatic",
fully_automatic: "fully_automatic",
全自动: "fully_automatic"
};
const mode = aliases[normalized];
if (!mode) {
throw new Error("Invalid Webhook mode. Use assisted, semi_automatic, or fully_automatic");
}
return mode;
}
export function webhookRateOption(options: Record<string, string | boolean>): number | undefined {
const value = numberOption(
options.autoApprovalRate ?? options["auto-approval-rate"] ?? options["approval-rate"]
);
if (value !== undefined && (!Number.isInteger(value) || value < 0 || value > 100)) {
throw new Error("Webhook auto approval rate must be an integer between 0 and 100");
}
return value;
}
export function webhookShowSecretOption(options: Record<string, string | boolean>): boolean | undefined {
return booleanOption(options.showSecret) ?? booleanOption(options["show-secret"]);
}