🚀 Implement real logic for refresh-md and cleanup commands - CLI parity with rpm-ostree achieved!

This commit is contained in:
robojerk 2025-08-18 19:35:04 -07:00
parent edd89aed05
commit e265eda14c
3 changed files with 545 additions and 11 deletions

View file

@ -1640,12 +1640,15 @@ impl Command for RefreshMdCommand {
return Ok(());
}
// Parse options
// Parse options from CLI structure
let mut opt_force = false;
let mut opt_verbose = false;
// Check for force flag in args
for arg in args {
match arg.as_str() {
"--force" | "-f" => opt_force = true,
"--verbose" | "-v" => opt_verbose = true,
_ => {}
}
}
@ -1665,7 +1668,140 @@ impl Command for RefreshMdCommand {
println!("Refreshing package repository metadata...");
// TODO: Implement real metadata refresh logic when daemon is ready
// Use the real APT manager for metadata refresh
let apt_manager = AptManager::new();
// Check if APT is available and healthy
if !apt_manager.check_database_health()? {
return Err(AptOstreeError::System("APT database is not healthy".to_string()));
}
// Force refresh if requested
if opt_force {
println!("Forcing metadata refresh and expiring cache...");
// Clear APT cache
if let Err(e) = std::process::Command::new("apt-get")
.arg("clean")
.output() {
println!("Warning: Failed to clean APT cache: {}", e);
}
// Remove package lists
if let Err(e) = std::process::Command::new("rm")
.arg("-rf")
.arg("/var/lib/apt/lists/*")
.output() {
println!("Warning: Failed to remove package lists: {}", e);
}
}
// Update APT package lists
println!("Updating APT package lists...");
match apt_manager.update_cache() {
Ok(_) => println!("✅ APT package lists updated successfully"),
Err(e) => {
println!("❌ Failed to update APT package lists: {}", e);
return Err(e);
}
}
// Get repository information
println!("Repository information:");
let sources_list = std::path::Path::new("/etc/apt/sources.list");
let sources_list_d = std::path::Path::new("/etc/apt/sources.list.d");
let mut repo_count = 0;
// Read main sources.list
if sources_list.exists() {
if let Ok(content) = std::fs::read_to_string(sources_list) {
for line in content.lines() {
if line.trim().starts_with("deb ") && !line.trim().starts_with("#") {
repo_count += 1;
if opt_verbose {
println!(" {}. {}", repo_count, line.trim());
}
}
}
}
}
// Read sources.list.d files
if sources_list_d.exists() {
if let Ok(entries) = std::fs::read_dir(sources_list_d) {
for entry in entries.flatten() {
if let Some(ext) = entry.path().extension() {
if ext == "list" {
if let Ok(content) = std::fs::read_to_string(entry.path()) {
for line in content.lines() {
if line.trim().starts_with("deb ") && !line.trim().starts_with("#") {
repo_count += 1;
if opt_verbose {
println!(" {}. {}", repo_count, line.trim());
}
}
}
}
}
}
}
}
}
println!(" Active repositories: {}", repo_count);
// Check for available updates
println!("Checking for available updates...");
// Use apt list --upgradable to check for available updates
let apt_output = std::process::Command::new("apt")
.arg("list")
.arg("--upgradable")
.output();
match apt_output {
Ok(output) if output.status.success() => {
let output_str = String::from_utf8_lossy(&output.stdout);
let lines: Vec<&str> = output_str.lines().collect();
if lines.len() <= 1 { // Only header line
println!("✅ No APT package updates available");
} else {
let upgradeable_count = lines.len() - 1; // Subtract header
println!("📦 {} packages can be upgraded", upgradeable_count);
if opt_verbose {
for line in lines.iter().skip(1).take(10) {
if line.contains('/') {
let parts: Vec<&str> = line.split('/').collect();
if parts.len() >= 2 {
let package_name = parts[0];
let version_info = parts[1];
println!(" - {} ({})", package_name, version_info);
}
}
}
if upgradeable_count > 10 {
println!(" ... and {} more packages", upgradeable_count - 10);
}
}
}
}
Ok(_) => {
println!("⚠ Could not check APT package updates");
}
Err(_) => {
println!("⚠ Could not check APT package updates (apt command not available)");
}
}
// Refresh OSTree repository metadata if available
if let Ok(repo_info) = ostree_manager.get_repo_info() {
println!("OSTree repository: {} references available", repo_info.refs.len());
}
if opt_force {
println!("✅ Package metadata refreshed successfully (forced)");
} else {
@ -1690,6 +1826,7 @@ impl Command for RefreshMdCommand {
println!();
println!("Options:");
println!(" --force, -f Expire current cache and force refresh");
println!(" --verbose, -v Show detailed output");
println!(" --help, -h Show this help message");
println!();
println!("Examples:");