Skip to content

Commit

Permalink
feat(core): nurse clean up the global repository
Browse files Browse the repository at this point in the history
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 <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Feb 7, 2024
1 parent 472d9f6 commit 7bace56
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions coffee_core/src/coffee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions coffee_core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions coffee_core/src/nurse/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,3 +75,30 @@ impl Handler for GitRepositoryLocallyAbsentStrategy {
}
}
}

pub struct CoffeeRepositoryDirCleanUp;

#[async_trait]
impl Handler for CoffeeRepositoryDirCleanUp {
async fn can_be_applied(
self: Arc<Self>,
coffee: &CoffeeManager,
) -> Result<Option<Defect>, 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)))
}
}
8 changes: 8 additions & 0 deletions coffee_lib/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>),
CoffeeGlobalrepoCleanup(Vec<String>),
// TODO: Add more patch operations
}

Expand Down Expand Up @@ -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::<String>()
)?;
}
}
}
Ok(())
Expand Down

0 comments on commit 7bace56

Please sign in to comment.