feat(cli): add corps search command and intuitive aliases
- Implement hose-use corps search <keyword> with case-insensitive fuzzy matching across id, name, corpId, staffId, staffName, and accountId - Format terminal table output with matched cell brackets, hit labels, active corp markers, and friendly no-match fallback - Support --json structured output conforming to CliResult specification - Add intuitive aliases for frequently guessed commands: auth list -> auth accounts, corps find -> corps search, config show -> config paths - Enhance CorpSummary and CorpContext with corpId, staffName, and getAllCorps - Add comprehensive unit tests in corps.test.ts, auth.test.ts, and config.test.ts
This commit is contained in:
1 parent
8efce4342a
commit
1cd8901f7a
10 files changed
+630
-11
No files matched your search
@@ -29,6 +29,7 @@ hose-use auth refresh # 批量刷新账号名下全部
|
|||||||
|
|
||||||
# 2. 多企业租户管理
|
# 2. 多企业租户管理
|
||||||
hose-use corps list # 列出名下所有已配置企业
|
hose-use corps list # 列出名下所有已配置企业
|
||||||
|
hose-use corps search <关键词> # 模糊搜索已保存的企业与员工 (别名: corps find)
|
||||||
hose-use corps switch <corpId> # 切换工作企业
|
hose-use corps switch <corpId> # 切换工作企业
|
||||||
hose-use corps current # 查看当前激活企业
|
hose-use corps current # 查看当前激活企业
|
||||||
|
|
||||||
|
|||||||
@@ -87,6 +87,14 @@ hose-use corps list
|
|||||||
hose-use corps list --json
|
hose-use corps list --json
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 模糊搜索企业 (`corps search` / `corps find`)
|
||||||
|
```bash
|
||||||
|
# 支持根据企业名称、企业 ID、员工工号、员工姓名或所属账号模糊搜索
|
||||||
|
hose-use corps search 某企业
|
||||||
|
hose-use corps search 17718556446
|
||||||
|
hose-use corps search 周某某 --json
|
||||||
|
```
|
||||||
|
|
||||||
### 切换当前活跃企业
|
### 切换当前活跃企业
|
||||||
```bash
|
```bash
|
||||||
hose-use corps switch uzU7pcWfScck00
|
hose-use corps switch uzU7pcWfScck00
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import { describe, it, expect } from "bun:test";
|
||||||
|
import { handleAuthCommand } from "./auth.js";
|
||||||
|
import type { CliRuntime } from "../types.js";
|
||||||
|
|
||||||
|
describe("cli/commands/auth > aliases and subcommands", () => {
|
||||||
|
const dummyRuntime: CliRuntime = {
|
||||||
|
corpContext: {
|
||||||
|
listCorps: () => [],
|
||||||
|
getCurrent: () => null,
|
||||||
|
switchCorp: () => false,
|
||||||
|
resolve: () => null,
|
||||||
|
} as any,
|
||||||
|
selectedCorp: null,
|
||||||
|
jsonMode: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
it("maps auth list to handleAccounts smoothly", async () => {
|
||||||
|
const res = await handleAuthCommand(
|
||||||
|
{
|
||||||
|
area: "auth",
|
||||||
|
action: "list",
|
||||||
|
args: [],
|
||||||
|
options: {},
|
||||||
|
raw: ["auth", "list"],
|
||||||
|
},
|
||||||
|
dummyRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
// 应该输出已保存账号表格或无账号提示
|
||||||
|
expect(res.stdout).toMatch(/(已保存账号列表|暂无保存的账号)/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("supports auth accounts with --json mode", async () => {
|
||||||
|
const jsonRuntime = { ...dummyRuntime, jsonMode: true };
|
||||||
|
const res = await handleAuthCommand(
|
||||||
|
{
|
||||||
|
area: "auth",
|
||||||
|
action: "list",
|
||||||
|
args: [],
|
||||||
|
options: { json: true },
|
||||||
|
raw: ["auth", "list", "--json"],
|
||||||
|
},
|
||||||
|
jsonRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
const data = JSON.parse(res.stdout);
|
||||||
|
expect(data).toHaveProperty("accounts");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles auth status / current aliases without crash", async () => {
|
||||||
|
const res = await handleAuthCommand(
|
||||||
|
{
|
||||||
|
area: "auth",
|
||||||
|
action: "current",
|
||||||
|
args: [],
|
||||||
|
options: {},
|
||||||
|
raw: ["auth", "current"],
|
||||||
|
},
|
||||||
|
dummyRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
expect(res.stdout).toContain("当前登录与企业状态");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -31,16 +31,26 @@ export async function handleAuthCommand(
|
|||||||
return handleLogin(parsed, runtime);
|
return handleLogin(parsed, runtime);
|
||||||
case "status":
|
case "status":
|
||||||
case "whoami":
|
case "whoami":
|
||||||
|
case "current":
|
||||||
return handleStatus(parsed, runtime);
|
return handleStatus(parsed, runtime);
|
||||||
case "refresh":
|
case "refresh":
|
||||||
|
case "renew":
|
||||||
return handleRefresh(parsed, runtime);
|
return handleRefresh(parsed, runtime);
|
||||||
case "import":
|
case "import":
|
||||||
|
case "add":
|
||||||
return handleImport(parsed, runtime);
|
return handleImport(parsed, runtime);
|
||||||
case "add-link":
|
case "add-link":
|
||||||
|
case "link":
|
||||||
|
case "import-link":
|
||||||
return handleAddLink(parsed, runtime);
|
return handleAddLink(parsed, runtime);
|
||||||
case "accounts":
|
case "accounts":
|
||||||
|
case "list":
|
||||||
|
case "account":
|
||||||
|
case "accs":
|
||||||
return handleAccounts(parsed, runtime);
|
return handleAccounts(parsed, runtime);
|
||||||
case "switch-account":
|
case "switch-account":
|
||||||
|
case "switch":
|
||||||
|
case "use-account":
|
||||||
return handleSwitchAccount(parsed, runtime);
|
return handleSwitchAccount(parsed, runtime);
|
||||||
default:
|
default:
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -0,0 +1,87 @@
|
|||||||
|
import { describe, it, expect } from "bun:test";
|
||||||
|
import { handleConfigCommand } from "./config.js";
|
||||||
|
import type { CliRuntime } from "../types.js";
|
||||||
|
|
||||||
|
describe("cli/commands/config > aliases and path mapping", () => {
|
||||||
|
const dummyRuntime: CliRuntime = {
|
||||||
|
corpContext: {
|
||||||
|
listCorps: () => [],
|
||||||
|
getCurrent: () => null,
|
||||||
|
switchCorp: () => false,
|
||||||
|
resolve: () => null,
|
||||||
|
} as any,
|
||||||
|
selectedCorp: null,
|
||||||
|
jsonMode: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
it("maps config show to config paths diagnosis", async () => {
|
||||||
|
const res = await handleConfigCommand(
|
||||||
|
{
|
||||||
|
area: "config",
|
||||||
|
action: "show",
|
||||||
|
args: [],
|
||||||
|
options: {},
|
||||||
|
raw: ["config", "show"],
|
||||||
|
},
|
||||||
|
dummyRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
expect(res.stdout).toContain("配置路径诊断 (Config Paths)");
|
||||||
|
expect(res.stdout).toContain("当前生效配置文件");
|
||||||
|
expect(res.stdout).toContain("运行模式 (Mode)");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("supports config paths directly", async () => {
|
||||||
|
const res = await handleConfigCommand(
|
||||||
|
{
|
||||||
|
area: "config",
|
||||||
|
action: "paths",
|
||||||
|
args: [],
|
||||||
|
options: {},
|
||||||
|
raw: ["config", "paths"],
|
||||||
|
},
|
||||||
|
dummyRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
expect(res.stdout).toContain("配置路径诊断 (Config Paths)");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("supports config show --json structured output", async () => {
|
||||||
|
const jsonRuntime = { ...dummyRuntime, jsonMode: true };
|
||||||
|
const res = await handleConfigCommand(
|
||||||
|
{
|
||||||
|
area: "config",
|
||||||
|
action: "show",
|
||||||
|
args: [],
|
||||||
|
options: { json: true },
|
||||||
|
raw: ["config", "show", "--json"],
|
||||||
|
},
|
||||||
|
jsonRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
const data = JSON.parse(res.stdout);
|
||||||
|
expect(data).toHaveProperty("configFile");
|
||||||
|
expect(data).toHaveProperty("writableDir");
|
||||||
|
expect(data).toHaveProperty("mode");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("supports config get for inspecting global stateful configuration", async () => {
|
||||||
|
const res = await handleConfigCommand(
|
||||||
|
{
|
||||||
|
area: "config",
|
||||||
|
action: "get",
|
||||||
|
args: [],
|
||||||
|
options: {},
|
||||||
|
raw: ["config", "get"],
|
||||||
|
},
|
||||||
|
dummyRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
expect(res.stdout).toContain("全局运行配置");
|
||||||
|
expect(res.stdout).toContain("运行模式 (Mode)");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -15,17 +15,19 @@ export async function handleConfigCommand(
|
|||||||
parsed: CliParsedArgs,
|
parsed: CliParsedArgs,
|
||||||
runtime: CliRuntime
|
runtime: CliRuntime
|
||||||
): Promise<CliResult> {
|
): Promise<CliResult> {
|
||||||
const action = parsed.action || "show";
|
const action = parsed.action || "paths";
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case "show":
|
|
||||||
case "get":
|
|
||||||
case "list":
|
|
||||||
return handleShow(parsed, runtime);
|
|
||||||
case "paths":
|
case "paths":
|
||||||
case "path":
|
case "path":
|
||||||
case "dirs":
|
case "dirs":
|
||||||
|
case "show":
|
||||||
|
case "list":
|
||||||
return handlePaths(parsed, runtime);
|
return handlePaths(parsed, runtime);
|
||||||
|
case "get":
|
||||||
|
case "status":
|
||||||
|
case "info":
|
||||||
|
return handleShow(parsed, runtime);
|
||||||
case "mode":
|
case "mode":
|
||||||
return handleMode(parsed, runtime);
|
return handleMode(parsed, runtime);
|
||||||
case "test-dir":
|
case "test-dir":
|
||||||
@@ -36,7 +38,7 @@ export async function handleConfigCommand(
|
|||||||
default:
|
default:
|
||||||
return {
|
return {
|
||||||
stdout: "",
|
stdout: "",
|
||||||
stderr: `未知 config 子命令: ${action}。支持: show, paths, mode <stateful|stateless>, test-dir <path|--clear>, set <key> <val>\n`,
|
stderr: `未知 config 子命令: ${action}。支持: paths, show, get, mode <stateful|stateless>, test-dir <path|--clear>, set <key> <val>\n`,
|
||||||
exitCode: 1,
|
exitCode: 1,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -227,11 +229,17 @@ async function handlePaths(parsed: CliParsedArgs, runtime: CliRuntime): Promise<
|
|||||||
const writableDir = resolveWritableEnterprisesDir();
|
const writableDir = resolveWritableEnterprisesDir();
|
||||||
const configFile = resolveGlobalConfigFile();
|
const configFile = resolveGlobalConfigFile();
|
||||||
const candidates = getEnterprisesDirCandidates();
|
const candidates = getEnterprisesDirCandidates();
|
||||||
|
const globalConfig = loadGlobalConfig();
|
||||||
|
const currentCorp = runtime.selectedCorp;
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
configFile,
|
configFile,
|
||||||
writableDir,
|
writableDir,
|
||||||
existingDirs,
|
existingDirs,
|
||||||
|
mode: globalConfig.stateful ? "stateful" : "stateless",
|
||||||
|
stateful: globalConfig.stateful,
|
||||||
|
defaultCorpId: globalConfig.defaultCorpId || null,
|
||||||
|
activeCorp: currentCorp ? { id: currentCorp.id, name: currentCorp.name } : null,
|
||||||
env: {
|
env: {
|
||||||
HOSE_ENTERPRISES_DIR: envDir || null,
|
HOSE_ENTERPRISES_DIR: envDir || null,
|
||||||
HOSE_USER_ENTERPRISES_DIR: envUserDir || null,
|
HOSE_USER_ENTERPRISES_DIR: envUserDir || null,
|
||||||
@@ -241,13 +249,16 @@ async function handlePaths(parsed: CliParsedArgs, runtime: CliRuntime): Promise<
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (runtime.jsonMode) {
|
if (runtime.jsonMode) {
|
||||||
return { stdout: printJson(data), stderr: "", exitCode: 0 };
|
return { stdout: printJson(data, runtime.outputOptions), stderr: "", exitCode: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
const output = printKeyValue({
|
const output = printKeyValue({
|
||||||
"当前生效配置文件": configFile,
|
"当前生效配置文件": configFile,
|
||||||
"凭证保存写入目录": writableDir,
|
"凭证保存写入目录": writableDir,
|
||||||
"已发现企业目录": existingDirs.length ? existingDirs.join("\n ") : "无 (未发现任何有效企业目录)",
|
"已发现企业目录": existingDirs.length ? existingDirs.join("\n ") : "无 (未发现任何有效企业目录)",
|
||||||
|
"运行模式 (Mode)": globalConfig.stateful ? "有状态模式 (Stateful - 自动记忆默认企业)" : "无状态模式 (Stateless - 每次需显式传 --corp)",
|
||||||
|
"默认工作企业": globalConfig.defaultCorpId || "未设置 (使用首个可用企业)",
|
||||||
|
"当前解析企业": currentCorp ? `${currentCorp.name} (${currentCorp.id})` : "无",
|
||||||
"环境变量指定目录": envDir || "未设置 (HOSE_ENTERPRISES_DIR)",
|
"环境变量指定目录": envDir || "未设置 (HOSE_ENTERPRISES_DIR)",
|
||||||
"全部目录候选链": candidates.join("\n "),
|
"全部目录候选链": candidates.join("\n "),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -79,3 +79,240 @@ describe("cli/commands/corps > handleCorpsCommand add-link", () => {
|
|||||||
expect(res.stderr).toContain("必须提供协助/免密链接 URL");
|
expect(res.stderr).toContain("必须提供协助/免密链接 URL");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("cli/commands/corps > handleCorpsCommand search & aliases", () => {
|
||||||
|
const mockCorps = [
|
||||||
|
{
|
||||||
|
id: "corpAlpha",
|
||||||
|
name: "阿尔法网络科技有限公司",
|
||||||
|
corpId: "alphaPhysicalId",
|
||||||
|
staffId: "STAFF_001",
|
||||||
|
staffName: "张三",
|
||||||
|
accountId: "13800000001",
|
||||||
|
source: "account",
|
||||||
|
baseUrl: "https://app.ekuaibao.com",
|
||||||
|
credentials: {
|
||||||
|
accessToken: "token-alpha",
|
||||||
|
corpId: "alphaPhysicalId",
|
||||||
|
staffId: "STAFF_001",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "corpBeta@13900000002",
|
||||||
|
name: "贝塔云端信息系统",
|
||||||
|
corpId: "corpBeta",
|
||||||
|
staffId: "STAFF_002",
|
||||||
|
staffName: "李四",
|
||||||
|
accountId: "13900000002",
|
||||||
|
source: "link",
|
||||||
|
baseUrl: "https://wx2.ekuaibao.com",
|
||||||
|
credentials: {
|
||||||
|
accessToken: "token-beta",
|
||||||
|
corpId: "corpBeta",
|
||||||
|
staffId: "STAFF_002",
|
||||||
|
},
|
||||||
|
session: {
|
||||||
|
staffName: "李四",
|
||||||
|
cellphone: "13900000002",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const searchRuntime: CliRuntime = {
|
||||||
|
corpContext: {
|
||||||
|
getAllCorps: () => mockCorps,
|
||||||
|
listCorps: () => mockCorps.map((c) => ({
|
||||||
|
id: c.id,
|
||||||
|
name: c.name,
|
||||||
|
corpId: c.corpId,
|
||||||
|
staffId: c.staffId,
|
||||||
|
staffName: c.staffName,
|
||||||
|
accountId: c.accountId,
|
||||||
|
source: c.source,
|
||||||
|
current: c.id === "corpAlpha",
|
||||||
|
})),
|
||||||
|
getCurrent: () => mockCorps[0],
|
||||||
|
switchCorp: (id: string) => mockCorps.some((c) => c.id === id),
|
||||||
|
resolve: () => mockCorps[0],
|
||||||
|
} as any,
|
||||||
|
selectedCorp: mockCorps[0] as any,
|
||||||
|
jsonMode: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
it("returns error exit code when search keyword is empty", async () => {
|
||||||
|
const res = await handleCorpsCommand(
|
||||||
|
{
|
||||||
|
area: "corps",
|
||||||
|
action: "search",
|
||||||
|
args: [],
|
||||||
|
options: {},
|
||||||
|
raw: ["corps", "search"],
|
||||||
|
},
|
||||||
|
searchRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(1);
|
||||||
|
expect(res.stderr).toContain("用法: hose-use corps search <关键词>");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("searches by name with case-insensitive substring match", async () => {
|
||||||
|
const res = await handleCorpsCommand(
|
||||||
|
{
|
||||||
|
area: "corps",
|
||||||
|
action: "search",
|
||||||
|
args: ["阿尔法"],
|
||||||
|
options: {},
|
||||||
|
raw: ["corps", "search", "阿尔法"],
|
||||||
|
},
|
||||||
|
searchRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
expect(res.stdout).toContain("[阿尔法网络科技有限公司]");
|
||||||
|
expect(res.stdout).toContain("命中企业名称");
|
||||||
|
expect(res.stdout).toContain("* 1"); // 当前激活标记
|
||||||
|
});
|
||||||
|
|
||||||
|
it("searches by staffName and highlights the matched cell", async () => {
|
||||||
|
const res = await handleCorpsCommand(
|
||||||
|
{
|
||||||
|
area: "corps",
|
||||||
|
action: "search",
|
||||||
|
args: ["李四"],
|
||||||
|
options: {},
|
||||||
|
raw: ["corps", "search", "李四"],
|
||||||
|
},
|
||||||
|
searchRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
expect(res.stdout).toContain("[李四]");
|
||||||
|
expect(res.stdout).toContain("命中员工姓名");
|
||||||
|
expect(res.stdout).toContain("corpBeta");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("searches by staffId case-insensitively", async () => {
|
||||||
|
const res = await handleCorpsCommand(
|
||||||
|
{
|
||||||
|
area: "corps",
|
||||||
|
action: "search",
|
||||||
|
args: ["staff_001"],
|
||||||
|
options: {},
|
||||||
|
raw: ["corps", "search", "staff_001"],
|
||||||
|
},
|
||||||
|
searchRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
expect(res.stdout).toContain("[STAFF_001]");
|
||||||
|
expect(res.stdout).toContain("命中员工工号");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("searches by physical corpId", async () => {
|
||||||
|
const res = await handleCorpsCommand(
|
||||||
|
{
|
||||||
|
area: "corps",
|
||||||
|
action: "search",
|
||||||
|
args: ["alphaPhysicalId"],
|
||||||
|
options: {},
|
||||||
|
raw: ["corps", "search", "alphaPhysicalId"],
|
||||||
|
},
|
||||||
|
searchRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
expect(res.stdout).toContain("阿尔法网络科技有限公司");
|
||||||
|
expect(res.stdout).toContain("命中企业ID");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("searches by accountId", async () => {
|
||||||
|
const res = await handleCorpsCommand(
|
||||||
|
{
|
||||||
|
area: "corps",
|
||||||
|
action: "search",
|
||||||
|
args: ["13900000002"],
|
||||||
|
options: {},
|
||||||
|
raw: ["corps", "search", "13900000002"],
|
||||||
|
},
|
||||||
|
searchRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
expect(res.stdout).toContain("[13900000002]");
|
||||||
|
expect(res.stdout).toContain("命中所属账号");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("supports find alias for search", async () => {
|
||||||
|
const res = await handleCorpsCommand(
|
||||||
|
{
|
||||||
|
area: "corps",
|
||||||
|
action: "find",
|
||||||
|
args: ["阿尔法"],
|
||||||
|
options: {},
|
||||||
|
raw: ["corps", "find", "阿尔法"],
|
||||||
|
},
|
||||||
|
searchRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
expect(res.stdout).toContain("[阿尔法网络科技有限公司]");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("outputs friendly message when no corps match", async () => {
|
||||||
|
const res = await handleCorpsCommand(
|
||||||
|
{
|
||||||
|
area: "corps",
|
||||||
|
action: "search",
|
||||||
|
args: ["nonexistent_keyword"],
|
||||||
|
options: {},
|
||||||
|
raw: ["corps", "search", "nonexistent_keyword"],
|
||||||
|
},
|
||||||
|
searchRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
expect(res.stdout).toBe("未找到匹配 \"nonexistent_keyword\" 的企业配置\n");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("supports --json mode with structured output", async () => {
|
||||||
|
const jsonRuntime = { ...searchRuntime, jsonMode: true };
|
||||||
|
const res = await handleCorpsCommand(
|
||||||
|
{
|
||||||
|
area: "corps",
|
||||||
|
action: "search",
|
||||||
|
args: ["阿尔法"],
|
||||||
|
options: { json: true },
|
||||||
|
raw: ["corps", "search", "阿尔法", "--json"],
|
||||||
|
},
|
||||||
|
jsonRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
const data = JSON.parse(res.stdout);
|
||||||
|
expect(data.keyword).toBe("阿尔法");
|
||||||
|
expect(data.count).toBe(1);
|
||||||
|
expect(data.activeCorpId).toBe("corpAlpha");
|
||||||
|
expect(data.corporations).toHaveLength(1);
|
||||||
|
expect(data.corporations[0].id).toBe("corpAlpha");
|
||||||
|
expect(data.corporations[0].name).toBe("阿尔法网络科技有限公司");
|
||||||
|
expect(data.corporations[0].matchedFields).toContain("name");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("outputs help message via corps help", async () => {
|
||||||
|
const res = await handleCorpsCommand(
|
||||||
|
{
|
||||||
|
area: "corps",
|
||||||
|
action: "help",
|
||||||
|
args: [],
|
||||||
|
options: {},
|
||||||
|
raw: ["corps", "help"],
|
||||||
|
},
|
||||||
|
searchRuntime
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.exitCode).toBe(0);
|
||||||
|
expect(res.stdout).toContain("hose-use corps <子命令>");
|
||||||
|
expect(res.stdout).toContain("search <关键词>");
|
||||||
|
expect(res.stdout).toContain("find");
|
||||||
|
});
|
||||||
|
});
|
||||||
+190
-1
@@ -6,6 +6,15 @@ import { prompt } from "../helpers.js";
|
|||||||
import { printJson, printKeyValue, printTable, formatSourceLabel, formatExpiresAt } from "../printer.js";
|
import { printJson, printKeyValue, printTable, formatSourceLabel, formatExpiresAt } from "../printer.js";
|
||||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
||||||
|
|
||||||
|
const FIELD_LABEL_MAP: Record<string, string> = {
|
||||||
|
id: "配置ID",
|
||||||
|
name: "企业名称",
|
||||||
|
corpId: "企业ID",
|
||||||
|
staffId: "员工工号",
|
||||||
|
staffName: "员工姓名",
|
||||||
|
accountId: "所属账号",
|
||||||
|
};
|
||||||
|
|
||||||
export async function handleCorpsCommand(
|
export async function handleCorpsCommand(
|
||||||
parsed: CliParsedArgs,
|
parsed: CliParsedArgs,
|
||||||
runtime: CliRuntime
|
runtime: CliRuntime
|
||||||
@@ -14,23 +23,203 @@ export async function handleCorpsCommand(
|
|||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case "list":
|
case "list":
|
||||||
|
case "ls":
|
||||||
return handleList(parsed, runtime);
|
return handleList(parsed, runtime);
|
||||||
|
case "search":
|
||||||
|
case "find":
|
||||||
|
case "query":
|
||||||
|
case "filter":
|
||||||
|
return handleSearch(parsed, runtime);
|
||||||
case "switch":
|
case "switch":
|
||||||
case "use":
|
case "use":
|
||||||
return handleSwitch(parsed, runtime);
|
return handleSwitch(parsed, runtime);
|
||||||
case "current":
|
case "current":
|
||||||
|
case "whoami":
|
||||||
|
case "status":
|
||||||
|
case "active":
|
||||||
return handleCurrent(parsed, runtime);
|
return handleCurrent(parsed, runtime);
|
||||||
case "add-link":
|
case "add-link":
|
||||||
|
case "link":
|
||||||
|
case "import-link":
|
||||||
return handleAddLink(parsed, runtime);
|
return handleAddLink(parsed, runtime);
|
||||||
|
case "help":
|
||||||
|
return handleCorpsHelp();
|
||||||
default:
|
default:
|
||||||
return {
|
return {
|
||||||
stdout: "",
|
stdout: "",
|
||||||
stderr: `未知 corps 子命令: ${action}。支持: list, switch, current, add-link\n`,
|
stderr: `未知 corps 子命令: ${action}。支持: list, search, switch, current, add-link\n`,
|
||||||
exitCode: 1,
|
exitCode: 1,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function printCorpsHelp(): string {
|
||||||
|
return `
|
||||||
|
用法:
|
||||||
|
hose-use corps <子命令> [参数] [选项]
|
||||||
|
|
||||||
|
子命令:
|
||||||
|
list 列出本地已保存的所有企业配置 (别名: ls)
|
||||||
|
search <关键词> 根据关键词模糊搜索企业(支持 id, name, corpId, staffId, staffName, accountId,别名: find)
|
||||||
|
switch <企业ID> 切换当前激活的工作企业 (别名: use)
|
||||||
|
current 查看当前激活的企业配置与凭证详情
|
||||||
|
add-link <URL> 从第三方协助/免密链接导入企业凭证
|
||||||
|
|
||||||
|
选项:
|
||||||
|
--json, -j 以 JSON 格式输出
|
||||||
|
--help, -h 查看帮助信息
|
||||||
|
|
||||||
|
示例:
|
||||||
|
hose-use corps list
|
||||||
|
hose-use corps search 某企业
|
||||||
|
hose-use corps search 17718556446
|
||||||
|
hose-use corps switch ID01MCcOgJl1td
|
||||||
|
`.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleCorpsHelp(): CliResult {
|
||||||
|
return {
|
||||||
|
stdout: printCorpsHelp() + "\n",
|
||||||
|
stderr: "",
|
||||||
|
exitCode: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleSearch(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||||
|
const keyword = parsed.args[0] || stringOption(parsed.options, ["keyword", "query", "k", "q", "name", "id"]);
|
||||||
|
if (!keyword || !keyword.trim()) {
|
||||||
|
return {
|
||||||
|
stdout: "",
|
||||||
|
stderr: "用法: hose-use corps search <关键词>\n",
|
||||||
|
exitCode: 1,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const kw = keyword.trim().toLowerCase();
|
||||||
|
|
||||||
|
// 从 CorpContext 中获取企业列表
|
||||||
|
const rawCorps = typeof runtime.corpContext.getAllCorps === "function"
|
||||||
|
? runtime.corpContext.getAllCorps()
|
||||||
|
: null;
|
||||||
|
|
||||||
|
const normalizedCorps = rawCorps
|
||||||
|
? rawCorps.map((c) => ({
|
||||||
|
id: c.id,
|
||||||
|
name: c.name,
|
||||||
|
corpId: c.corpId || c.credentials?.corpId || c.id,
|
||||||
|
staffId: c.staffId || c.credentials?.staffId || "",
|
||||||
|
staffName: c.staffName || c.session?.staffName || "",
|
||||||
|
accountId: c.accountId || "",
|
||||||
|
source: c.source || "manual",
|
||||||
|
baseUrl: c.baseUrl,
|
||||||
|
current: c.id === runtime.selectedCorp?.id,
|
||||||
|
expiresAt: c.auth?.expiresAt || (c.session?.expTime ?? undefined),
|
||||||
|
}))
|
||||||
|
: runtime.corpContext.listCorps().map((c) => ({
|
||||||
|
id: c.id,
|
||||||
|
name: c.name,
|
||||||
|
corpId: c.corpId || c.id,
|
||||||
|
staffId: c.staffId || "",
|
||||||
|
staffName: c.staffName || "",
|
||||||
|
accountId: c.accountId || "",
|
||||||
|
source: c.source || "manual",
|
||||||
|
baseUrl: c.baseUrl,
|
||||||
|
current: c.current || c.id === runtime.selectedCorp?.id,
|
||||||
|
expiresAt: c.expiresAt,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const matches: Array<typeof normalizedCorps[number] & { matchedFields: string[] }> = [];
|
||||||
|
|
||||||
|
for (const corp of normalizedCorps) {
|
||||||
|
const matchedFields: string[] = [];
|
||||||
|
|
||||||
|
if (corp.id && corp.id.toLowerCase().includes(kw)) {
|
||||||
|
matchedFields.push("id");
|
||||||
|
}
|
||||||
|
if (corp.name && corp.name.toLowerCase().includes(kw)) {
|
||||||
|
matchedFields.push("name");
|
||||||
|
}
|
||||||
|
if (corp.corpId && corp.corpId.toLowerCase().includes(kw) && !matchedFields.includes("corpId") && corp.corpId !== corp.id) {
|
||||||
|
matchedFields.push("corpId");
|
||||||
|
}
|
||||||
|
if (corp.staffId && corp.staffId.toLowerCase().includes(kw)) {
|
||||||
|
matchedFields.push("staffId");
|
||||||
|
}
|
||||||
|
if (corp.staffName && corp.staffName.toLowerCase().includes(kw)) {
|
||||||
|
matchedFields.push("staffName");
|
||||||
|
}
|
||||||
|
if (corp.accountId && corp.accountId.toLowerCase().includes(kw)) {
|
||||||
|
matchedFields.push("accountId");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matchedFields.length > 0) {
|
||||||
|
matches.push({ ...corp, matchedFields });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (runtime.jsonMode) {
|
||||||
|
return {
|
||||||
|
stdout: printJson(
|
||||||
|
{
|
||||||
|
keyword: keyword.trim(),
|
||||||
|
count: matches.length,
|
||||||
|
activeCorpId: runtime.selectedCorp?.id,
|
||||||
|
corporations: matches.map((m) => ({
|
||||||
|
id: m.id,
|
||||||
|
name: m.name,
|
||||||
|
corpId: m.corpId,
|
||||||
|
staffId: m.staffId,
|
||||||
|
staffName: m.staffName || undefined,
|
||||||
|
accountId: m.accountId || undefined,
|
||||||
|
source: m.source,
|
||||||
|
baseUrl: m.baseUrl,
|
||||||
|
current: m.current,
|
||||||
|
expiresAt: m.expiresAt,
|
||||||
|
matchedFields: m.matchedFields,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
runtime.outputOptions
|
||||||
|
),
|
||||||
|
stderr: "",
|
||||||
|
exitCode: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matches.length === 0) {
|
||||||
|
return {
|
||||||
|
stdout: `未找到匹配 "${keyword.trim()}" 的企业配置\n`,
|
||||||
|
stderr: "",
|
||||||
|
exitCode: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const rows = matches.map((m, i) => {
|
||||||
|
const isCurrent = m.current;
|
||||||
|
const matchLabels = m.matchedFields.map((f) => FIELD_LABEL_MAP[f] || f);
|
||||||
|
return [
|
||||||
|
isCurrent ? `* ${i + 1}` : ` ${i + 1}`,
|
||||||
|
m.matchedFields.includes("id") || m.matchedFields.includes("corpId") ? `[${m.id}]` : m.id,
|
||||||
|
m.matchedFields.includes("name") ? `[${m.name}]` : m.name,
|
||||||
|
m.matchedFields.includes("staffId") ? `[${m.staffId}]` : m.staffId,
|
||||||
|
m.staffName ? (m.matchedFields.includes("staffName") ? `[${m.staffName}]` : m.staffName) : "-",
|
||||||
|
m.accountId ? (m.matchedFields.includes("accountId") ? `[${m.accountId}]` : m.accountId) : "-",
|
||||||
|
matchLabels.map((l) => `命中${l}`).join(", "),
|
||||||
|
formatExpiresAt(m.expiresAt),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
const table = printTable(
|
||||||
|
["#", "企业 ID", "企业名称", "员工工号", "员工姓名", "所属账号", "匹配项", "Token 过期时间"],
|
||||||
|
rows
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
stdout: `====== 企业搜索结果 (关键词: "${keyword.trim()}", 找到 ${matches.length} 家) ======\n${table}\n\n提示:\n - 标记 * 为当前激活企业\n - 标记 [...] 为命中关键词的字段\n - 使用 hose-use corps switch <企业ID> 切换工作企业\n`,
|
||||||
|
stderr: "",
|
||||||
|
exitCode: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async function handleList(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
async function handleList(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||||
const corps = runtime.corpContext.listCorps();
|
const corps = runtime.corpContext.listCorps();
|
||||||
|
|
||||||
|
|||||||
+3
-2
@@ -118,8 +118,8 @@ export function printHelp(): string {
|
|||||||
|
|
||||||
核心命令域:
|
核心命令域:
|
||||||
context 查看全局运行上下文与身份诊断 (context, status, whoami, env)
|
context 查看全局运行上下文与身份诊断 (context, status, whoami, env)
|
||||||
auth 认证与凭证管理 (login, status, refresh, import, accounts)
|
auth 认证与凭证管理 (login, status, refresh, import, accounts, switch-account)
|
||||||
corps 多企业租户管理 (list, switch, current)
|
corps 多企业租户管理 (list, search <关键词>, switch, current, add-link)
|
||||||
aipro 合思 AI PRO 确定性审批仲裁网关入口 (status, scopes, bind, checklist, review, decide, logs)
|
aipro 合思 AI PRO 确定性审批仲裁网关入口 (status, scopes, bind, checklist, review, decide, logs)
|
||||||
config 全局运行模式与路径诊断 (show, paths, mode <stateful|stateless>, set)
|
config 全局运行模式与路径诊断 (show, paths, mode <stateful|stateless>, set)
|
||||||
workflow 工作流管理 (list, get, find, analyze, toggle)
|
workflow 工作流管理 (list, get, find, analyze, toggle)
|
||||||
@@ -163,6 +163,7 @@ export function printHelp(): string {
|
|||||||
hose-use config paths # 查看多级企业配置目录链
|
hose-use config paths # 查看多级企业配置目录链
|
||||||
hose-use config mode stateless # 切换为无状态模式 (防止 Agent 误操作)
|
hose-use config mode stateless # 切换为无状态模式 (防止 Agent 误操作)
|
||||||
hose-use corps list # 列出名下所有企业
|
hose-use corps list # 列出名下所有企业
|
||||||
|
hose-use corps search 某企业 # 模糊搜索已保存的企业与员工信息
|
||||||
hose-use corps switch ID01MCcOgJl1td # 切换工作企业
|
hose-use corps switch ID01MCcOgJl1td # 切换工作企业
|
||||||
hose-use contact search 张三 # 模糊搜索员工工号、邮箱与电话
|
hose-use contact search 张三 # 模糊搜索员工工号、邮箱与电话
|
||||||
hose-use contact list --limit=20 # 采样查看企业通讯录(带防爆截断保护)
|
hose-use contact list --limit=20 # 采样查看企业通讯录(带防爆截断保护)
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { saveGlobalConfig } from "./loader.js";
|
|||||||
export interface CorpSummary {
|
export interface CorpSummary {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
corpId?: string;
|
||||||
|
staffName?: string;
|
||||||
source?: string;
|
source?: string;
|
||||||
accountId?: string;
|
accountId?: string;
|
||||||
staffId: string;
|
staffId: string;
|
||||||
@@ -143,13 +145,19 @@ export class CorpContext {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getAllCorps(): CorpConfig[] {
|
||||||
|
return Array.from(this.corps.values());
|
||||||
|
}
|
||||||
|
|
||||||
listCorps(): CorpSummary[] {
|
listCorps(): CorpSummary[] {
|
||||||
return Array.from(this.corps.values()).map((corp) => ({
|
return Array.from(this.corps.values()).map((corp) => ({
|
||||||
id: corp.id,
|
id: corp.id,
|
||||||
name: corp.name,
|
name: corp.name,
|
||||||
|
corpId: corp.corpId || corp.credentials.corpId,
|
||||||
|
staffId: corp.credentials.staffId,
|
||||||
|
staffName: corp.staffName || corp.session?.staffName || undefined,
|
||||||
source: corp.source || "manual",
|
source: corp.source || "manual",
|
||||||
accountId: corp.accountId,
|
accountId: corp.accountId,
|
||||||
staffId: corp.credentials.staffId,
|
|
||||||
baseUrl: corp.baseUrl,
|
baseUrl: corp.baseUrl,
|
||||||
current: corp.id === this.current?.id,
|
current: corp.id === this.current?.id,
|
||||||
expiresAt: corp.auth?.expiresAt || (corp.session?.expTime ?? undefined),
|
expiresAt: corp.auth?.expiresAt || (corp.session?.expTime ?? undefined),
|
||||||
|
|||||||
Reference in new issue
Block a user