|
|
@@ -20,6 +20,13 @@ pub struct CommandResult {
|
|
|
pub error: Option<String>,
|
|
|
}
|
|
|
|
|
|
+#[derive(Serialize, Deserialize)]
|
|
|
+pub struct DeleteEnvVarsResult {
|
|
|
+ pub success: bool,
|
|
|
+ pub error: Option<String>,
|
|
|
+ pub deleted_vars: Vec<String>,
|
|
|
+}
|
|
|
+
|
|
|
/// 检查 Claude Code 是否已安装
|
|
|
#[tauri::command]
|
|
|
pub async fn check_claude_code() -> ClaudeCodeStatus {
|
|
|
@@ -279,7 +286,7 @@ const CLAUDE_ENV_VARS: &[&str] = &[
|
|
|
|
|
|
/// 删除 Claude Code 相关的环境变量
|
|
|
#[tauri::command]
|
|
|
-pub async fn delete_claude_env_vars() -> CommandResult {
|
|
|
+pub async fn delete_claude_env_vars() -> DeleteEnvVarsResult {
|
|
|
#[cfg(target_os = "windows")]
|
|
|
{
|
|
|
delete_claude_env_vars_windows()
|
|
|
@@ -298,7 +305,7 @@ pub async fn delete_claude_env_vars() -> CommandResult {
|
|
|
|
|
|
/// Windows 平台删除环境变量(用户级和系统级)
|
|
|
#[cfg(target_os = "windows")]
|
|
|
-fn delete_claude_env_vars_windows() -> CommandResult {
|
|
|
+fn delete_claude_env_vars_windows() -> DeleteEnvVarsResult {
|
|
|
let mut deleted_vars = Vec::new();
|
|
|
let mut errors = Vec::new();
|
|
|
|
|
|
@@ -367,22 +374,24 @@ fn delete_claude_env_vars_windows() -> CommandResult {
|
|
|
}
|
|
|
|
|
|
if errors.is_empty() {
|
|
|
- CommandResult {
|
|
|
+ DeleteEnvVarsResult {
|
|
|
success: true,
|
|
|
error: None,
|
|
|
+ deleted_vars,
|
|
|
}
|
|
|
} else {
|
|
|
- CommandResult {
|
|
|
+ DeleteEnvVarsResult {
|
|
|
success: false,
|
|
|
error: Some(errors.join("; ")),
|
|
|
+ deleted_vars,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// Unix 平台删除环境变量(macOS/Linux)- 用户级和系统级
|
|
|
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
|
|
-fn delete_claude_env_vars_unix() -> CommandResult {
|
|
|
- let mut modified_files = Vec::new();
|
|
|
+fn delete_claude_env_vars_unix() -> DeleteEnvVarsResult {
|
|
|
+ let mut deleted_vars = Vec::new();
|
|
|
let mut errors = Vec::new();
|
|
|
|
|
|
// 1. 删除用户级环境变量(用户 home 目录下的配置文件)
|
|
|
@@ -399,9 +408,11 @@ fn delete_claude_env_vars_unix() -> CommandResult {
|
|
|
let config_path = home.join(config_file);
|
|
|
if config_path.exists() {
|
|
|
match remove_env_vars_from_file(&config_path) {
|
|
|
- Ok(modified) => {
|
|
|
+ Ok((modified, vars)) => {
|
|
|
if modified {
|
|
|
- modified_files.push(format!("用户级: ~/{}", config_file));
|
|
|
+ for var in vars {
|
|
|
+ deleted_vars.push(format!("用户级 (~/{}) : {}", config_file, var));
|
|
|
+ }
|
|
|
log::info!("Modified user config: {}", config_file);
|
|
|
}
|
|
|
}
|
|
|
@@ -428,9 +439,11 @@ fn delete_claude_env_vars_unix() -> CommandResult {
|
|
|
let config_path = PathBuf::from(config_file);
|
|
|
if config_path.exists() {
|
|
|
match remove_env_vars_from_file(&config_path) {
|
|
|
- Ok(modified) => {
|
|
|
+ Ok((modified, vars)) => {
|
|
|
if modified {
|
|
|
- modified_files.push(format!("系统级: {}", config_file));
|
|
|
+ for var in vars {
|
|
|
+ deleted_vars.push(format!("系统级 ({}) : {}", config_file, var));
|
|
|
+ }
|
|
|
log::info!("Modified system config: {}", config_file);
|
|
|
}
|
|
|
}
|
|
|
@@ -450,9 +463,11 @@ fn delete_claude_env_vars_unix() -> CommandResult {
|
|
|
let path = entry.path();
|
|
|
if path.extension().map_or(false, |ext| ext == "sh") {
|
|
|
match remove_env_vars_from_file(&path) {
|
|
|
- Ok(modified) => {
|
|
|
+ Ok((modified, vars)) => {
|
|
|
if modified {
|
|
|
- modified_files.push(format!("系统级: {}", path.display()));
|
|
|
+ for var in vars {
|
|
|
+ deleted_vars.push(format!("系统级 ({}) : {}", path.display(), var));
|
|
|
+ }
|
|
|
log::info!("Modified profile.d script: {}", path.display());
|
|
|
}
|
|
|
}
|
|
|
@@ -466,29 +481,34 @@ fn delete_claude_env_vars_unix() -> CommandResult {
|
|
|
}
|
|
|
|
|
|
if errors.is_empty() {
|
|
|
- CommandResult {
|
|
|
+ DeleteEnvVarsResult {
|
|
|
success: true,
|
|
|
error: None,
|
|
|
+ deleted_vars,
|
|
|
}
|
|
|
} else {
|
|
|
- CommandResult {
|
|
|
+ DeleteEnvVarsResult {
|
|
|
success: false,
|
|
|
error: Some(errors.join("; ")),
|
|
|
+ deleted_vars,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// 从文件中移除 Claude 相关的环境变量导出语句
|
|
|
+/// 返回 (是否修改, 删除的变量名列表)
|
|
|
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
|
|
-fn remove_env_vars_from_file(path: &PathBuf) -> Result<bool, String> {
|
|
|
+fn remove_env_vars_from_file(path: &PathBuf) -> Result<(bool, Vec<String>), String> {
|
|
|
let content = fs::read_to_string(path).map_err(|e| e.to_string())?;
|
|
|
|
|
|
let mut modified = false;
|
|
|
let mut new_lines = Vec::new();
|
|
|
+ let mut removed_vars = Vec::new();
|
|
|
|
|
|
for line in content.lines() {
|
|
|
let trimmed = line.trim();
|
|
|
let mut should_remove = false;
|
|
|
+ let mut matched_var: Option<&str> = None;
|
|
|
|
|
|
for var_name in CLAUDE_ENV_VARS {
|
|
|
// 匹配 export VAR_NAME= 或 VAR_NAME= 格式
|
|
|
@@ -497,11 +517,16 @@ fn remove_env_vars_from_file(path: &PathBuf) -> Result<bool, String> {
|
|
|
{
|
|
|
should_remove = true;
|
|
|
modified = true;
|
|
|
+ matched_var = Some(var_name);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if !should_remove {
|
|
|
+ if should_remove {
|
|
|
+ if let Some(var) = matched_var {
|
|
|
+ removed_vars.push(var.to_string());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
new_lines.push(line);
|
|
|
}
|
|
|
}
|
|
|
@@ -517,5 +542,5 @@ fn remove_env_vars_from_file(path: &PathBuf) -> Result<bool, String> {
|
|
|
fs::write(path, new_content).map_err(|e| e.to_string())?;
|
|
|
}
|
|
|
|
|
|
- Ok(modified)
|
|
|
+ Ok((modified, removed_vars))
|
|
|
}
|