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"; export async function handleWorkflowCommand( parsed: CliParsedArgs, runtime: CliRuntime ): Promise { const currentCorp = runtime.selectedCorp; if (!currentCorp) { return { stdout: "", stderr: "未选中企业,请先通过 hose 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); default: return { stdout: "", stderr: `未知 workflow 子命令: ${action}。支持: list, get, find, toggle\n`, exitCode: 1, }; } } async function handleList(parsed: CliParsedArgs, runtime: CliRuntime): Promise { 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 { const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]); if (!pipelineId) { return { stdout: "", stderr: "用法: hose workflow get \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 { const taskType = stringOption(parsed.options, ["task", "taskType", "type"]) || parsed.args[0]; if (!taskType) { return { stdout: "", stderr: "用法: hose 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 { 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 workflow toggle --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, }; }