自动更新功能
parent
1d5ac78fed
commit
a603248159
@ -0,0 +1,78 @@
|
||||
use serde::Serialize;
|
||||
#[cfg(target_os = "linux")]
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AptUpdateCheckResult {
|
||||
package_name: String,
|
||||
current_version: String,
|
||||
installed_version: String,
|
||||
candidate_version: String,
|
||||
has_update: bool,
|
||||
source_available: bool,
|
||||
update_command: String,
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn extract_policy_value(output: &str, key: &str) -> String {
|
||||
output
|
||||
.lines()
|
||||
.find_map(|line| line.trim().strip_prefix(key).map(|value| value.trim().to_string()))
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or_else(|| "(unknown)".to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn check_apt_update(package_name: String, current_version: String) -> Result<AptUpdateCheckResult, String> {
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
{
|
||||
let _ = (&package_name, ¤t_version);
|
||||
return Err("当前系统不支持 apt 更新检测,仅支持 Linux。".to_string());
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
let output = Command::new("apt-cache")
|
||||
.arg("policy")
|
||||
.arg(&package_name)
|
||||
.output()
|
||||
.map_err(|error| format!("执行 apt-cache 失败: {error}"))?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
|
||||
return Err(if stderr.is_empty() {
|
||||
"apt-cache policy 执行失败".to_string()
|
||||
} else {
|
||||
format!("apt-cache policy 执行失败: {stderr}")
|
||||
});
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
||||
let installed_version = extract_policy_value(&stdout, "Installed:");
|
||||
let candidate_version = extract_policy_value(&stdout, "Candidate:");
|
||||
let baseline_version = if installed_version == "(none)" || installed_version == "(unknown)" {
|
||||
current_version.clone()
|
||||
} else {
|
||||
installed_version.clone()
|
||||
};
|
||||
let source_available = candidate_version != "(none)" && candidate_version != "(unknown)";
|
||||
let has_update = source_available
|
||||
&& candidate_version != baseline_version
|
||||
&& !candidate_version.trim().is_empty();
|
||||
let update_command = format!(
|
||||
"sudo apt update && sudo apt install --only-upgrade {}",
|
||||
package_name
|
||||
);
|
||||
|
||||
Ok(AptUpdateCheckResult {
|
||||
package_name,
|
||||
current_version,
|
||||
installed_version,
|
||||
candidate_version,
|
||||
has_update,
|
||||
source_available,
|
||||
update_command,
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue