Skip to content

Commit

Permalink
fixup! feat(core): nurse clean up the global repository
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Apr 14, 2024
1 parent 8911d8f commit 99633ee
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 57 deletions.
17 changes: 7 additions & 10 deletions coffee_cmd/src/coffee_term/command_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,16 @@ pub fn show_nurse_verify(nurse_verify: &ChainOfResponsibilityStatus) -> Result<(
]);
}
}
Defect::CoffeeGlobalrepoCleanup(networks) => {
let defect = format!(
"Global repository migration completed for the networks: {}",
networks
.iter()
.map(|(network, _)| format!("{network} "))
.collect::<String>()
.trim_end()
);
Defect::CoffeeGlobalRepoCleanup(networks) => {
let defect = "Network specific repository missing";
let networks = networks
.iter()
.map(|(network, _)| network.clone())
.collect::<Vec<String>>();
table.push([
term::format::positive("●").into(),
term::format::bold(defect.to_owned()),
term::format::highlight("_".to_owned()),
term::format::highlight(networks.join(", ")),
]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion coffee_cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeEr
match action {
Some(RemoteAction::Add { name, url }) => {
let mut spinner = term::spinner(format!("Fetch remote from {url}"));
let result = coffee.add_remote(&name, &url).await;
let result = coffee.add_remote(&name, &url, false).await;
if let Err(err) = &result {
spinner.error(format!("Error while add remote: {err}"));
return result;
Expand Down
28 changes: 15 additions & 13 deletions coffee_core/src/coffee.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Coffee mod implementation
use std::collections::HashMap;
use std::fmt::Debug;
use std::path::Path;
use std::vec::Vec;

Check warning on line 4 in coffee_core/src/coffee.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

the item `Vec` is imported redundantly

Check warning on line 4 in coffee_core/src/coffee.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

the item `Vec` is imported redundantly

use async_trait::async_trait;
Expand All @@ -21,7 +20,7 @@ use coffee_lib::plugin_manager::PluginManager;
use coffee_lib::repository::Repository;
use coffee_lib::types::response::*;
use coffee_lib::url::URL;
use coffee_lib::utils::{check_dir_or_make_if_missing, copy_dir_if_exist, rm_dir_if_exist};
use coffee_lib::utils::rm_dir_if_exist;
use coffee_lib::{commit_id, error, get_repo_info, sh};
use coffee_storage::model::repository::{Kind, Repository as RepositoryInfo};
use coffee_storage::nosql_db::NoSQlStorage;
Expand Down Expand Up @@ -427,8 +426,8 @@ impl PluginManager for CoffeeManager {
Ok(())
}

async fn add_remote(&mut self, name: &str, url: &str) -> Result<(), CoffeeError> {
if self.repos.contains_key(name) {
async fn add_remote(&mut self, name: &str, url: &str, force: bool) -> Result<(), CoffeeError> {
if !force && self.repos.contains_key(name) {
return Err(error!("repository with name: {name} already exists"));
}
let url = URL::new(&self.config.path(), url, name);
Expand Down Expand Up @@ -544,17 +543,21 @@ impl PluginManager for CoffeeManager {
let mut actions = self.patch_repository_locally_absent(repos.to_vec()).await?;
nurse_actions.append(&mut actions);
}
Defect::CoffeeGlobalrepoCleanup(networks) => {
let global_repo = format!("{}/repositories", self.config.root_path);
for (network, path) in networks {
log::info!("{network} - {path}");
check_dir_or_make_if_missing(path.to_owned()).await?;
if !Path::exists(Path::new(&path)) {
copy_dir_if_exist(&global_repo, path).await?;
Defect::CoffeeGlobalRepoCleanup(networks) => {
for network in networks {
log::debug!("reindexing repository for the network `{:?}`", network);
let iter = self
.repos
.iter()
.map(|(name, repo)| (name.to_owned(), repo.url()))
.collect::<Vec<(String, URL)>>();
for (name, url) in iter {
self.add_remote(&name, &url.url_string, true).await?;
}
nurse_actions
.push(NurseStatus::MovingGlobalRepostoryTo(network.to_owned()));
.push(NurseStatus::MovingGlobalRepostoryTo(network.1.to_owned()));
}
let global_repo = format!("{}/repositories", self.config.root_path);
rm_dir_if_exist(&global_repo).await?;
}
}
Expand Down Expand Up @@ -587,7 +590,6 @@ impl PluginManager for CoffeeManager {
.get_mut(repo_name)
.ok_or_else(|| error!("repository with name: {repo_name} not found"))?;

repo.change_root_path(&self.config.path());
match repo.recover().await {
Ok(_) => {
log::info!("repository {} recovered", repo_name.clone());
Expand Down
4 changes: 2 additions & 2 deletions coffee_core/src/nurse/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use async_trait::async_trait;
use coffee_lib::errors::CoffeeError;
use coffee_lib::types::response::{ChainOfResponsibilityStatus, Defect};

use super::strategy::{CoffeeRepositoryDirCleanUp, GitRepositoryLocallyAbsentStrategy};
use super::strategy::{CoffeeRepositoryDirCleanUpStrategy, GitRepositoryLocallyAbsentStrategy};
use crate::coffee::CoffeeManager;

#[async_trait]
Expand All @@ -53,7 +53,7 @@ impl RecoveryChainOfResponsibility {
pub async fn new() -> Result<Self, CoffeeError> {
Ok(Self {
handlers: vec![
Arc::new(CoffeeRepositoryDirCleanUp),
Arc::new(CoffeeRepositoryDirCleanUpStrategy),
Arc::new(GitRepositoryLocallyAbsentStrategy),
],
})
Expand Down
29 changes: 11 additions & 18 deletions coffee_core/src/nurse/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,37 +76,30 @@ impl Handler for GitRepositoryLocallyAbsentStrategy {
}

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

#[async_trait]
impl Handler for CoffeeRepositoryDirCleanUp {
impl Handler for CoffeeRepositoryDirCleanUpStrategy {
async fn can_be_applied(
self: Arc<Self>,
coffee: &CoffeeManager,
) -> Result<Option<Defect>, CoffeeError> {
let networks = ["testnet", "signet", "bitcoin", "liquid"];
let network = coffee.config.network.clone();
// Check whether there exists a network-specific repositories folder for each network.
let mut directory_moving = vec![];
for network in networks {
let network_dir = format!("{}/{network}", coffee.config.root_path);
if !Path::exists(Path::new(&network_dir)) {
log::debug!("network dir `{network_dir}` not found");
continue;
}
let subpath_repo = format!("{}/{network}/repositories", coffee.config.root_path);
if !Path::exists(Path::new(&subpath_repo)) {
directory_moving.push((network.to_string(), subpath_repo));
}
let subpath_repo = format!("{}/{network}/repositories", coffee.config.root_path);
if !Path::exists(Path::new(&subpath_repo)) {
directory_moving.push((network.to_string(), subpath_repo));
}
if directory_moving.is_empty() {
return Ok(None);
}
Ok(Some(Defect::CoffeeGlobalrepoCleanup(directory_moving)))
Ok(Some(Defect::CoffeeGlobalRepoCleanup(directory_moving)))
}
}
8 changes: 4 additions & 4 deletions coffee_github/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,6 @@ impl Repository for Github {
}
}

fn change_root_path(&mut self, path: &str) {
self.url.set_coffee_path(path);
}

async fn upgrade(
&mut self,
plugins: &Vec<Plugin>,
Expand Down Expand Up @@ -343,6 +339,10 @@ impl Repository for Github {
fn as_any(&self) -> &dyn Any {
self
}

fn plugins(&mut self) -> &mut Vec<Plugin> {
&mut self.plugins
}
}

impl From<StorageRepository> for Github {
Expand Down
4 changes: 3 additions & 1 deletion coffee_httpd/src/httpd/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ async fn coffee_remote_add(
let repository_url = &body.repository_url;

let mut coffee = data.coffee.lock().await;
let result = coffee.add_remote(repository_name, repository_url).await;
let result = coffee
.add_remote(repository_name, repository_url, false)
.await;

handle_httpd_response!(result, "Repository '{repository_name}' added successfully")
}
Expand Down
2 changes: 1 addition & 1 deletion coffee_lib/src/plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait PluginManager {
async fn upgrade(&mut self, repo: &str, verbose: bool) -> Result<CoffeeUpgrade, CoffeeError>;

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

/// remove the remote repository from the plugin manager.
async fn rm_remote(&mut self, name: &str) -> Result<(), CoffeeError>;
Expand Down
9 changes: 4 additions & 5 deletions coffee_lib/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ pub trait Repository: Any {
/// recover the repository from the commit id.
async fn recover(&mut self) -> Result<(), CoffeeError>;

/// While migrating there is a possibility that we should
/// move an old repository into a new path. So this
/// is semplyfing this process.
fn change_root_path(&mut self, path: &str);

/// return the name of the repository.
fn name(&self) -> String;

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

fn as_any(&self) -> &dyn Any;

/// Return the vector of plugin
/// that are inside the repository
fn plugins(&mut self) -> &mut Vec<Plugin>;
}
2 changes: 1 addition & 1 deletion coffee_lib/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub mod response {
// but is absent from the local storage.
RepositoryLocallyAbsent(Vec<String>),
/// (Affected network, path)
CoffeeGlobalrepoCleanup(Vec<(String, String)>),
CoffeeGlobalRepoCleanup(Vec<(String, String)>),
// TODO: Add more patch operations
}

Expand Down
6 changes: 5 additions & 1 deletion coffee_plugin/src/plugin/plugin_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ fn coffee_remote(plugin: &mut Plugin<State>, request: Value) -> Result<Value, Pl
let mut coffee = coffee.lock().unwrap();
let cmd = request.cmd().unwrap();
match cmd {
RemoteCmd::Add => coffee.add_remote(&request.name, &request.url()).await,
RemoteCmd::Add => {
coffee
.add_remote(&request.name, &request.url(), false)
.await
}
RemoteCmd::Rm => coffee.rm_remote(&request.name).await,
}
})
Expand Down

0 comments on commit 99633ee

Please sign in to comment.