use colored::Colorize; use std::io::{self, Write}; use crate::config::{Config, RepositoryConfig}; pub fn add_repository(name: &str, url: &str, mirror: Option<&str>) -> Result<(), String> { let mut cfg = Config::load(); if cfg.repositories.contains_key(name) { return Err(format!("Repository '{}' already exists", name)); } print!( "{} Enable GPG verification for '{}'? [Y/n]: ", "[ror]".blue().bold(), name ); io::stdout().flush().ok(); let mut answer = String::new(); io::stdin().read_line(&mut answer).ok(); let gpg_support = !answer.trim().eq_ignore_ascii_case("n"); if !gpg_support { print!( "{} Repository '{}' will have GPG disabled. It may be unsafe. Continue? [y/N]: ", "[ror]".yellow().bold(), name ); io::stdout().flush().ok(); let mut confirm = String::new(); io::stdin().read_line(&mut confirm).ok(); if !confirm.trim().eq_ignore_ascii_case("y") { println!("{} Aborted.", "[ror]".yellow()); return Ok(()); } } let gpg_key = if gpg_support { print!("{} GPG key filename (e.g. rorka.pub): ", "[ror]".blue().bold()); io::stdout().flush().ok(); let mut key = String::new(); io::stdin().read_line(&mut key).ok(); let key = key.trim().to_string(); if key.is_empty() { None } else { Some(key) } } else { None }; let repo = RepositoryConfig { url: url.to_string(), urlrepo: None, mirror: mirror.map(String::from), gpg_support, gpg_key, }; cfg.repositories.insert(name.to_string(), repo); cfg.save().map_err(|e| format!("Failed to save config: {}", e))?; println!("{} Repository '{}' added.", "[ror]".green().bold(), name); Ok(()) } pub fn remove_repository(name: &str) -> Result<(), String> { let mut cfg = Config::load(); if !cfg.repositories.contains_key(name) { return Err(format!("Repository '{}' not found", name)); } cfg.repositories.remove(name); cfg.save().map_err(|e| format!("Failed to save config: {}", e))?; println!("{} Repository '{}' removed.", "[ror]".green().bold(), name); Ok(()) } pub fn list_repositories() { let cfg = Config::load(); if cfg.repositories.is_empty() { println!("{} No repositories configured.", "[ror]".yellow()); return; } println!("{} Configured repositories:", "[ror]".blue().bold()); for (name, repo) in &cfg.repositories { println!(" {} {}", name.green(), repo.url); if let Some(m) = &repo.mirror { println!(" mirror: {}", m); } if repo.gpg_support { let key = repo.gpg_key.as_deref().unwrap_or("none"); println!(" gpg: {} (key: {})", "enabled".green(), key.cyan()); } else { println!(" gpg: {}", "disabled".red().bold()); } } }