From 7bace56cd139eb7207ea9d2b023f68b654d6c4e2 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Wed, 7 Feb 2024 20:08:50 +0100 Subject: [PATCH] feat(core): nurse clean up the global repository Adding a new case for the coffe nurse command to clean up the global repository directory and move it insied the network subdirectory. Signed-off-by: Vincenzo Palazzo --- coffee_core/src/coffee.rs | 2 ++ coffee_core/src/config.rs | 2 ++ coffee_core/src/nurse/strategy.rs | 28 ++++++++++++++++++++++++++++ coffee_lib/src/types/mod.rs | 8 ++++++++ 4 files changed, 40 insertions(+) diff --git a/coffee_core/src/coffee.rs b/coffee_core/src/coffee.rs index c60244de..981d4572 100644 --- a/coffee_core/src/coffee.rs +++ b/coffee_core/src/coffee.rs @@ -497,6 +497,8 @@ impl PluginManager for CoffeeManager { let mut actions = self.patch_repository_locally_absent(repos.to_vec()).await?; nurse_actions.append(&mut actions); } + // FIXME: move the code of the migration here and not inside the strategy + Defect::CoffeeGlobalrepoCleanup(_) => {} } } let mut nurse = CoffeeNurse { diff --git a/coffee_core/src/config.rs b/coffee_core/src/config.rs index bd43eb1e..78f76944 100644 --- a/coffee_core/src/config.rs +++ b/coffee_core/src/config.rs @@ -66,7 +66,9 @@ impl CoffeeConf { check_dir_or_make_if_missing(format!("{def_path}/{}", coffee.network)).await?; check_dir_or_make_if_missing(format!("{def_path}/{}/plugins", coffee.network)).await?; let repo_dir = format!("{def_path}/{}/repositories", coffee.network); + // older version of coffee has a repository inside the directory move_dir_if_exist(&format!("{def_path}/repositories"), &repo_dir).await?; + // FIXME: nurse should clean up the `{def_path}/repositories`. check_dir_or_make_if_missing(repo_dir).await?; // after we know all the information regarding // the configuration we try to see if there is diff --git a/coffee_core/src/nurse/strategy.rs b/coffee_core/src/nurse/strategy.rs index a1e85d4d..343e73ed 100644 --- a/coffee_core/src/nurse/strategy.rs +++ b/coffee_core/src/nurse/strategy.rs @@ -28,6 +28,7 @@ use async_trait::async_trait; use coffee_lib::errors::CoffeeError; use coffee_lib::types::response::Defect; +use coffee_lib::utils::move_dir_if_exist; use crate::coffee::CoffeeManager; use crate::nurse::chain::Handler; @@ -74,3 +75,30 @@ impl Handler for GitRepositoryLocallyAbsentStrategy { } } } + +pub struct CoffeeRepositoryDirCleanUp; + +#[async_trait] +impl Handler for CoffeeRepositoryDirCleanUp { + async fn can_be_applied( + self: Arc, + coffee: &CoffeeManager, + ) -> Result, CoffeeError> { + let root_path_repo = format!("{}/repositories", coffee.config.root_path); + let networks = ["testnet", "signet", "bitcoin", "liquid"]; + // check if the repository subdirectory has the repository directory + // inside the subdirectory. + let mut directory_moving = vec![]; + for network in networks { + let subpath_repo = format!("{}/{network}/repositories", coffee.config.root_path); + if !Path::exists(&Path::new(&subpath_repo)) { + move_dir_if_exist(&root_path_repo, &subpath_repo).await?; + directory_moving.push(network.to_string()); + } + } + if directory_moving.is_empty() { + return Ok(None); + } + Ok(Some(Defect::CoffeeGlobalrepoCleanup(directory_moving))) + } +} diff --git a/coffee_lib/src/types/mod.rs b/coffee_lib/src/types/mod.rs index 794dc53a..5242d6a8 100644 --- a/coffee_lib/src/types/mod.rs +++ b/coffee_lib/src/types/mod.rs @@ -138,6 +138,7 @@ pub mod response { // A patch operation when a git repository is present in the coffee configuration // but is absent from the local storage. RepositoryLocallyAbsent(Vec), + CoffeeGlobalrepoCleanup(Vec), // TODO: Add more patch operations } @@ -166,6 +167,13 @@ pub mod response { write!(f, " {}", repo)?; } } + Defect::CoffeeGlobalrepoCleanup(networks) => { + writeln!( + f, + "Global repository migration completed for the networks: {}", + networks.iter().map(String::from).collect::() + )?; + } } } Ok(())