Skip to content

Commit 99633ee

Browse files
fixup! feat(core): nurse clean up the global repository
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 8911d8f commit 99633ee

File tree

11 files changed

+54
-57
lines changed

11 files changed

+54
-57
lines changed

coffee_cmd/src/coffee_term/command_show.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,16 @@ pub fn show_nurse_verify(nurse_verify: &ChainOfResponsibilityStatus) -> Result<(
113113
]);
114114
}
115115
}
116-
Defect::CoffeeGlobalrepoCleanup(networks) => {
117-
let defect = format!(
118-
"Global repository migration completed for the networks: {}",
119-
networks
120-
.iter()
121-
.map(|(network, _)| format!("{network} "))
122-
.collect::<String>()
123-
.trim_end()
124-
);
116+
Defect::CoffeeGlobalRepoCleanup(networks) => {
117+
let defect = "Network specific repository missing";
118+
let networks = networks
119+
.iter()
120+
.map(|(network, _)| network.clone())
121+
.collect::<Vec<String>>();
125122
table.push([
126123
term::format::positive("●").into(),
127124
term::format::bold(defect.to_owned()),
128-
term::format::highlight("_".to_owned()),
125+
term::format::highlight(networks.join(", ")),
129126
]);
130127
}
131128
}

coffee_cmd/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeEr
8282
match action {
8383
Some(RemoteAction::Add { name, url }) => {
8484
let mut spinner = term::spinner(format!("Fetch remote from {url}"));
85-
let result = coffee.add_remote(&name, &url).await;
85+
let result = coffee.add_remote(&name, &url, false).await;
8686
if let Err(err) = &result {
8787
spinner.error(format!("Error while add remote: {err}"));
8888
return result;

coffee_core/src/coffee.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Coffee mod implementation
22
use std::collections::HashMap;
33
use std::fmt::Debug;
4-
use std::path::Path;
54
use std::vec::Vec;
65

76
use async_trait::async_trait;
@@ -21,7 +20,7 @@ use coffee_lib::plugin_manager::PluginManager;
2120
use coffee_lib::repository::Repository;
2221
use coffee_lib::types::response::*;
2322
use coffee_lib::url::URL;
24-
use coffee_lib::utils::{check_dir_or_make_if_missing, copy_dir_if_exist, rm_dir_if_exist};
23+
use coffee_lib::utils::rm_dir_if_exist;
2524
use coffee_lib::{commit_id, error, get_repo_info, sh};
2625
use coffee_storage::model::repository::{Kind, Repository as RepositoryInfo};
2726
use coffee_storage::nosql_db::NoSQlStorage;
@@ -427,8 +426,8 @@ impl PluginManager for CoffeeManager {
427426
Ok(())
428427
}
429428

430-
async fn add_remote(&mut self, name: &str, url: &str) -> Result<(), CoffeeError> {
431-
if self.repos.contains_key(name) {
429+
async fn add_remote(&mut self, name: &str, url: &str, force: bool) -> Result<(), CoffeeError> {
430+
if !force && self.repos.contains_key(name) {
432431
return Err(error!("repository with name: {name} already exists"));
433432
}
434433
let url = URL::new(&self.config.path(), url, name);
@@ -544,17 +543,21 @@ impl PluginManager for CoffeeManager {
544543
let mut actions = self.patch_repository_locally_absent(repos.to_vec()).await?;
545544
nurse_actions.append(&mut actions);
546545
}
547-
Defect::CoffeeGlobalrepoCleanup(networks) => {
548-
let global_repo = format!("{}/repositories", self.config.root_path);
549-
for (network, path) in networks {
550-
log::info!("{network} - {path}");
551-
check_dir_or_make_if_missing(path.to_owned()).await?;
552-
if !Path::exists(Path::new(&path)) {
553-
copy_dir_if_exist(&global_repo, path).await?;
546+
Defect::CoffeeGlobalRepoCleanup(networks) => {
547+
for network in networks {
548+
log::debug!("reindexing repository for the network `{:?}`", network);
549+
let iter = self
550+
.repos
551+
.iter()
552+
.map(|(name, repo)| (name.to_owned(), repo.url()))
553+
.collect::<Vec<(String, URL)>>();
554+
for (name, url) in iter {
555+
self.add_remote(&name, &url.url_string, true).await?;
554556
}
555557
nurse_actions
556-
.push(NurseStatus::MovingGlobalRepostoryTo(network.to_owned()));
558+
.push(NurseStatus::MovingGlobalRepostoryTo(network.1.to_owned()));
557559
}
560+
let global_repo = format!("{}/repositories", self.config.root_path);
558561
rm_dir_if_exist(&global_repo).await?;
559562
}
560563
}
@@ -587,7 +590,6 @@ impl PluginManager for CoffeeManager {
587590
.get_mut(repo_name)
588591
.ok_or_else(|| error!("repository with name: {repo_name} not found"))?;
589592

590-
repo.change_root_path(&self.config.path());
591593
match repo.recover().await {
592594
Ok(_) => {
593595
log::info!("repository {} recovered", repo_name.clone());

coffee_core/src/nurse/chain.rs

+2-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::{CoffeeRepositoryDirCleanUp, GitRepositoryLocallyAbsentStrategy};
36+
use super::strategy::{CoffeeRepositoryDirCleanUpStrategy, GitRepositoryLocallyAbsentStrategy};
3737
use crate::coffee::CoffeeManager;
3838

3939
#[async_trait]
@@ -53,7 +53,7 @@ impl RecoveryChainOfResponsibility {
5353
pub async fn new() -> Result<Self, CoffeeError> {
5454
Ok(Self {
5555
handlers: vec![
56-
Arc::new(CoffeeRepositoryDirCleanUp),
56+
Arc::new(CoffeeRepositoryDirCleanUpStrategy),
5757
Arc::new(GitRepositoryLocallyAbsentStrategy),
5858
],
5959
})

coffee_core/src/nurse/strategy.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -76,37 +76,30 @@ impl Handler for GitRepositoryLocallyAbsentStrategy {
7676
}
7777

7878
/// Stategy for migration of the repository global directory
79-
/// to a local directory for each network, see [1]
79+
/// to a local directory for each network. See [related issue]
8080
///
81-
/// This is a strategy tht return the list of network that
82-
/// needs to be migrated to to the new repository configuration.
81+
/// This is a strategy that returns the list of networks that
82+
/// need to be migrated to to the new repository configuration.
8383
///
84-
/// [1] https://github.com/coffee-tools/coffee/issues/234
85-
pub struct CoffeeRepositoryDirCleanUp;
84+
/// [issue]: https://github.com/coffee-tools/coffee/issues/234
85+
pub struct CoffeeRepositoryDirCleanUpStrategy;
8686

8787
#[async_trait]
88-
impl Handler for CoffeeRepositoryDirCleanUp {
88+
impl Handler for CoffeeRepositoryDirCleanUpStrategy {
8989
async fn can_be_applied(
9090
self: Arc<Self>,
9191
coffee: &CoffeeManager,
9292
) -> Result<Option<Defect>, CoffeeError> {
93-
let networks = ["testnet", "signet", "bitcoin", "liquid"];
93+
let network = coffee.config.network.clone();
9494
// Check whether there exists a network-specific repositories folder for each network.
9595
let mut directory_moving = vec![];
96-
for network in networks {
97-
let network_dir = format!("{}/{network}", coffee.config.root_path);
98-
if !Path::exists(Path::new(&network_dir)) {
99-
log::debug!("network dir `{network_dir}` not found");
100-
continue;
101-
}
102-
let subpath_repo = format!("{}/{network}/repositories", coffee.config.root_path);
103-
if !Path::exists(Path::new(&subpath_repo)) {
104-
directory_moving.push((network.to_string(), subpath_repo));
105-
}
96+
let subpath_repo = format!("{}/{network}/repositories", coffee.config.root_path);
97+
if !Path::exists(Path::new(&subpath_repo)) {
98+
directory_moving.push((network.to_string(), subpath_repo));
10699
}
107100
if directory_moving.is_empty() {
108101
return Ok(None);
109102
}
110-
Ok(Some(Defect::CoffeeGlobalrepoCleanup(directory_moving)))
103+
Ok(Some(Defect::CoffeeGlobalRepoCleanup(directory_moving)))
111104
}
112105
}

coffee_github/src/repository.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,6 @@ impl Repository for Github {
233233
}
234234
}
235235

236-
fn change_root_path(&mut self, path: &str) {
237-
self.url.set_coffee_path(path);
238-
}
239-
240236
async fn upgrade(
241237
&mut self,
242238
plugins: &Vec<Plugin>,
@@ -343,6 +339,10 @@ impl Repository for Github {
343339
fn as_any(&self) -> &dyn Any {
344340
self
345341
}
342+
343+
fn plugins(&mut self) -> &mut Vec<Plugin> {
344+
&mut self.plugins
345+
}
346346
}
347347

348348
impl From<StorageRepository> for Github {

coffee_httpd/src/httpd/server.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ async fn coffee_remote_add(
130130
let repository_url = &body.repository_url;
131131

132132
let mut coffee = data.coffee.lock().await;
133-
let result = coffee.add_remote(repository_name, repository_url).await;
133+
let result = coffee
134+
.add_remote(repository_name, repository_url, false)
135+
.await;
134136

135137
handle_httpd_response!(result, "Repository '{repository_name}' added successfully")
136138
}

coffee_lib/src/plugin_manager.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub trait PluginManager {
2828
async fn upgrade(&mut self, repo: &str, verbose: bool) -> Result<CoffeeUpgrade, CoffeeError>;
2929

3030
/// add the remote repository to the plugin manager.
31-
async fn add_remote(&mut self, name: &str, url: &str) -> Result<(), CoffeeError>;
31+
async fn add_remote(&mut self, name: &str, url: &str, force: bool) -> Result<(), CoffeeError>;
3232

3333
/// remove the remote repository from the plugin manager.
3434
async fn rm_remote(&mut self, name: &str) -> Result<(), CoffeeError>;

coffee_lib/src/repository.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ pub trait Repository: Any {
3434
/// recover the repository from the commit id.
3535
async fn recover(&mut self) -> Result<(), CoffeeError>;
3636

37-
/// While migrating there is a possibility that we should
38-
/// move an old repository into a new path. So this
39-
/// is semplyfing this process.
40-
fn change_root_path(&mut self, path: &str);
41-
4237
/// return the name of the repository.
4338
fn name(&self) -> String;
4439

4540
/// return the url of the repository.
4641
fn url(&self) -> URL;
4742

4843
fn as_any(&self) -> &dyn Any;
44+
45+
/// Return the vector of plugin
46+
/// that are inside the repository
47+
fn plugins(&mut self) -> &mut Vec<Plugin>;
4948
}

coffee_lib/src/types/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub mod response {
151151
// but is absent from the local storage.
152152
RepositoryLocallyAbsent(Vec<String>),
153153
/// (Affected network, path)
154-
CoffeeGlobalrepoCleanup(Vec<(String, String)>),
154+
CoffeeGlobalRepoCleanup(Vec<(String, String)>),
155155
// TODO: Add more patch operations
156156
}
157157

coffee_plugin/src/plugin/plugin_mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ fn coffee_remote(plugin: &mut Plugin<State>, request: Value) -> Result<Value, Pl
119119
let mut coffee = coffee.lock().unwrap();
120120
let cmd = request.cmd().unwrap();
121121
match cmd {
122-
RemoteCmd::Add => coffee.add_remote(&request.name, &request.url()).await,
122+
RemoteCmd::Add => {
123+
coffee
124+
.add_remote(&request.name, &request.url(), false)
125+
.await
126+
}
123127
RemoteCmd::Rm => coffee.rm_remote(&request.name).await,
124128
}
125129
})

0 commit comments

Comments
 (0)