import { toggleWorkflow } from "../../../domains/platform/workflow/services.js"; import { stringOption } from "../../parser.js"; import { printJson } from "../../printer.js"; import { isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js"; import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js"; import { createWorkflowDetailClient } from "./shared.js"; export 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-use workflow toggle --state=<1|0> (1启用, 0停用)\n", exitCode: 1, }; } const state = (stateArg === "1" || stateArg === "true" ? 1 : 0) as 0 | 1; requireMutationConfirmation( parsed, runtime, "切换工作流状态", `hose-use workflow toggle ${pipelineId} --state=${state} --yes` ); if (isDryRun(parsed, runtime)) { return mutationPreview(parsed, runtime, "切换工作流状态", pipelineId, { state }); } const { client } = await createWorkflowDetailClient(parsed, runtime, pipelineId); const result = await toggleWorkflow(client, pipelineId, state); const label = state === 1 ? "🟢 启用" : "⚪ 停用"; if (runtime.jsonMode) { return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: result.verified ? 0 : 1, }; } // 启停未回读确认时必须如实告知:不得把「请求已提交」呈现为「已生效」。 if (!result.verified) { return { stdout: "", stderr: `[WARNING] 工作流 ${pipelineId} 启停请求已提交,但${result.message}\n`, exitCode: 1, }; } return { stdout: `[SUCCESS] 工作流 ${pipelineId} 状态已更新为: ${label}(已回读确认)\n`, stderr: "", exitCode: 0, }; } export async function handlePublish(parsed: CliParsedArgs, runtime: CliRuntime): Promise { const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]); if (!pipelineId) return { stdout: "", stderr: "用法: hose-use workflow publish \n", exitCode: 1 }; requireMutationConfirmation( parsed, runtime, "发布并启用工作流", `hose-use workflow publish ${pipelineId} --yes` ); if (isDryRun(parsed, runtime)) { return mutationPreview(parsed, runtime, "发布并启用工作流", pipelineId, { state: 1 }); } const { client } = await createWorkflowDetailClient(parsed, runtime, pipelineId); const result = await toggleWorkflow(client, pipelineId, 1); const output = { success: result.verified, pipelineId, published: result.verified, state: 1, verified: result.verified, message: result.message }; if (runtime.jsonMode) return { stdout: printJson(output, runtime.outputOptions), stderr: "", exitCode: result.verified ? 0 : 1 }; if (!result.verified) { return { stdout: "", stderr: `❌ 工作流 [${pipelineId}] 发布状态未能确认:${result.message}\n`, exitCode: 1 }; } return { stdout: `✅ 工作流 [${pipelineId}] 已发布并启用(已回读确认)。\n`, stderr: "", exitCode: 0 }; }