import { printJson, printKeyValue, printTable } from "../printer.js"; import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js"; export async function handleCorpsCommand( parsed: CliParsedArgs, runtime: CliRuntime ): Promise { const action = parsed.action || "list"; switch (action) { case "list": return handleList(parsed, runtime); case "switch": case "use": return handleSwitch(parsed, runtime); case "current": return handleCurrent(parsed, runtime); default: return { stdout: "", stderr: `未知 corps 子命令: ${action}。支持: list, switch, current\n`, exitCode: 1, }; } } async function handleList(parsed: CliParsedArgs, runtime: CliRuntime): Promise { const corps = runtime.corpContext.listCorps(); if (runtime.jsonMode) { return { stdout: printJson({ activeCorpId: runtime.selectedCorp?.id, corporations: corps, }), stderr: "", exitCode: 0, }; } if (corps.length === 0) { return { stdout: "未找到任何企业配置,请先运行 hose auth login\n", stderr: "", exitCode: 0 }; } const rows = corps.map((c, i) => [ c.current ? `* ${i + 1}` : ` ${i + 1}`, c.id, c.name, c.staffId, c.source || "manual", c.accountId || "-", ]); const table = printTable( ["#", "企业 ID", "企业名称", "员工工号", "来源", "所属账号"], rows ); return { stdout: `====== 已配置企业列表 (${corps.length} 家) ======\n${table}\n\n提示: 使用 hose corps switch <企业ID> 切换工作企业\n`, stderr: "", exitCode: 0, }; } async function handleSwitch(parsed: CliParsedArgs, runtime: CliRuntime): Promise { const corpId = parsed.args[0]; if (!corpId) { return { stdout: "", stderr: "用法: hose corps switch <企业ID>\n", exitCode: 1 }; } const success = runtime.corpContext.switchCorp(corpId); if (!success) { return { stdout: "", stderr: `企业 ${corpId} 不存在或未配置。可运行 hose corps list 查看可用列表。\n`, exitCode: 1, }; } const current = runtime.corpContext.getCurrent(); if (runtime.jsonMode) { return { stdout: printJson(current), stderr: "", exitCode: 0 }; } return { stdout: `[SUCCESS] 已切换到企业: ${current?.name} (${current?.id})\n`, stderr: "", exitCode: 0, }; } async function handleCurrent(parsed: CliParsedArgs, runtime: CliRuntime): Promise { const current = runtime.selectedCorp; if (!current) { return { stdout: "", stderr: "未选中任何企业\n", exitCode: 1 }; } if (runtime.jsonMode) { return { stdout: printJson(current), stderr: "", exitCode: 0 }; } const output = printKeyValue({ "企业 ID": current.id, "企业名称": current.name, "员工工号": current.credentials.staffId, "来源类型": current.source || "manual", "所属账号": current.accountId || "-", "主站地址": current.baseUrl || "https://app.ekuaibao.com", }); return { stdout: `====== 当前激活企业 ======\n${output}\n`, stderr: "", exitCode: 0, }; }