feat: 自动选择已配置的 runtime 策略账号

This commit is contained in:
Codex
2026-07-16 08:07:33 +02:00
parent f612f55902
commit d36eef5370
8 changed files with 242 additions and 19 deletions
@@ -6,6 +6,9 @@ export interface RuntimeOptions {
action: RuntimeAction;
account: string | null;
accounts: string[];
selectConfiguredPolicy: boolean;
accountType: "apikey" | null;
excludeAccounts: string[];
group: string | null;
allGroups: boolean;
platform: string | null;
@@ -26,10 +29,13 @@ export interface RuntimeOptions {
export function parseRuntimeOptions(args: string[]): RuntimeOptions {
const [actionRaw = "list", ...rest] = args;
if (!(["list", "get", "errors", "infrastructure", "apply", "delete"] as string[]).includes(actionRaw)) {
throw new Error("runtime usage: list|get|errors|infrastructure|apply|delete [--account <name-or-id>|--accounts <selectors>] [--group <name-or-id>|--all-groups] [--platform <name>] [--page-token <token>] [--since 24h] [--tail 50000] [--template <id>] [--kind temp-unschedulable|priority|account-name] [--priority <0-1000>|--name <account-name>] [--confirm] [--target <id>] [--json|--full|--raw]");
throw new Error("runtime usage: list|get|errors|infrastructure|apply|delete [--account <name-or-id>|--accounts <selectors>|--select-configured-policy --account-type apikey [--exclude-accounts <selectors>]] [--group <name-or-id>|--all-groups] [--platform <name>] [--page-token <token>] [--since 24h] [--tail 50000] [--template <id>] [--kind temp-unschedulable|priority|account-name] [--priority <0-1000>|--name <account-name>] [--confirm] [--target <id>] [--json|--full|--raw]");
}
let account: string | null = null;
let accounts: string[] = [];
let selectConfiguredPolicy = false;
let accountType: "apikey" | null = null;
let excludeAccounts: string[] = [];
let group: string | null = null;
let allGroups = false;
let platform: string | null = null;
@@ -62,6 +68,11 @@ export function parseRuntimeOptions(args: string[]): RuntimeOptions {
else if (arg.startsWith("--account=")) account = arg.slice("--account=".length).trim();
else if (arg === "--accounts") accounts = parseAccountSelectors(readValue("--accounts"));
else if (arg.startsWith("--accounts=")) accounts = parseAccountSelectors(arg.slice("--accounts=".length));
else if (arg === "--select-configured-policy") selectConfiguredPolicy = true;
else if (arg === "--account-type") accountType = parseAccountType(readValue("--account-type"));
else if (arg.startsWith("--account-type=")) accountType = parseAccountType(arg.slice("--account-type=".length));
else if (arg === "--exclude-accounts") excludeAccounts = parseAccountSelectors(readValue("--exclude-accounts"), "--exclude-accounts");
else if (arg.startsWith("--exclude-accounts=")) excludeAccounts = parseAccountSelectors(arg.slice("--exclude-accounts=".length), "--exclude-accounts");
else if (arg === "--group") group = readValue("--group");
else if (arg.startsWith("--group=")) group = arg.slice("--group=".length).trim();
else if (arg === "--all-groups") allGroups = true;
@@ -87,8 +98,11 @@ export function parseRuntimeOptions(args: string[]): RuntimeOptions {
}
const action = actionRaw as RuntimeAction;
if (account !== null && accounts.length > 0) throw new Error("use only one of --account or --accounts");
if (selectConfiguredPolicy && (account !== null || accounts.length > 0)) throw new Error("--select-configured-policy cannot be combined with --account or --accounts");
if ((action === "get" || action === "infrastructure") && !account) throw new Error(`runtime ${action} requires --account <name-or-id>`);
if ((action === "apply" || action === "delete") && account === null && accounts.length === 0) throw new Error(`runtime ${action} requires --account or --accounts`);
if ((action === "apply" || action === "delete") && account === null && accounts.length === 0 && !selectConfiguredPolicy) {
throw new Error(`runtime ${action} requires --account, --accounts, or --select-configured-policy`);
}
if (accounts.length > 0 && action !== "apply" && action !== "delete") throw new Error(`runtime ${action} does not accept --accounts`);
if (account !== null && (account.length > 256 || /[\r\n]/u.test(account))) throw new Error("--account has an unsupported format");
if (group !== null && (group.length > 256 || /[\r\n]/u.test(group))) throw new Error("--group has an unsupported format");
@@ -99,6 +113,12 @@ export function parseRuntimeOptions(args: string[]): RuntimeOptions {
if ((group !== null || platform !== null) && action !== "errors" && action !== "infrastructure") throw new Error(`runtime ${action} does not accept group scope options`);
if (pageToken !== null && !allGroups) throw new Error("--page-token requires --all-groups");
if (account !== null && allGroups) throw new Error("--account cannot be combined with --all-groups; use --group first");
if (selectConfiguredPolicy && !((action === "apply" || action === "delete") && kind === "temp-unschedulable")) {
throw new Error("--select-configured-policy only supports runtime apply|delete --kind temp-unschedulable");
}
if (selectConfiguredPolicy && accountType === null) throw new Error("--select-configured-policy requires --account-type apikey");
if (!selectConfiguredPolicy && accountType !== null) throw new Error("--account-type requires --select-configured-policy");
if (!selectConfiguredPolicy && excludeAccounts.length > 0) throw new Error("--exclude-accounts requires --select-configured-policy");
if (action === "apply" && kind === "temp-unschedulable" && !template) throw new Error("runtime apply --kind temp-unschedulable requires --template <id>");
if (kind === "priority" && action !== "apply") throw new Error("runtime --kind priority only supports apply");
if (kind === "priority" && priority === null) throw new Error("runtime apply --kind priority requires --priority <0-1000>");
@@ -116,17 +136,22 @@ export function parseRuntimeOptions(args: string[]): RuntimeOptions {
if (!/^\d+[mhd]$/u.test(since)) throw new Error("--since must use <number>m, <number>h, or <number>d");
if (["list", "get", "errors", "infrastructure"].includes(action) && confirm) throw new Error(`runtime ${action} does not accept --confirm`);
if ([full, raw, json].filter(Boolean).length > 1) throw new Error("use only one of --json, --full, or --raw");
return { action, account, accounts, group, allGroups, platform, pageToken, template, kind, priority, name, confirm, full, raw, json, since, tail, targetId };
return { action, account, accounts, selectConfiguredPolicy, accountType, excludeAccounts, group, allGroups, platform, pageToken, template, kind, priority, name, confirm, full, raw, json, since, tail, targetId };
}
function parseAccountSelectors(value: string): string[] {
function parseAccountSelectors(value: string, option = "--accounts"): string[] {
const selectors = value.split(",").map((item) => item.trim()).filter(Boolean);
if (selectors.length === 0 || selectors.length > 100) throw new Error("--accounts requires 1 to 100 comma-separated selectors");
if (selectors.some((item) => item.length > 256 || /[\r\n]/u.test(item))) throw new Error("--accounts has an unsupported selector");
if (new Set(selectors).size !== selectors.length) throw new Error("--accounts contains duplicate selectors");
if (selectors.length === 0 || selectors.length > 100) throw new Error(`${option} requires 1 to 100 comma-separated selectors`);
if (selectors.some((item) => item.length > 256 || /[\r\n]/u.test(item))) throw new Error(`${option} has an unsupported selector`);
if (new Set(selectors).size !== selectors.length) throw new Error(`${option} contains duplicate selectors`);
return selectors;
}
function parseAccountType(value: string): "apikey" {
if (value !== "apikey") throw new Error("--account-type currently supports only apikey");
return value;
}
function parseRuntimeKind(value: string): "temp-unschedulable" | "priority" | "account-name" {
if (value !== "temp-unschedulable" && value !== "priority" && value !== "account-name") throw new Error("--kind must be temp-unschedulable, priority, or account-name");
return value;