fix: harden CLI parsing, MCP startup and release integrity
This commit is contained in:
1 parent
f0902f5f20
commit
103ead0fe5
53 files changed
+3713
-1945
No files matched your search
+29
-25
@@ -1,6 +1,7 @@
|
||||
import { VERSION_INFO, formatVersionLabel } from "../version.js";
|
||||
import { parseArgs, stringOption, booleanOption, outputOptions } from "./parser.js";
|
||||
import { parseArgs, stringOption, booleanOption, outputOptions, isJsonRequested } from "./parser.js";
|
||||
import { printHelp, printJson, redactSensitiveText } from "./printer.js";
|
||||
import { resolveCommandMetadata } from "./command-metadata.js";
|
||||
import { classifyCliError } from "./errors.js";
|
||||
import { initCorpRuntime } from "./helpers.js";
|
||||
import { findCommandByArea, isStandingCommand } from "./registry.js";
|
||||
@@ -29,7 +30,7 @@ async function runCliOnce(
|
||||
): Promise<CliResult> {
|
||||
const stdoutChunks: string[] = [];
|
||||
const stderrChunks: string[] = [];
|
||||
const machineJson = booleanOption(parseArgs(rawArgs).options, ["json", "j"]);
|
||||
const machineJson = isJsonRequested(rawArgs);
|
||||
const originalLog = console.log;
|
||||
const originalError = console.error;
|
||||
|
||||
@@ -62,7 +63,7 @@ async function runCliOnce(
|
||||
|
||||
function isHelpRequested(parsed: CliParsedArgs): boolean {
|
||||
const normArea = (parsed.area || "").replace(/^-+/, "").toLowerCase();
|
||||
if (normArea === "help" || normArea === "h" || normArea === "?") return true;
|
||||
if (normArea === "help" || normArea === "h" || normArea === "?" || parsed.action === "help") return true;
|
||||
|
||||
for (const [key, val] of Object.entries(parsed.options)) {
|
||||
const normKey = key.replace(/^-+/, "").toLowerCase();
|
||||
@@ -79,6 +80,9 @@ function isVersionRequested(parsed: CliParsedArgs): boolean {
|
||||
|
||||
for (const [key, val] of Object.entries(parsed.options)) {
|
||||
const normKey = key.replace(/^-+/, "").toLowerCase();
|
||||
const localFlags = resolveCommandMetadata(parsed.area)?.metadata.flags;
|
||||
if (key === "v" && localFlags?.v) continue;
|
||||
if (key === "version" && localFlags?.version === "value" && typeof val !== "boolean") continue;
|
||||
if (normKey === "version" || normKey === "v") {
|
||||
if (val === true || val === "true" || val === "") return true;
|
||||
}
|
||||
@@ -90,10 +94,28 @@ function resolveHelpTarget(parsed: CliParsedArgs): string[] {
|
||||
if (["help", "h", "?"].includes((parsed.area || "").toLowerCase())) {
|
||||
return [parsed.action, parsed.args[0]].filter((value): value is string => Boolean(value));
|
||||
}
|
||||
return [parsed.area, parsed.action].filter((value): value is string => Boolean(value));
|
||||
return [parsed.area, parsed.action === "help" ? parsed.args[0] : parsed.action].filter((value): value is string => Boolean(value));
|
||||
}
|
||||
|
||||
async function dispatchCli(
|
||||
async function dispatchCli(rawArgs: string[], options: CliRunOptions = {}): Promise<CliResult> {
|
||||
try {
|
||||
return await dispatchParsedCli(rawArgs, options);
|
||||
} catch (error) {
|
||||
return cliErrorResult(error, isJsonRequested(rawArgs));
|
||||
}
|
||||
}
|
||||
|
||||
export function cliErrorResult(error: unknown, jsonMode: boolean): CliResult {
|
||||
const message = redactSensitiveText(error instanceof Error ? error.message : String(error));
|
||||
const classification = classifyCliError(error);
|
||||
return {
|
||||
stdout: jsonMode ? JSON.stringify({ error: true, message, exitCode: classification.exitCode, ...(classification.nextAction ? { nextAction: classification.nextAction } : {}) }, null, 2) : "",
|
||||
stderr: jsonMode ? "" : `[ERROR] ${message}${classification.nextAction ? `\n下一步: ${classification.nextAction}` : ""}\n`,
|
||||
exitCode: classification.exitCode,
|
||||
};
|
||||
}
|
||||
|
||||
async function dispatchParsedCli(
|
||||
rawArgs: string[],
|
||||
options: CliRunOptions = {}
|
||||
): Promise<CliResult> {
|
||||
@@ -117,7 +139,7 @@ async function dispatchCli(
|
||||
};
|
||||
}
|
||||
|
||||
const corpIdOption = stringOption(parsed.options, ["corp", "c"]);
|
||||
const corpIdOption = stringOption(parsed.options, ["corp"]);
|
||||
const statelessOption = booleanOption(parsed.options, ["stateless"]);
|
||||
const statefulOption = booleanOption(parsed.options, ["stateful"]);
|
||||
const outputOpts = outputOptions(parsed.options);
|
||||
@@ -152,24 +174,6 @@ async function dispatchCli(
|
||||
}
|
||||
return await commandDef.handler(parsed, runtime, options);
|
||||
} catch (error) {
|
||||
const errorMsg = redactSensitiveText(error instanceof Error ? error.message : String(error));
|
||||
const classification = classifyCliError(error);
|
||||
if (jsonMode) {
|
||||
return {
|
||||
stdout: JSON.stringify({
|
||||
error: true,
|
||||
message: errorMsg,
|
||||
exitCode: classification.exitCode,
|
||||
...(classification.nextAction ? { nextAction: classification.nextAction } : {}),
|
||||
}, null, 2),
|
||||
stderr: "",
|
||||
exitCode: classification.exitCode,
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[ERROR] ${errorMsg}${classification.nextAction ? `\n下一步: ${classification.nextAction}` : ""}\n`,
|
||||
exitCode: classification.exitCode,
|
||||
};
|
||||
return cliErrorResult(error, jsonMode);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user