Files
hose-use/src/cli/commands/config.ts
T

199 lines
6.4 KiB
TypeScript

import {
loadGlobalConfig,
saveGlobalConfig,
resolveGlobalConfigFile,
resolveEnterprisesDirs,
resolveWritableEnterprisesDir,
getEnterprisesDirCandidates,
} from "../../core/config/loader.js";
import { printJson, printKeyValue } from "../printer.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
export async function handleConfigCommand(
parsed: CliParsedArgs,
runtime: CliRuntime
): Promise<CliResult> {
const action = parsed.action || "show";
switch (action) {
case "show":
case "get":
case "list":
return handleShow(parsed, runtime);
case "paths":
case "path":
case "dirs":
return handlePaths(parsed, runtime);
case "mode":
return handleMode(parsed, runtime);
case "set":
return handleSet(parsed, runtime);
default:
return {
stdout: "",
stderr: `未知 config 子命令: ${action}。支持: show, paths, mode <stateful|stateless>, set <key> <val>\n`,
exitCode: 1,
};
}
}
async function handleShow(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const globalConfig = loadGlobalConfig();
const currentCorp = runtime.selectedCorp;
const configPath = resolveGlobalConfigFile();
const summaryObj = {
mode: globalConfig.stateful ? "stateful" : "stateless",
stateful: globalConfig.stateful,
defaultCorpId: globalConfig.defaultCorpId || null,
defaultAccountId: globalConfig.defaultAccountId || null,
activeCorp: currentCorp ? { id: currentCorp.id, name: currentCorp.name } : null,
updateRepo: globalConfig.updateRepo || null,
updateSource: globalConfig.updateSource || "auto",
configFile: configPath,
};
if (runtime.jsonMode) {
return { stdout: printJson(summaryObj), stderr: "", exitCode: 0 };
}
const output = printKeyValue({
"运行模式 (Mode)": globalConfig.stateful ? "有状态模式 (Stateful - 自动记忆默认企业)" : "无状态模式 (Stateless - 每次需显式传 --corp)",
"默认工作企业": globalConfig.defaultCorpId || "未设置 (使用首个可用企业)",
"当前解析企业": currentCorp ? `${currentCorp.name} (${currentCorp.id})` : "无",
"默认自更新仓库": globalConfig.updateRepo || "官方默认 (yunyingk/hose-use)",
"自更新源偏好": globalConfig.updateSource || "auto (自动择优)",
"配置文件路径": configPath,
});
return {
stdout: `====== 全局运行配置 ======\n${output}\n\n提示:\n 切换模式: hose config mode <stateful|stateless>\n 指定企业: hose corps switch <corpId>\n 更新源设置: hose config set updateSource <auto|github|gitea>\n`,
stderr: "",
exitCode: 0,
};
}
async function handleMode(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const targetMode = parsed.args[0]?.toLowerCase();
if (!targetMode || (targetMode !== "stateful" && targetMode !== "stateless")) {
return {
stdout: "",
stderr: "用法: hose config mode <stateful|stateless>\n",
exitCode: 1,
};
}
const isStateful = targetMode === "stateful";
const updated = saveGlobalConfig({ stateful: isStateful });
if (runtime.jsonMode) {
return { stdout: printJson(updated), stderr: "", exitCode: 0 };
}
return {
stdout: `[SUCCESS] 全局运行模式已切换为: ${isStateful ? "有状态模式 (Stateful)" : "无状态模式 (Stateless)"}\n`,
stderr: "",
exitCode: 0,
};
}
async function handleSet(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const rawKey = parsed.args[0];
const val = parsed.args[1];
if (!rawKey || val === undefined) {
return {
stdout: "",
stderr: "用法: hose config set <key> <val> (如 hose config set defaultCorpId uzU7pcWfScck00)\n",
exitCode: 1,
};
}
const keyLower = rawKey.toLowerCase();
const partial: Record<string, unknown> = {};
let resolvedKey = rawKey;
if (keyLower === "stateful") {
resolvedKey = "stateful";
partial.stateful = val === "true" || val === "1";
} else if (keyLower === "defaultcorpid") {
resolvedKey = "defaultCorpId";
partial.defaultCorpId = val;
} else if (keyLower === "defaultaccountid") {
resolvedKey = "defaultAccountId";
partial.defaultAccountId = val;
} else if (keyLower === "updaterepo") {
resolvedKey = "updateRepo";
partial.updateRepo = val;
} else if (keyLower === "updatesource") {
resolvedKey = "updateSource";
const srcLower = val.toLowerCase();
if (srcLower !== "auto" && srcLower !== "github" && srcLower !== "gitea") {
return {
stdout: "",
stderr: `不支持的 updateSource 值: ${val}。可选: auto, github, gitea\n`,
exitCode: 1,
};
}
partial.updateSource = srcLower;
} else {
return {
stdout: "",
stderr: `不支持配置键: ${rawKey}。支持: stateful, defaultCorpId, defaultAccountId, updateRepo, updateSource\n`,
exitCode: 1,
};
}
const updated = saveGlobalConfig(partial);
if (runtime.jsonMode) {
return { stdout: printJson(updated), stderr: "", exitCode: 0 };
}
return {
stdout: `[SUCCESS] 配置已更新: ${resolvedKey} = ${val}\n`,
stderr: "",
exitCode: 0,
};
}
async function handlePaths(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const envDir = process.env.HOSE_ENTERPRISES_DIR;
const envUserDir = process.env.HOSE_USER_ENTERPRISES_DIR;
const envConfigFile = process.env.HOSE_CONFIG_FILE;
const existingDirs = resolveEnterprisesDirs();
const writableDir = resolveWritableEnterprisesDir();
const configFile = resolveGlobalConfigFile();
const candidates = getEnterprisesDirCandidates();
const data = {
configFile,
writableDir,
existingDirs,
env: {
HOSE_ENTERPRISES_DIR: envDir || null,
HOSE_USER_ENTERPRISES_DIR: envUserDir || null,
HOSE_CONFIG_FILE: envConfigFile || null,
},
candidates,
};
if (runtime.jsonMode) {
return { stdout: printJson(data), stderr: "", exitCode: 0 };
}
const output = printKeyValue({
"当前生效配置文件": configFile,
"凭证保存写入目录": writableDir,
"已发现企业目录": existingDirs.length ? existingDirs.join("\n ") : "无 (未发现任何有效企业目录)",
"环境变量指定目录": envDir || "未设置 (HOSE_ENTERPRISES_DIR)",
"全部目录候选链": candidates.join("\n "),
});
return {
stdout: `====== 配置路径诊断 (Config Paths) ======\n${output}\n`,
stderr: "",
exitCode: 0,
};
}