From 21e8be0b5745c50bb538b20a21f5670360e186dd Mon Sep 17 00:00:00 2001 From: Tarek Date: Thu, 28 Sep 2023 14:47:13 +0300 Subject: [PATCH] feat(cmd): print nurse status in a table Signed-off-by: Tarek --- coffee_cmd/src/coffee_term/command_show.rs | 50 +++++++++++++++++++++- coffee_cmd/src/main.rs | 14 ++---- coffee_core/src/coffee.rs | 24 ++++------- coffee_core/src/nurse/strategy.rs | 6 +-- coffee_github/src/repository.rs | 6 ++- coffee_lib/src/types/mod.rs | 3 +- 6 files changed, 70 insertions(+), 33 deletions(-) diff --git a/coffee_cmd/src/coffee_term/command_show.rs b/coffee_cmd/src/coffee_term/command_show.rs index bece9a95..20693153 100644 --- a/coffee_cmd/src/coffee_term/command_show.rs +++ b/coffee_cmd/src/coffee_term/command_show.rs @@ -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) -> Result<(), CoffeeError> { @@ -76,3 +76,51 @@ pub fn show_remote_list(remote_list: Result) -> Resul Ok(()) } + +pub fn show_nurse_result( + nurse_result: Result, +) -> 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(()) +} diff --git a/coffee_cmd/src/main.rs b/coffee_cmd/src/main.rs index d27b89f2..ed37abf9 100644 --- a/coffee_cmd/src/main.rs +++ b/coffee_cmd/src/main.rs @@ -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 { diff --git a/coffee_core/src/coffee.rs b/coffee_core/src/coffee.rs index 3a30817e..651ba58f 100644 --- a/coffee_core/src/coffee.rs +++ b/coffee_core/src/coffee.rs @@ -62,28 +62,23 @@ impl From<&CoffeeManager> for CoffeeStorageInfo { } pub struct CoffeeManager { - config: config::CoffeeConf, - repos: HashMap>, + pub config: config::CoffeeConf, + pub repos: HashMap>, /// 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, + pub cln_config: Option, /// storage instance to make all the plugin manager /// information persistent on disk - storage: NoSQlStorage, + pub storage: NoSQlStorage, /// core lightning rpc connection - rpc: Option, + pub rpc: Option, /// 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> { - &self.repos - } - pub async fn new(conf: &dyn CoffeeArgs) -> Result { let conf = CoffeeConf::new(conf).await?; let mut coffee = CoffeeManager { @@ -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); diff --git a/coffee_core/src/nurse/strategy.rs b/coffee_core/src/nurse/strategy.rs index 7a47bb6f..a1e85d4d 100644 --- a/coffee_core/src/nurse/strategy.rs +++ b/coffee_core/src/nurse/strategy.rs @@ -54,7 +54,7 @@ impl Handler for GitRepositoryLocallyAbsentStrategy { coffee: &CoffeeManager, ) -> Result, CoffeeError> { let mut repos: Vec = 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; @@ -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))) } } } diff --git a/coffee_github/src/repository.rs b/coffee_github/src/repository.rs index 9df62ce1..97728f1f 100644 --- a/coffee_github/src/repository.rs +++ b/coffee_github/src/repository.rs @@ -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); diff --git a/coffee_lib/src/types/mod.rs b/coffee_lib/src/types/mod.rs index c3f4afad..f946eb64 100644 --- a/coffee_lib/src/types/mod.rs +++ b/coffee_lib/src/types/mod.rs @@ -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)]