Skip to content

Commit 7bace56

Browse files
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 <[email protected]>
1 parent 472d9f6 commit 7bace56

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

coffee_core/src/coffee.rs

+2
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,8 @@ impl PluginManager for CoffeeManager {
497497
let mut actions = self.patch_repository_locally_absent(repos.to_vec()).await?;
498498
nurse_actions.append(&mut actions);
499499
}
500+
// FIXME: move the code of the migration here and not inside the strategy
501+
Defect::CoffeeGlobalrepoCleanup(_) => {}
500502
}
501503
}
502504
let mut nurse = CoffeeNurse {

coffee_core/src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ impl CoffeeConf {
6666
check_dir_or_make_if_missing(format!("{def_path}/{}", coffee.network)).await?;
6767
check_dir_or_make_if_missing(format!("{def_path}/{}/plugins", coffee.network)).await?;
6868
let repo_dir = format!("{def_path}/{}/repositories", coffee.network);
69+
// older version of coffee has a repository inside the directory
6970
move_dir_if_exist(&format!("{def_path}/repositories"), &repo_dir).await?;
71+
// FIXME: nurse should clean up the `{def_path}/repositories`.
7072
check_dir_or_make_if_missing(repo_dir).await?;
7173
// after we know all the information regarding
7274
// the configuration we try to see if there is

coffee_core/src/nurse/strategy.rs

+28
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use async_trait::async_trait;
2828

2929
use coffee_lib::errors::CoffeeError;
3030
use coffee_lib::types::response::Defect;
31+
use coffee_lib::utils::move_dir_if_exist;
3132

3233
use crate::coffee::CoffeeManager;
3334
use crate::nurse::chain::Handler;
@@ -74,3 +75,30 @@ impl Handler for GitRepositoryLocallyAbsentStrategy {
7475
}
7576
}
7677
}
78+
79+
pub struct CoffeeRepositoryDirCleanUp;
80+
81+
#[async_trait]
82+
impl Handler for CoffeeRepositoryDirCleanUp {
83+
async fn can_be_applied(
84+
self: Arc<Self>,
85+
coffee: &CoffeeManager,
86+
) -> Result<Option<Defect>, CoffeeError> {
87+
let root_path_repo = format!("{}/repositories", coffee.config.root_path);
88+
let networks = ["testnet", "signet", "bitcoin", "liquid"];
89+
// check if the repository subdirectory has the repository directory
90+
// inside the subdirectory.
91+
let mut directory_moving = vec![];
92+
for network in networks {
93+
let subpath_repo = format!("{}/{network}/repositories", coffee.config.root_path);
94+
if !Path::exists(&Path::new(&subpath_repo)) {
95+
move_dir_if_exist(&root_path_repo, &subpath_repo).await?;
96+
directory_moving.push(network.to_string());
97+
}
98+
}
99+
if directory_moving.is_empty() {
100+
return Ok(None);
101+
}
102+
Ok(Some(Defect::CoffeeGlobalrepoCleanup(directory_moving)))
103+
}
104+
}

coffee_lib/src/types/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ pub mod response {
138138
// A patch operation when a git repository is present in the coffee configuration
139139
// but is absent from the local storage.
140140
RepositoryLocallyAbsent(Vec<String>),
141+
CoffeeGlobalrepoCleanup(Vec<String>),
141142
// TODO: Add more patch operations
142143
}
143144

@@ -166,6 +167,13 @@ pub mod response {
166167
write!(f, " {}", repo)?;
167168
}
168169
}
170+
Defect::CoffeeGlobalrepoCleanup(networks) => {
171+
writeln!(
172+
f,
173+
"Global repository migration completed for the networks: {}",
174+
networks.iter().map(String::from).collect::<String>()
175+
)?;
176+
}
169177
}
170178
}
171179
Ok(())

0 commit comments

Comments
 (0)