|
|
@@ -296,38 +296,55 @@ pub async fn delete_claude_env_vars() -> CommandResult {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/// Windows 平台删除环境变量
|
|
|
+/// Windows 平台删除环境变量(用户级和系统级)
|
|
|
#[cfg(target_os = "windows")]
|
|
|
fn delete_claude_env_vars_windows() -> CommandResult {
|
|
|
- let hkcu = RegKey::predef(HKEY_CURRENT_USER);
|
|
|
-
|
|
|
- let env_key = match hkcu.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) {
|
|
|
- Ok(key) => key,
|
|
|
- Err(e) => {
|
|
|
- return CommandResult {
|
|
|
- success: false,
|
|
|
- error: Some(format!("Failed to open registry: {}", e)),
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
let mut deleted_vars = Vec::new();
|
|
|
let mut errors = Vec::new();
|
|
|
|
|
|
- for var_name in CLAUDE_ENV_VARS {
|
|
|
- // 检查环境变量是否存在
|
|
|
- if env_key.get_value::<String, _>(*var_name).is_ok() {
|
|
|
- match env_key.delete_value(*var_name) {
|
|
|
- Ok(_) => {
|
|
|
- deleted_vars.push(*var_name);
|
|
|
- log::info!("Deleted environment variable: {}", var_name);
|
|
|
+ // 删除用户级环境变量 (HKEY_CURRENT_USER\Environment)
|
|
|
+ let hkcu = RegKey::predef(HKEY_CURRENT_USER);
|
|
|
+ if let Ok(user_env_key) = hkcu.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE) {
|
|
|
+ for var_name in CLAUDE_ENV_VARS {
|
|
|
+ if user_env_key.get_value::<String, _>(*var_name).is_ok() {
|
|
|
+ match user_env_key.delete_value(*var_name) {
|
|
|
+ Ok(_) => {
|
|
|
+ deleted_vars.push(format!("用户级: {}", var_name));
|
|
|
+ log::info!("Deleted user environment variable: {}", var_name);
|
|
|
+ }
|
|
|
+ Err(e) => {
|
|
|
+ errors.push(format!("用户级 {}: {}", var_name, e));
|
|
|
+ log::error!("Failed to delete user env {}: {}", var_name, e);
|
|
|
+ }
|
|
|
}
|
|
|
- Err(e) => {
|
|
|
- errors.push(format!("{}: {}", var_name, e));
|
|
|
- log::error!("Failed to delete {}: {}", var_name, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log::warn!("Failed to open user environment registry key");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除系统级环境变量 (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment)
|
|
|
+ let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
|
|
|
+ if let Ok(system_env_key) = hklm.open_subkey_with_flags(
|
|
|
+ r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment",
|
|
|
+ KEY_READ | KEY_WRITE,
|
|
|
+ ) {
|
|
|
+ for var_name in CLAUDE_ENV_VARS {
|
|
|
+ if system_env_key.get_value::<String, _>(*var_name).is_ok() {
|
|
|
+ match system_env_key.delete_value(*var_name) {
|
|
|
+ Ok(_) => {
|
|
|
+ deleted_vars.push(format!("系统级: {}", var_name));
|
|
|
+ log::info!("Deleted system environment variable: {}", var_name);
|
|
|
+ }
|
|
|
+ Err(e) => {
|
|
|
+ // 系统级环境变量删除失败通常是权限问题,记录但不作为错误
|
|
|
+ log::warn!("Failed to delete system env {} (may need admin): {}", var_name, e);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ } else {
|
|
|
+ log::info!("Cannot open system environment registry (may need admin privileges)");
|
|
|
}
|
|
|
|
|
|
// 通知系统环境变量已更改
|
|
|
@@ -350,16 +367,9 @@ fn delete_claude_env_vars_windows() -> CommandResult {
|
|
|
}
|
|
|
|
|
|
if errors.is_empty() {
|
|
|
- if deleted_vars.is_empty() {
|
|
|
- CommandResult {
|
|
|
- success: true,
|
|
|
- error: None,
|
|
|
- }
|
|
|
- } else {
|
|
|
- CommandResult {
|
|
|
- success: true,
|
|
|
- error: None,
|
|
|
- }
|
|
|
+ CommandResult {
|
|
|
+ success: true,
|
|
|
+ error: None,
|
|
|
}
|
|
|
} else {
|
|
|
CommandResult {
|
|
|
@@ -369,44 +379,87 @@ fn delete_claude_env_vars_windows() -> CommandResult {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/// Unix 平台删除环境变量(macOS/Linux)
|
|
|
+/// Unix 平台删除环境变量(macOS/Linux)- 用户级和系统级
|
|
|
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
|
|
fn delete_claude_env_vars_unix() -> CommandResult {
|
|
|
- // 在 Unix 系统上,环境变量通常存储在 shell 配置文件中
|
|
|
- // 我们需要从这些文件中移除相关的 export 语句
|
|
|
- let home = match dirs::home_dir() {
|
|
|
- Some(h) => h,
|
|
|
- None => {
|
|
|
- return CommandResult {
|
|
|
- success: false,
|
|
|
- error: Some("Cannot determine home directory".to_string()),
|
|
|
+ let mut modified_files = Vec::new();
|
|
|
+ let mut errors = Vec::new();
|
|
|
+
|
|
|
+ // 1. 删除用户级环境变量(用户 home 目录下的配置文件)
|
|
|
+ if let Some(home) = dirs::home_dir() {
|
|
|
+ let user_configs = [
|
|
|
+ ".bashrc",
|
|
|
+ ".bash_profile",
|
|
|
+ ".zshrc",
|
|
|
+ ".zprofile",
|
|
|
+ ".profile",
|
|
|
+ ];
|
|
|
+
|
|
|
+ for config_file in user_configs {
|
|
|
+ let config_path = home.join(config_file);
|
|
|
+ if config_path.exists() {
|
|
|
+ match remove_env_vars_from_file(&config_path) {
|
|
|
+ Ok(modified) => {
|
|
|
+ if modified {
|
|
|
+ modified_files.push(format!("用户级: ~/{}", config_file));
|
|
|
+ log::info!("Modified user config: {}", config_file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Err(e) => {
|
|
|
+ errors.push(format!("用户级 {}: {}", config_file, e));
|
|
|
+ log::error!("Failed to modify user config {}: {}", config_file, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- };
|
|
|
+ } else {
|
|
|
+ log::warn!("Cannot determine home directory for user-level env vars");
|
|
|
+ }
|
|
|
|
|
|
- let shell_configs = [
|
|
|
- ".bashrc",
|
|
|
- ".bash_profile",
|
|
|
- ".zshrc",
|
|
|
- ".profile",
|
|
|
+ // 2. 删除系统级环境变量(需要 root/sudo 权限)
|
|
|
+ let system_configs = [
|
|
|
+ "/etc/environment",
|
|
|
+ "/etc/profile",
|
|
|
+ "/etc/bash.bashrc", // Linux
|
|
|
+ "/etc/zshrc", // macOS/Linux
|
|
|
];
|
|
|
|
|
|
- let mut modified_files = Vec::new();
|
|
|
- let mut errors = Vec::new();
|
|
|
-
|
|
|
- for config_file in shell_configs {
|
|
|
- let config_path = home.join(config_file);
|
|
|
+ for config_file in system_configs {
|
|
|
+ let config_path = PathBuf::from(config_file);
|
|
|
if config_path.exists() {
|
|
|
match remove_env_vars_from_file(&config_path) {
|
|
|
Ok(modified) => {
|
|
|
if modified {
|
|
|
- modified_files.push(config_file);
|
|
|
- log::info!("Modified shell config: {}", config_file);
|
|
|
+ modified_files.push(format!("系统级: {}", config_file));
|
|
|
+ log::info!("Modified system config: {}", config_file);
|
|
|
}
|
|
|
}
|
|
|
Err(e) => {
|
|
|
- errors.push(format!("{}: {}", config_file, e));
|
|
|
- log::error!("Failed to modify {}: {}", config_file, e);
|
|
|
+ // 系统级文件修改失败通常是权限问题,记录但不作为错误
|
|
|
+ log::warn!("Failed to modify system config {} (may need sudo): {}", config_file, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 检查 /etc/profile.d/ 目录下的脚本
|
|
|
+ let profile_d = PathBuf::from("/etc/profile.d");
|
|
|
+ if profile_d.exists() && profile_d.is_dir() {
|
|
|
+ if let Ok(entries) = fs::read_dir(&profile_d) {
|
|
|
+ for entry in entries.flatten() {
|
|
|
+ let path = entry.path();
|
|
|
+ if path.extension().map_or(false, |ext| ext == "sh") {
|
|
|
+ match remove_env_vars_from_file(&path) {
|
|
|
+ Ok(modified) => {
|
|
|
+ if modified {
|
|
|
+ modified_files.push(format!("系统级: {}", path.display()));
|
|
|
+ log::info!("Modified profile.d script: {}", path.display());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Err(e) => {
|
|
|
+ log::warn!("Failed to modify {} (may need sudo): {}", path.display(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|