import { printJson, printTable } from "../printer.js"; import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js"; import { createSessionClient } from "../../core/http/session-client.js"; import { getPermissions } from "../../domains/permission/tools.js"; export async function handlePermissionCommand(parsed: CliParsedArgs, runtime: CliRuntime): Promise { const corp = runtime.selectedCorp; if (!corp) throw new Error("未指定或未找到有效企业。请通过 --corp= 指定企业,或运行 hose-use auth login 登录。"); const action = parsed.action?.toLowerCase() || "get"; if (!["get", "list", "show"].includes(action)) throw new Error(`未知 permission 子命令: ${action}。支持: get`); const client = await createSessionClient(corp); const report = await getPermissions(client, corp.corpId || corp.id); if (runtime.jsonMode) return { stdout: printJson({ corpId: corp.corpId || corp.id, corpName: corp.name, ...report }, runtime.outputOptions), stderr: "", exitCode: 0 }; const rows = report.permissions.map((item) => [item.name, item.auth ? "启用" : "停用"]); return { stdout: `====== 角色权限快照 (权限模型: ${report.type || "-"}) ======\n${rows.length ? printTable(["权限", "状态"], rows) : "暂无权限项"}\n`, stderr: "", exitCode: 0 }; }