Skip to content

Commit

Permalink
feat(cmd): print nurse status in a table
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser authored and vincenzopalazzo committed Sep 29, 2023
1 parent 2ab26d7 commit 21e8be0
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 33 deletions.
50 changes: 49 additions & 1 deletion coffee_cmd/src/coffee_term/command_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use radicle_term::table::TableOptions;

use coffee_lib::error;
use coffee_lib::errors::CoffeeError;
use coffee_lib::types::response::{CoffeeList, CoffeeRemote};
use coffee_lib::types::response::{CoffeeList, CoffeeNurse, CoffeeRemote, NurseStatus};
use term::Element;

pub fn show_list(coffee_list: Result<CoffeeList, CoffeeError>) -> Result<(), CoffeeError> {
Expand Down Expand Up @@ -76,3 +76,51 @@ pub fn show_remote_list(remote_list: Result<CoffeeRemote, CoffeeError>) -> Resul

Ok(())
}

pub fn show_nurse_result(
nurse_result: Result<CoffeeNurse, CoffeeError>,
) -> Result<(), CoffeeError> {
match nurse_result {
Ok(nurse) => {
// special case: if the nurse is sane
// we print a message and return
if nurse.status[0] == NurseStatus::Sane {
term::success!("Coffee configuration is not corrupt! No need to run coffee nurse");
return Ok(());
}
let mut table = radicle_term::Table::new(TableOptions::bordered());
table.push([
term::format::dim(String::from("●")),
term::format::bold(String::from("Actions Taken")),
term::format::bold(String::from("Affected repositories")),
]);
table.divider();

for status in &nurse.status {
let action_str = match status {
NurseStatus::Sane => "".to_string(),
NurseStatus::RepositoryLocallyRestored(_) => "Restored using Git".to_string(),
NurseStatus::RepositoryLocallyRemoved(_) => {
"Removed from local storage".to_string()
}
};
let repos_str = match status {
NurseStatus::Sane => "".to_string(),
NurseStatus::RepositoryLocallyRestored(repos)
| NurseStatus::RepositoryLocallyRemoved(repos) => repos.join(", "),
};

table.push([
term::format::positive("●").into(),
term::format::bold(action_str.clone()),
term::format::highlight(repos_str.clone()),
]);
}

table.print();
}
Err(err) => eprintln!("{}", err),
}

Ok(())
}
14 changes: 4 additions & 10 deletions coffee_cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,10 @@ async fn main() -> Result<(), CoffeeError> {
}
Err(err) => Err(err),
},
CoffeeCommand::Nurse {} => match coffee.nurse().await {
Ok(val) => {
// For every status in CoffeeNurse, print it
for status in val.status {
term::success!("{}", status.to_string());
}
Ok(())
}
Err(err) => Err(err),
},
CoffeeCommand::Nurse {} => {
let nurse_result = coffee.nurse().await;
coffee_term::show_nurse_result(nurse_result)
}
};

if let Err(err) = result {
Expand Down
24 changes: 8 additions & 16 deletions coffee_core/src/coffee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,23 @@ impl From<&CoffeeManager> for CoffeeStorageInfo {
}

pub struct CoffeeManager {
config: config::CoffeeConf,
repos: HashMap<String, Box<dyn Repository + Send + Sync>>,
pub config: config::CoffeeConf,
pub repos: HashMap<String, Box<dyn Repository + Send + Sync>>,
/// Core lightning configuration managed by coffee
coffee_cln_config: CLNConf,
pub coffee_cln_config: CLNConf,
/// Core lightning configuration that include the
/// configuration managed by coffee
cln_config: Option<CLNConf>,
pub cln_config: Option<CLNConf>,
/// storage instance to make all the plugin manager
/// information persistent on disk
storage: NoSQlStorage,
pub storage: NoSQlStorage,
/// core lightning rpc connection
rpc: Option<Client>,
pub rpc: Option<Client>,
/// Recovery Strategies for the nurse command.
recovery_strategies: RecoveryChainOfResponsibility,
pub recovery_strategies: RecoveryChainOfResponsibility,
}

impl CoffeeManager {
/// return the repos of the plugin manager
pub fn repos(&self) -> &HashMap<String, Box<dyn Repository + Send + Sync>> {
&self.repos
}

pub async fn new(conf: &dyn CoffeeArgs) -> Result<Self, CoffeeError> {
let conf = CoffeeConf::new(conf).await?;
let mut coffee = CoffeeManager {
Expand Down Expand Up @@ -497,10 +492,7 @@ impl PluginManager for CoffeeManager {
]));
}
Err(err) => {
log::debug!(
"error while recovering repository {}: {err}",
repo_name.clone()
);
log::debug!("error while recovering repository {repo_name}: {err}");
log::info!("removing repository {}", repo_name.clone());
self.repos.remove(repo_name);
log::debug!("remote removed: {}", repo_name);
Expand Down
6 changes: 3 additions & 3 deletions coffee_core/src/nurse/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Handler for GitRepositoryLocallyAbsentStrategy {
coffee: &CoffeeManager,
) -> Result<Option<Defect>, CoffeeError> {
let mut repos: Vec<String> = Vec::new();
let coffee_repos = coffee.repos();
let coffee_repos = &coffee.repos;
for repo in coffee_repos.values() {
log::debug!("Checking if repository {} exists locally", repo.name());
let repo_path = repo.url().path_string;
Expand All @@ -67,10 +67,10 @@ impl Handler for GitRepositoryLocallyAbsentStrategy {

if repos.is_empty() {
log::debug!("No repositories missing locally");
return Ok(None);
Ok(None)
} else {
log::debug!("Found {} repositories missing locally", repos.len());
return Ok(Some(Defect::RepositoryLocallyAbsent(repos)));
Ok(Some(Defect::RepositoryLocallyAbsent(repos)))
}
}
}
6 changes: 4 additions & 2 deletions coffee_github/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,11 @@ impl Repository for Github {
async fn recover(&mut self) -> Result<(), CoffeeError> {
let commit = self.git_head.clone();

debug!(
log::debug!(
"recovering repository: {} {} > {}",
self.name, &self.url.url_string, &self.url.path_string,
self.name,
&self.url.url_string,
&self.url.path_string,
);
// recursively clone the repository
let res = git2::Repository::clone(&self.url.url_string, &self.url.path_string);
Expand Down
3 changes: 2 additions & 1 deletion coffee_lib/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ pub mod request {

// Definition of the response types.
pub mod response {
use serde::{Deserialize, Serialize};
use std::fmt;

use serde::{Deserialize, Serialize};

use crate::plugin::Plugin;

#[derive(Debug, Serialize, Deserialize)]
Expand Down

0 comments on commit 21e8be0

Please sign in to comment.