Skip to content

Commit f145b1f

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 f145b1f

File tree

6 files changed

+68
-3
lines changed

6 files changed

+68
-3
lines changed

coffee_cmd/src/coffee_term/command_show.rs

+4
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ pub fn show_nurse_result(
102102
NurseStatus::RepositoryLocallyRemoved(_) => {
103103
"Removed from local storage".to_string()
104104
}
105+
NurseStatus::MovingGlobalRepostoryTo(_) => {
106+
format!("Moving Global repository directory")
107+
}
105108
};
106109
let repos_str = match status {
107110
NurseStatus::RepositoryLocallyRestored(repos)
108111
| NurseStatus::RepositoryLocallyRemoved(repos) => repos.join(", "),
112+
NurseStatus::MovingGlobalRepostoryTo(network) => network.to_owned(),
109113
};
110114

111115
table.push([

coffee_core/src/coffee.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Coffee mod implementation
22
use std::collections::HashMap;
33
use std::fmt::Debug;
4+
use std::path::Path;
45
use std::vec::Vec;
5-
use tokio::fs;
66

77
use async_trait::async_trait;
88
use clightningrpc_common::client::Client;
@@ -12,6 +12,7 @@ use log;
1212
use serde::de::DeserializeOwned;
1313
use serde::{Deserialize, Serialize};
1414
use serde_json::json;
15+
use tokio::fs;
1516
use tokio::process::Command;
1617

1718
use coffee_github::repository::Github;
@@ -20,6 +21,7 @@ use coffee_lib::plugin_manager::PluginManager;
2021
use coffee_lib::repository::Repository;
2122
use coffee_lib::types::response::*;
2223
use coffee_lib::url::URL;
24+
use coffee_lib::utils::move_dir_if_exist;
2325
use coffee_lib::{commit_id, error, get_repo_info, sh};
2426
use coffee_storage::model::repository::{Kind, Repository as RepositoryInfo};
2527
use coffee_storage::nosql_db::NoSQlStorage;
@@ -497,6 +499,16 @@ impl PluginManager for CoffeeManager {
497499
let mut actions = self.patch_repository_locally_absent(repos.to_vec()).await?;
498500
nurse_actions.append(&mut actions);
499501
}
502+
Defect::CoffeeGlobalrepoCleanup(networks) => {
503+
for (network, path) in networks {
504+
let global_repo = format!("{}/repositories", self.config.root_path);
505+
if !Path::exists(&Path::new(&path)) {
506+
move_dir_if_exist(&global_repo, &path).await?;
507+
}
508+
nurse_actions
509+
.push(NurseStatus::MovingGlobalRepostoryTo(network.to_owned()));
510+
}
511+
}
500512
}
501513
}
502514
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/chain.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use async_trait::async_trait;
3333
use coffee_lib::errors::CoffeeError;
3434
use coffee_lib::types::response::{ChainOfResponsibilityStatus, Defect};
3535

36-
use super::strategy::GitRepositoryLocallyAbsentStrategy;
36+
use super::strategy::{CoffeeRepositoryDirCleanUp, GitRepositoryLocallyAbsentStrategy};
3737
use crate::coffee::CoffeeManager;
3838

3939
#[async_trait]
@@ -52,7 +52,10 @@ impl RecoveryChainOfResponsibility {
5252
/// Create a new instance of the chain of responsibility
5353
pub async fn new() -> Result<Self, CoffeeError> {
5454
Ok(Self {
55-
handlers: vec![Arc::new(GitRepositoryLocallyAbsentStrategy)],
55+
handlers: vec![
56+
Arc::new(GitRepositoryLocallyAbsentStrategy),
57+
Arc::new(CoffeeRepositoryDirCleanUp),
58+
],
5659
})
5760
}
5861

coffee_core/src/nurse/strategy.rs

+25
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,28 @@ impl Handler for GitRepositoryLocallyAbsentStrategy {
7474
}
7575
}
7676
}
77+
78+
pub struct CoffeeRepositoryDirCleanUp;
79+
80+
#[async_trait]
81+
impl Handler for CoffeeRepositoryDirCleanUp {
82+
async fn can_be_applied(
83+
self: Arc<Self>,
84+
coffee: &CoffeeManager,
85+
) -> Result<Option<Defect>, CoffeeError> {
86+
let networks = ["testnet", "signet", "bitcoin", "liquid"];
87+
// check if the repository subdirectory has the repository directory
88+
// inside the subdirectory.
89+
let mut directory_moving = vec![];
90+
for network in networks {
91+
let subpath_repo = format!("{}/{network}/repositories", coffee.config.root_path);
92+
if !Path::exists(&Path::new(&subpath_repo)) {
93+
directory_moving.push((network.to_string(), subpath_repo));
94+
}
95+
}
96+
if directory_moving.is_empty() {
97+
return Ok(None);
98+
}
99+
Ok(Some(Defect::CoffeeGlobalrepoCleanup(directory_moving)))
100+
}
101+
}

coffee_lib/src/types/mod.rs

+19
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, String)>),
141142
// TODO: Add more patch operations
142143
}
143144

@@ -166,6 +167,16 @@ 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
175+
.iter()
176+
.map(|(network, _)| network.to_owned())
177+
.collect::<String>()
178+
)?;
179+
}
169180
}
170181
}
171182
Ok(())
@@ -180,6 +191,7 @@ pub mod response {
180191
pub enum NurseStatus {
181192
RepositoryLocallyRestored(Vec<String>),
182193
RepositoryLocallyRemoved(Vec<String>),
194+
MovingGlobalRepostoryTo(String),
183195
}
184196

185197
#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -206,6 +218,7 @@ pub mod response {
206218
NurseStatus::RepositoryLocallyRestored(repos) => {
207219
repositories_locally_restored.append(&mut repos.clone())
208220
}
221+
NurseStatus::MovingGlobalRepostoryTo(_) => {}
209222
}
210223
}
211224
if !repositories_locally_removed.is_empty() {
@@ -231,6 +244,12 @@ pub mod response {
231244
NurseStatus::RepositoryLocallyRemoved(val) => {
232245
write!(f, "Repositories removed locally: {}", val.join(" "))
233246
}
247+
NurseStatus::MovingGlobalRepostoryTo(net) => {
248+
write!(
249+
f,
250+
"Global repository directory moved to subdirectory for network `{net}`"
251+
)
252+
}
234253
}
235254
}
236255
}

0 commit comments

Comments
 (0)