diff --git a/coffee_cmd/src/coffee_term/command_show.rs b/coffee_cmd/src/coffee_term/command_show.rs index 28497d8..593a09f 100644 --- a/coffee_cmd/src/coffee_term/command_show.rs +++ b/coffee_cmd/src/coffee_term/command_show.rs @@ -113,19 +113,16 @@ pub fn show_nurse_verify(nurse_verify: &ChainOfResponsibilityStatus) -> Result<( ]); } } - Defect::CoffeeGlobalrepoCleanup(networks) => { - let defect = format!( - "Global repository migration completed for the networks: {}", - networks - .iter() - .map(|(network, _)| format!("{network} ")) - .collect::() - .trim_end() - ); + Defect::CoffeeGlobalRepoCleanup(networks) => { + let defect = "Network specific repository missing"; + let networks = networks + .iter() + .map(|(network, _)| network.clone()) + .collect::>(); table.push([ term::format::positive("●").into(), term::format::bold(defect.to_owned()), - term::format::highlight("_".to_owned()), + term::format::highlight(networks.join(", ")), ]); } } diff --git a/coffee_cmd/src/main.rs b/coffee_cmd/src/main.rs index 5cd7a4f..7e83ae5 100644 --- a/coffee_cmd/src/main.rs +++ b/coffee_cmd/src/main.rs @@ -82,7 +82,7 @@ async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeEr match action { Some(RemoteAction::Add { name, url }) => { let mut spinner = term::spinner(format!("Fetch remote from {url}")); - let result = coffee.add_remote(&name, &url).await; + let result = coffee.add_remote(&name, &url, false).await; if let Err(err) = &result { spinner.error(format!("Error while add remote: {err}")); return result; diff --git a/coffee_core/src/coffee.rs b/coffee_core/src/coffee.rs index 0ed4adf..a750b0d 100644 --- a/coffee_core/src/coffee.rs +++ b/coffee_core/src/coffee.rs @@ -1,7 +1,6 @@ //! Coffee mod implementation use std::collections::HashMap; use std::fmt::Debug; -use std::path::Path; use std::vec::Vec; use async_trait::async_trait; @@ -21,7 +20,7 @@ use coffee_lib::plugin_manager::PluginManager; use coffee_lib::repository::Repository; use coffee_lib::types::response::*; use coffee_lib::url::URL; -use coffee_lib::utils::{check_dir_or_make_if_missing, copy_dir_if_exist, rm_dir_if_exist}; +use coffee_lib::utils::rm_dir_if_exist; use coffee_lib::{commit_id, error, get_repo_info, sh}; use coffee_storage::model::repository::{Kind, Repository as RepositoryInfo}; use coffee_storage::nosql_db::NoSQlStorage; @@ -427,8 +426,8 @@ impl PluginManager for CoffeeManager { Ok(()) } - async fn add_remote(&mut self, name: &str, url: &str) -> Result<(), CoffeeError> { - if self.repos.contains_key(name) { + async fn add_remote(&mut self, name: &str, url: &str, force: bool) -> Result<(), CoffeeError> { + if !force && self.repos.contains_key(name) { return Err(error!("repository with name: {name} already exists")); } let url = URL::new(&self.config.path(), url, name); @@ -544,17 +543,21 @@ impl PluginManager for CoffeeManager { let mut actions = self.patch_repository_locally_absent(repos.to_vec()).await?; nurse_actions.append(&mut actions); } - Defect::CoffeeGlobalrepoCleanup(networks) => { - let global_repo = format!("{}/repositories", self.config.root_path); - for (network, path) in networks { - log::info!("{network} - {path}"); - check_dir_or_make_if_missing(path.to_owned()).await?; - if !Path::exists(Path::new(&path)) { - copy_dir_if_exist(&global_repo, path).await?; + Defect::CoffeeGlobalRepoCleanup(networks) => { + for network in networks { + log::debug!("reindexing repository for the network `{:?}`", network); + let iter = self + .repos + .iter() + .map(|(name, repo)| (name.to_owned(), repo.url())) + .collect::>(); + for (name, url) in iter { + self.add_remote(&name, &url.url_string, true).await?; } nurse_actions - .push(NurseStatus::MovingGlobalRepostoryTo(network.to_owned())); + .push(NurseStatus::MovingGlobalRepostoryTo(network.1.to_owned())); } + let global_repo = format!("{}/repositories", self.config.root_path); rm_dir_if_exist(&global_repo).await?; } } @@ -587,7 +590,6 @@ impl PluginManager for CoffeeManager { .get_mut(repo_name) .ok_or_else(|| error!("repository with name: {repo_name} not found"))?; - repo.change_root_path(&self.config.path()); match repo.recover().await { Ok(_) => { log::info!("repository {} recovered", repo_name.clone()); diff --git a/coffee_core/src/nurse/chain.rs b/coffee_core/src/nurse/chain.rs index a189271..e46f6ed 100644 --- a/coffee_core/src/nurse/chain.rs +++ b/coffee_core/src/nurse/chain.rs @@ -33,7 +33,7 @@ use async_trait::async_trait; use coffee_lib::errors::CoffeeError; use coffee_lib::types::response::{ChainOfResponsibilityStatus, Defect}; -use super::strategy::{CoffeeRepositoryDirCleanUp, GitRepositoryLocallyAbsentStrategy}; +use super::strategy::{CoffeeRepositoryDirCleanUpStrategy, GitRepositoryLocallyAbsentStrategy}; use crate::coffee::CoffeeManager; #[async_trait] @@ -53,7 +53,7 @@ impl RecoveryChainOfResponsibility { pub async fn new() -> Result { Ok(Self { handlers: vec![ - Arc::new(CoffeeRepositoryDirCleanUp), + Arc::new(CoffeeRepositoryDirCleanUpStrategy), Arc::new(GitRepositoryLocallyAbsentStrategy), ], }) diff --git a/coffee_core/src/nurse/strategy.rs b/coffee_core/src/nurse/strategy.rs index 0ecfc55..a30c932 100644 --- a/coffee_core/src/nurse/strategy.rs +++ b/coffee_core/src/nurse/strategy.rs @@ -76,37 +76,30 @@ impl Handler for GitRepositoryLocallyAbsentStrategy { } /// Stategy for migration of the repository global directory -/// to a local directory for each network, see [1] +/// to a local directory for each network. See [related issue] /// -/// This is a strategy tht return the list of network that -/// needs to be migrated to to the new repository configuration. +/// This is a strategy that returns the list of networks that +/// need to be migrated to to the new repository configuration. /// -/// [1] https://github.com/coffee-tools/coffee/issues/234 -pub struct CoffeeRepositoryDirCleanUp; +/// [issue]: https://github.com/coffee-tools/coffee/issues/234 +pub struct CoffeeRepositoryDirCleanUpStrategy; #[async_trait] -impl Handler for CoffeeRepositoryDirCleanUp { +impl Handler for CoffeeRepositoryDirCleanUpStrategy { async fn can_be_applied( self: Arc, coffee: &CoffeeManager, ) -> Result, CoffeeError> { - let networks = ["testnet", "signet", "bitcoin", "liquid"]; + let network = coffee.config.network.clone(); // Check whether there exists a network-specific repositories folder for each network. let mut directory_moving = vec![]; - for network in networks { - let network_dir = format!("{}/{network}", coffee.config.root_path); - if !Path::exists(Path::new(&network_dir)) { - log::debug!("network dir `{network_dir}` not found"); - continue; - } - let subpath_repo = format!("{}/{network}/repositories", coffee.config.root_path); - if !Path::exists(Path::new(&subpath_repo)) { - directory_moving.push((network.to_string(), subpath_repo)); - } + let subpath_repo = format!("{}/{network}/repositories", coffee.config.root_path); + if !Path::exists(Path::new(&subpath_repo)) { + directory_moving.push((network.to_string(), subpath_repo)); } if directory_moving.is_empty() { return Ok(None); } - Ok(Some(Defect::CoffeeGlobalrepoCleanup(directory_moving))) + Ok(Some(Defect::CoffeeGlobalRepoCleanup(directory_moving))) } } diff --git a/coffee_github/src/repository.rs b/coffee_github/src/repository.rs index fa6f7e2..1e6d5ff 100644 --- a/coffee_github/src/repository.rs +++ b/coffee_github/src/repository.rs @@ -233,10 +233,6 @@ impl Repository for Github { } } - fn change_root_path(&mut self, path: &str) { - self.url.set_coffee_path(path); - } - async fn upgrade( &mut self, plugins: &Vec, @@ -343,6 +339,10 @@ impl Repository for Github { fn as_any(&self) -> &dyn Any { self } + + fn plugins(&mut self) -> &mut Vec { + &mut self.plugins + } } impl From for Github { diff --git a/coffee_httpd/src/httpd/server.rs b/coffee_httpd/src/httpd/server.rs index dc429fd..ebfaed0 100644 --- a/coffee_httpd/src/httpd/server.rs +++ b/coffee_httpd/src/httpd/server.rs @@ -130,7 +130,9 @@ async fn coffee_remote_add( let repository_url = &body.repository_url; let mut coffee = data.coffee.lock().await; - let result = coffee.add_remote(repository_name, repository_url).await; + let result = coffee + .add_remote(repository_name, repository_url, false) + .await; handle_httpd_response!(result, "Repository '{repository_name}' added successfully") } diff --git a/coffee_lib/src/plugin_manager.rs b/coffee_lib/src/plugin_manager.rs index 103b77e..78efa0d 100644 --- a/coffee_lib/src/plugin_manager.rs +++ b/coffee_lib/src/plugin_manager.rs @@ -28,7 +28,7 @@ pub trait PluginManager { async fn upgrade(&mut self, repo: &str, verbose: bool) -> Result; /// add the remote repository to the plugin manager. - async fn add_remote(&mut self, name: &str, url: &str) -> Result<(), CoffeeError>; + async fn add_remote(&mut self, name: &str, url: &str, force: bool) -> Result<(), CoffeeError>; /// remove the remote repository from the plugin manager. async fn rm_remote(&mut self, name: &str) -> Result<(), CoffeeError>; diff --git a/coffee_lib/src/repository.rs b/coffee_lib/src/repository.rs index 586a51d..ffa7ee8 100644 --- a/coffee_lib/src/repository.rs +++ b/coffee_lib/src/repository.rs @@ -34,11 +34,6 @@ pub trait Repository: Any { /// recover the repository from the commit id. async fn recover(&mut self) -> Result<(), CoffeeError>; - /// While migrating there is a possibility that we should - /// move an old repository into a new path. So this - /// is semplyfing this process. - fn change_root_path(&mut self, path: &str); - /// return the name of the repository. fn name(&self) -> String; @@ -46,4 +41,8 @@ pub trait Repository: Any { fn url(&self) -> URL; fn as_any(&self) -> &dyn Any; + + /// Return the vector of plugin + /// that are inside the repository + fn plugins(&mut self) -> &mut Vec; } diff --git a/coffee_lib/src/types/mod.rs b/coffee_lib/src/types/mod.rs index ba33b74..e266ef0 100644 --- a/coffee_lib/src/types/mod.rs +++ b/coffee_lib/src/types/mod.rs @@ -151,7 +151,7 @@ pub mod response { // but is absent from the local storage. RepositoryLocallyAbsent(Vec), /// (Affected network, path) - CoffeeGlobalrepoCleanup(Vec<(String, String)>), + CoffeeGlobalRepoCleanup(Vec<(String, String)>), // TODO: Add more patch operations } diff --git a/coffee_plugin/src/plugin/plugin_mod.rs b/coffee_plugin/src/plugin/plugin_mod.rs index 4f3fa40..9ad7923 100644 --- a/coffee_plugin/src/plugin/plugin_mod.rs +++ b/coffee_plugin/src/plugin/plugin_mod.rs @@ -119,7 +119,11 @@ fn coffee_remote(plugin: &mut Plugin, request: Value) -> Result coffee.add_remote(&request.name, &request.url()).await, + RemoteCmd::Add => { + coffee + .add_remote(&request.name, &request.url(), false) + .await + } RemoteCmd::Rm => coffee.rm_remote(&request.name).await, } })