添加Claude和Codex环境变量检查 (#242)
* feat(env): add environment variable conflict detection and management 实现了系统环境变量冲突检测与管理功能: 核心功能: - 自动检测会影响 Claude/Codex 的系统环境变量 - 支持 Windows 注册表和 Unix shell 配置文件检测 - 提供可视化的环境变量冲突警告横幅 - 支持批量选择和删除环境变量 - 删除前自动备份,支持后续恢复 技术实现: - Rust 后端: 跨平台环境变量检测与管理 - React 前端: EnvWarningBanner 组件交互界面 - 国际化支持: 中英文界面 - 类型安全: 完整的 TypeScript 类型定义 * refactor(env): remove unused imports and function Remove unused HashMap and PathBuf imports, and delete the unused get_source_description function to clean up the code.
This commit is contained in:
161
src-tauri/src/services/env_checker.rs
Normal file
161
src-tauri/src/services/env_checker.rs
Normal file
@@ -0,0 +1,161 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct EnvConflict {
|
||||
pub var_name: String,
|
||||
pub var_value: String,
|
||||
pub source_type: String, // "system" | "file"
|
||||
pub source_path: String, // Registry path or file path
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
use winreg::enums::*;
|
||||
#[cfg(target_os = "windows")]
|
||||
use winreg::RegKey;
|
||||
|
||||
/// Check environment variables for conflicts
|
||||
pub fn check_env_conflicts(app: &str) -> Result<Vec<EnvConflict>, String> {
|
||||
let keywords = get_keywords_for_app(app);
|
||||
let mut conflicts = Vec::new();
|
||||
|
||||
// Check system environment variables
|
||||
conflicts.extend(check_system_env(&keywords)?);
|
||||
|
||||
// Check shell configuration files (Unix only)
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
conflicts.extend(check_shell_configs(&keywords)?);
|
||||
|
||||
Ok(conflicts)
|
||||
}
|
||||
|
||||
/// Get relevant keywords for each app
|
||||
fn get_keywords_for_app(app: &str) -> Vec<&str> {
|
||||
match app.to_lowercase().as_str() {
|
||||
"claude" => vec!["ANTHROPIC"],
|
||||
"codex" => vec!["OPENAI"],
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// Check system environment variables (Windows Registry or Unix env)
|
||||
#[cfg(target_os = "windows")]
|
||||
fn check_system_env(keywords: &[&str]) -> Result<Vec<EnvConflict>, String> {
|
||||
let mut conflicts = Vec::new();
|
||||
|
||||
// Check HKEY_CURRENT_USER\Environment
|
||||
if let Ok(hkcu) = RegKey::predef(HKEY_CURRENT_USER).open_subkey("Environment") {
|
||||
for (name, value) in hkcu.enum_values().filter_map(Result::ok) {
|
||||
if keywords.iter().any(|k| name.to_uppercase().contains(k)) {
|
||||
if let Ok(val) = value.to_string() {
|
||||
conflicts.push(EnvConflict {
|
||||
var_name: name.clone(),
|
||||
var_value: val,
|
||||
source_type: "system".to_string(),
|
||||
source_path: "HKEY_CURRENT_USER\\Environment".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
|
||||
if let Ok(hklm) = RegKey::predef(HKEY_LOCAL_MACHINE)
|
||||
.open_subkey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment")
|
||||
{
|
||||
for (name, value) in hklm.enum_values().filter_map(Result::ok) {
|
||||
if keywords.iter().any(|k| name.to_uppercase().contains(k)) {
|
||||
if let Ok(val) = value.to_string() {
|
||||
conflicts.push(EnvConflict {
|
||||
var_name: name.clone(),
|
||||
var_value: val,
|
||||
source_type: "system".to_string(),
|
||||
source_path: "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(conflicts)
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
fn check_system_env(keywords: &[&str]) -> Result<Vec<EnvConflict>, String> {
|
||||
let mut conflicts = Vec::new();
|
||||
|
||||
// Check current process environment
|
||||
for (key, value) in std::env::vars() {
|
||||
if keywords.iter().any(|k| key.to_uppercase().contains(k)) {
|
||||
conflicts.push(EnvConflict {
|
||||
var_name: key,
|
||||
var_value: value,
|
||||
source_type: "system".to_string(),
|
||||
source_path: "Process Environment".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(conflicts)
|
||||
}
|
||||
|
||||
/// Check shell configuration files for environment variable exports (Unix only)
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
fn check_shell_configs(keywords: &[&str]) -> Result<Vec<EnvConflict>, String> {
|
||||
let mut conflicts = Vec::new();
|
||||
|
||||
let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".to_string());
|
||||
let config_files = vec![
|
||||
format!("{}/.bashrc", home),
|
||||
format!("{}/.bash_profile", home),
|
||||
format!("{}/.zshrc", home),
|
||||
format!("{}/.zprofile", home),
|
||||
format!("{}/.profile", home),
|
||||
"/etc/profile".to_string(),
|
||||
"/etc/bashrc".to_string(),
|
||||
];
|
||||
|
||||
for file_path in config_files {
|
||||
if let Ok(content) = fs::read_to_string(&file_path) {
|
||||
// Parse lines for export statements
|
||||
for (line_num, line) in content.lines().enumerate() {
|
||||
let trimmed = line.trim();
|
||||
|
||||
// Match patterns like: export VAR=value or VAR=value
|
||||
if trimmed.starts_with("export ") || (!trimmed.starts_with('#') && trimmed.contains('=')) {
|
||||
let export_line = trimmed.strip_prefix("export ").unwrap_or(trimmed);
|
||||
|
||||
if let Some(eq_pos) = export_line.find('=') {
|
||||
let var_name = export_line[..eq_pos].trim();
|
||||
let var_value = export_line[eq_pos + 1..].trim();
|
||||
|
||||
// Check if variable name contains any keyword
|
||||
if keywords.iter().any(|k| var_name.to_uppercase().contains(k)) {
|
||||
conflicts.push(EnvConflict {
|
||||
var_name: var_name.to_string(),
|
||||
var_value: var_value.trim_matches('"').trim_matches('\'').to_string(),
|
||||
source_type: "file".to_string(),
|
||||
source_path: format!("{}:{}", file_path, line_num + 1),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(conflicts)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_keywords() {
|
||||
assert_eq!(get_keywords_for_app("claude"), vec!["ANTHROPIC"]);
|
||||
assert_eq!(get_keywords_for_app("codex"), vec!["OPENAI"]);
|
||||
assert_eq!(get_keywords_for_app("unknown"), Vec::<&str>::new());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user