Files
hose-use/src/cli/commands/workflow.ts
T

229 lines
7.8 KiB
TypeScript

import {
listAllWorkflows,
findWorkflowsByTaskType,
} from "../../domains/platform/services.js";
import * as api from "../../domains/platform/api.js";
import { stringOption, numberOption, booleanOption } from "../parser.js";
import { createClientsForCorp } from "../helpers.js";
import { printJson, printTable, printKeyValue } from "../printer.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
import { analyzeWorkflow } from "../../domains/platform/analysis.js";
export async function handleWorkflowCommand(
parsed: CliParsedArgs,
runtime: CliRuntime
): Promise<CliResult> {
const currentCorp = runtime.selectedCorp;
if (!currentCorp) {
return {
stdout: "",
stderr: "未选中企业,请先通过 hose-use corps switch 或 --corp 指定企业\n",
exitCode: 1,
};
}
const action = parsed.action || "list";
switch (action) {
case "list":
case "list-all":
case "ls":
return handleList(parsed, runtime);
case "get":
case "detail":
case "info":
return handleGet(parsed, runtime);
case "find":
case "search":
return handleFind(parsed, runtime);
case "toggle":
return handleToggle(parsed, runtime);
case "analyze":
case "analyse":
return handleAnalyze(parsed, runtime);
default:
return {
stdout: "",
stderr: `未知 workflow 子命令: ${action}。支持: list, get, find, toggle\n`,
exitCode: 1,
};
}
}
async function handleAnalyze(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]);
if (!pipelineId) return { stdout: "", stderr: "用法: hose-use workflow analyze <pipelineId>\n", exitCode: 1 };
const clients = await createClientsForCorp(runtime.selectedCorp!);
const detail = await api.getWorkflowDetail(clients.easoClient, pipelineId);
const result = analyzeWorkflow(detail);
if (runtime.jsonMode) return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
const diagnosticRows = result.diagnostics.map((item) => [item.severity, item.code, item.path, item.message]);
const dependencyRows = result.dependencies.map((item) => [item.path, item.taskType, item.references.join(", ")]);
return { stdout: [
`====== 工作流静态分析 (${detail.label || detail.id}) ======`,
printKeyValue({ "工作流 ID": detail.id, "根节点数": result.ast.length, "依赖节点数": result.dependencies.length, "诊断数": result.diagnostics.length }),
`\n依赖引用:\n${dependencyRows.length ? printTable(["节点路径", "类型", "引用"], dependencyRows) : "暂无代码引用"}`,
`\n诊断:\n${diagnosticRows.length ? printTable(["级别", "代码", "节点路径", "说明"], diagnosticRows) : "未发现明显配置问题"}`, "",
].join("\n"), stderr: "", exitCode: 0 };
}
async function handleList(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const clients = await createClientsForCorp(runtime.selectedCorp!);
const page = numberOption(parsed.options, ["page"]) || 1;
const pageSize = numberOption(parsed.options, ["pageSize", "size"]) || 50;
const result = await listAllWorkflows(clients, { page, pageSize });
if (runtime.jsonMode) {
return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
}
if (result.workflows.length === 0) {
return {
stdout: `企业 [${runtime.selectedCorp!.name}] 下暂无工作流\n`,
stderr: "",
exitCode: 0,
};
}
const rows = result.workflows.map((wf, i) => [
i + 1,
wf.id,
wf.pipelineName,
wf.platformName || wf.platformId || "-",
wf.state === 1 ? "🟢 启用" : "⚪ 停用",
wf.tasks?.length ?? "-",
]);
const table = printTable(
["#", "工作流 ID", "工作流名称", "所属自建应用", "状态", "节点数"],
rows
);
return {
stdout: `====== [${runtime.selectedCorp!.name}] 工作流列表 (共 ${result.workflows.length} 条) ======\n${table}\n`,
stderr: "",
exitCode: 0,
};
}
async function handleGet(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]);
if (!pipelineId) {
return { stdout: "", stderr: "用法: hose-use workflow get <pipelineId>\n", exitCode: 1 };
}
const clients = await createClientsForCorp(runtime.selectedCorp!);
const detail = await api.getWorkflowDetail(clients.easoClient, pipelineId);
if (runtime.jsonMode) {
return { stdout: printJson(detail, runtime.outputOptions), stderr: "", exitCode: 0 };
}
const summary = printKeyValue({
"工作流 ID": detail.id,
"工作流名称": detail.label,
"状态": detail.state === 1 ? "🟢 启用" : "⚪ 停用",
"版本号": detail.version || "-",
"节点总数": detail.tasks?.length ?? 0,
});
let taskList = "";
if (detail.tasks && detail.tasks.length > 0) {
const taskRows = detail.tasks.map((t, i) => [
i + 1,
t.name || "-",
t.label || "-",
t.type,
t.groupName || "-",
]);
taskList = `\n 节点结构:\n` + printTable(["#", "节点标识", "节点名称", "类型", "分组"], taskRows);
}
return {
stdout: `====== 工作流详情 ======\n${summary}\n${taskList}\n`,
stderr: "",
exitCode: 0,
};
}
async function handleFind(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const taskType = stringOption(parsed.options, ["task", "taskType", "type"]) || parsed.args[0];
if (!taskType) {
return {
stdout: "",
stderr: "用法: hose-use workflow find --task=<节点类型> (如 code-js2, http, condition)\n",
exitCode: 1,
};
}
const clients = await createClientsForCorp(runtime.selectedCorp!);
const includeDetail = booleanOption(parsed.options, ["includeDetail", "detail"]);
const page = numberOption(parsed.options, ["page"]) || 1;
const pageSize = numberOption(parsed.options, ["pageSize", "size"]) || 50;
const result = await findWorkflowsByTaskType(clients, taskType, {
page,
pageSize,
includeDetail,
});
if (runtime.jsonMode) {
return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
}
if (result.workflows.length === 0) {
return {
stdout: `未找到包含节点类型 [${taskType}] 的工作流\n`,
stderr: "",
exitCode: 0,
};
}
const rows = result.workflows.map((wf, i) => [
i + 1,
wf.id,
wf.pipelineName,
wf.platformName || wf.platformId || "-",
wf.state === 1 ? "🟢 启用" : "⚪ 停用",
]);
const table = printTable(
["#", "工作流 ID", "工作流名称", "所属自建应用", "状态"],
rows
);
return {
stdout: `====== 匹配节点类型 [${taskType}] 的工作流 (共 ${result.workflows.length} 条) ======\n${table}\n`,
stderr: "",
exitCode: 0,
};
}
async function handleToggle(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]);
const stateArg = stringOption(parsed.options, ["state", "s"]);
if (!pipelineId || stateArg === undefined) {
return {
stdout: "",
stderr: "用法: hose-use workflow toggle <pipelineId> --state=<1|0> (1启用, 0停用)\n",
exitCode: 1,
};
}
const state = (stateArg === "1" || stateArg === "true" ? 1 : 0) as 0 | 1;
const clients = await createClientsForCorp(runtime.selectedCorp!);
const result = await api.toggleWorkflow(clients.easoClient, pipelineId, state);
if (runtime.jsonMode) {
return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
}
return {
stdout: `[SUCCESS] 工作流 ${pipelineId} 状态已更新为: ${state === 1 ? "🟢 启用" : "⚪ 停用"}\n`,
stderr: "",
exitCode: 0,
};
}