Files
hose-use/src/cli/commands/corps/probe.ts
T

77 lines
3.0 KiB
TypeScript

import { fetchHoseRoleDefGroups, listHoseStaffsByOpenApi, probeHoseDomain } from "../../../core/auth/login.js";
import { stringOption } from "../../parser.js";
import { printJson, printKeyValue, printTable } from "../../printer.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
export async function handleProbeHose(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const appKey = stringOption(parsed.options, ["app-key", "appKey"]);
const appSecurity = stringOption(parsed.options, ["app-security", "appSecurity", "secret"]);
const domain = stringOption(parsed.options, ["domain", "base-url"]);
if (!appKey || !appSecurity) {
return {
stdout: "",
stderr: "用法: hose-use corps probe-hose --app-key=<key> --app-security=<secret> [--domain=<url>]\n",
exitCode: 1,
};
}
const probe = await probeHoseDomain(appKey, appSecurity, domain);
const roleGroups = await fetchHoseRoleDefGroups(probe.domain, probe.openapiToken).catch(() => []);
const payload = {
domain: probe.domain,
corpId: probe.corpId,
name: probe.corpName || probe.corpId,
roleCount: roleGroups.reduce((acc, g) => acc + g.roleDefs.length, 0),
roleGroups,
};
if (runtime.jsonMode) {
return { stdout: printJson(payload, runtime.outputOptions), stderr: "", exitCode: 0 };
}
return {
stdout: `====== OpenAPI 域名与企业探测结果 ======\n${printKeyValue({
"企业 ID": payload.corpId,
"企业名称": payload.name,
"服务节点": payload.domain,
"角色数量": payload.roleCount,
})}\n`,
stderr: "",
exitCode: 0,
};
}
export async function handleHoseStaffs(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
let appKey = stringOption(parsed.options, ["app-key", "appKey"]);
let appSecurity = stringOption(parsed.options, ["app-security", "appSecurity", "secret"]);
let domain = stringOption(parsed.options, ["domain", "base-url"]);
const role = stringOption(parsed.options, ["role", "roleDefId"]);
if ((!appKey || !appSecurity) && runtime.selectedCorp?.credentials?.appKey && runtime.selectedCorp?.credentials?.appSecurity) {
appKey = runtime.selectedCorp.credentials.appKey;
appSecurity = runtime.selectedCorp.credentials.appSecurity;
domain = domain || runtime.selectedCorp.baseUrl;
}
if (!appKey || !appSecurity) {
return {
stdout: "",
stderr: "用法: hose-use corps hose-staffs [--corp=<id>] [--app-key=<key> --app-security=<secret>] [--role=admin]\n",
exitCode: 1,
};
}
const res = await listHoseStaffsByOpenApi({ appKey, appSecurity, domain, role });
if (runtime.jsonMode) {
return { stdout: printJson(res, runtime.outputOptions), stderr: "", exitCode: 0 };
}
const rows = res.staffs.map((s, i) => [i + 1, s.id, s.name, s.cellphone || "-", s.active ? "在职" : "离职"]);
return {
stdout: `====== 角色成员列表 (${res.roleName || res.roleId}) ======\n${printTable(["#", "员工 ID", "姓名", "手机号", "状态"], rows)}\n`,
stderr: "",
exitCode: 0,
};
}