|
| 1 | +use std::fs; |
| 2 | + |
| 3 | +use soar_config::{ |
| 4 | + config::{Config, CONFIG}, |
| 5 | + repository::Repository, |
| 6 | +}; |
| 7 | +use soar_core::{error::ErrorContext, SoarResult}; |
| 8 | + |
| 9 | +use crate::SoarContext; |
| 10 | + |
| 11 | +/// Fields that can be updated on an existing repository. |
| 12 | +pub struct RepoUpdate { |
| 13 | + pub url: Option<String>, |
| 14 | + pub enabled: Option<bool>, |
| 15 | + pub pubkey: Option<String>, |
| 16 | + pub desktop_integration: Option<bool>, |
| 17 | + pub signature_verification: Option<bool>, |
| 18 | + pub sync_interval: Option<String>, |
| 19 | +} |
| 20 | + |
| 21 | +/// Loads a fresh config from disk, applies the mutation, validates, saves, and updates the global. |
| 22 | +fn modify_config(mutate: impl FnOnce(&mut Config) -> SoarResult<()>) -> SoarResult<()> { |
| 23 | + let mut config = Config::new()?; |
| 24 | + mutate(&mut config)?; |
| 25 | + config.save()?; |
| 26 | + |
| 27 | + let mut global = CONFIG.write()?; |
| 28 | + *global = Some(config); |
| 29 | + Ok(()) |
| 30 | +} |
| 31 | + |
| 32 | +impl SoarContext { |
| 33 | + /// Add a new repository to the configuration. |
| 34 | + pub fn add_repository(&self, repo: Repository) -> SoarResult<()> { |
| 35 | + modify_config(|config| { |
| 36 | + if config.repositories.iter().any(|r| r.name == repo.name) { |
| 37 | + return Err(soar_config::error::ConfigError::DuplicateRepositoryName( |
| 38 | + repo.name.clone(), |
| 39 | + ) |
| 40 | + .into()); |
| 41 | + } |
| 42 | + |
| 43 | + config.repositories.push(repo); |
| 44 | + config.resolve()?; |
| 45 | + Ok(()) |
| 46 | + }) |
| 47 | + } |
| 48 | + |
| 49 | + /// Update an existing repository's configuration. |
| 50 | + pub fn update_repository(&self, name: &str, update: RepoUpdate) -> SoarResult<()> { |
| 51 | + modify_config(|config| { |
| 52 | + let repo = config |
| 53 | + .repositories |
| 54 | + .iter_mut() |
| 55 | + .find(|r| r.name == name) |
| 56 | + .ok_or_else(|| { |
| 57 | + soar_config::error::ConfigError::InvalidRepository(name.to_string()) |
| 58 | + })?; |
| 59 | + |
| 60 | + if let Some(url) = update.url { |
| 61 | + repo.url = url; |
| 62 | + } |
| 63 | + if let Some(enabled) = update.enabled { |
| 64 | + repo.enabled = Some(enabled); |
| 65 | + } |
| 66 | + if let Some(pubkey) = update.pubkey { |
| 67 | + repo.pubkey = Some(pubkey); |
| 68 | + } |
| 69 | + if let Some(desktop_integration) = update.desktop_integration { |
| 70 | + repo.desktop_integration = Some(desktop_integration); |
| 71 | + } |
| 72 | + if let Some(signature_verification) = update.signature_verification { |
| 73 | + repo.signature_verification = Some(signature_verification); |
| 74 | + } |
| 75 | + if let Some(sync_interval) = update.sync_interval { |
| 76 | + repo.sync_interval = Some(sync_interval); |
| 77 | + } |
| 78 | + |
| 79 | + config.resolve()?; |
| 80 | + Ok(()) |
| 81 | + }) |
| 82 | + } |
| 83 | + |
| 84 | + /// Remove a repository from the configuration and clean up its data. |
| 85 | + pub fn remove_repository(&self, name: &str) -> SoarResult<()> { |
| 86 | + modify_config(|config| { |
| 87 | + let idx = config |
| 88 | + .repositories |
| 89 | + .iter() |
| 90 | + .position(|r| r.name == name) |
| 91 | + .ok_or_else(|| { |
| 92 | + soar_config::error::ConfigError::InvalidRepository(name.to_string()) |
| 93 | + })?; |
| 94 | + |
| 95 | + let repo = config.repositories.remove(idx); |
| 96 | + |
| 97 | + // Clean up the repository's data directory |
| 98 | + if let Ok(repo_path) = repo.get_path() { |
| 99 | + if repo_path.exists() { |
| 100 | + fs::remove_dir_all(&repo_path).with_context(|| { |
| 101 | + format!("removing repository data at {}", repo_path.display()) |
| 102 | + })?; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + Ok(()) |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
0 commit comments