Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: improve the error formatting #229

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 29 additions & 38 deletions coffee_cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ use crate::cmd::CoffeeArgs;
use crate::cmd::CoffeeCommand;
use crate::cmd::RemoteAction;

#[tokio::main]
async fn main() -> Result<(), CoffeeError> {
env_logger::init();
let args = CoffeeArgs::parse();
let mut coffee = CoffeeManager::new(&args).await?;
let result = match args.command {
async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeError> {
match args.command {
CoffeeCommand::Install {
plugin,
verbose,
Expand All @@ -40,7 +36,6 @@ async fn main() -> Result<(), CoffeeError> {
} else if result.is_ok() {
term::success!("Plugin {plugin} Compiled and Installed")
}
result
}
CoffeeCommand::Remove { plugin } => {
let mut spinner = term::spinner(format!("Uninstalling plugin {plugin}"));
Expand All @@ -51,11 +46,10 @@ async fn main() -> Result<(), CoffeeError> {
}
spinner.message("Plugin uninstalled!");
spinner.finish();
Ok(())
}
CoffeeCommand::List {} => {
let remotes = coffee.list().await;
coffee_term::show_list(remotes)
coffee_term::show_list(remotes)?;
}
CoffeeCommand::Upgrade { repo, verbose } => {
let spinner = if !verbose {
Expand Down Expand Up @@ -83,7 +77,6 @@ async fn main() -> Result<(), CoffeeError> {
return Err(err);
}
}
Ok(())
}
CoffeeCommand::Remote {
action,
Expand All @@ -92,7 +85,7 @@ async fn main() -> Result<(), CoffeeError> {
} => {
if plugins {
let result = coffee.get_plugins_in_remote(&name.unwrap()).await;
coffee_term::show_list(result)
coffee_term::show_list(result)?;
} else {
match action {
Some(RemoteAction::Add { name, url }) => {
Expand All @@ -104,7 +97,6 @@ async fn main() -> Result<(), CoffeeError> {
}
spinner.message("Remote added!");
spinner.finish();
Ok(())
}
Some(RemoteAction::Rm { name }) => {
let mut spinner = term::spinner(format!("Removing remote {name}"));
Expand All @@ -115,11 +107,10 @@ async fn main() -> Result<(), CoffeeError> {
}
spinner.message("Remote removed!");
spinner.finish();
Ok(())
}
Some(RemoteAction::List {}) => {
let remotes = coffee.list_remotes().await;
coffee_term::show_remote_list(remotes)
coffee_term::show_remote_list(remotes)?;
}
None => {
// This is the case when the user does not provides the
Expand All @@ -144,51 +135,51 @@ async fn main() -> Result<(), CoffeeError> {
let remote = Ok(CoffeeRemote {
remotes: Some(vec![remote.clone()]),
});
coffee_term::show_remote_list(remote)
coffee_term::show_remote_list(remote)?;
}
}
}
}
CoffeeCommand::Setup { cln_conf } => {
// FIXME: read the core lightning config
// and the coffee script
coffee.setup(&cln_conf).await
coffee.setup(&cln_conf).await?;
}
CoffeeCommand::Show { plugin } => {
let val = coffee.show(&plugin).await?;

// FIXME: modify the radicle_term markdown
let val = val.readme.as_str();
term::markdown(val);
}
CoffeeCommand::Search { plugin } => {
let val = coffee.search(&plugin).await?;
let repository_url = val.repository_url.as_str();
term::success!("found plugin {plugin} in remote repository {repository_url}");
}
CoffeeCommand::Show { plugin } => match coffee.show(&plugin).await {
Ok(val) => {
// FIXME: modify the radicle_term markdown
let val = val.readme.as_str();
term::markdown(val);
Ok(())
}
Err(err) => Err(err),
},
CoffeeCommand::Search { plugin } => match coffee.search(&plugin).await {
Ok(val) => {
let repository_url = val.repository_url.as_str();
term::success!("found plugin {plugin} in remote repository {repository_url}");
Ok(())
}
Err(err) => Err(err),
},
CoffeeCommand::Nurse { verify } => {
if verify {
let result = coffee.nurse_verify().await?;
term::info!("{}", result);
if !result.is_sane() {
term::info!("Coffee local directory is damaged, please run `coffee nurse` to try to fix it");
}
Ok(())
} else {
let nurse_result = coffee.nurse().await;
coffee_term::show_nurse_result(nurse_result)
coffee_term::show_nurse_result(nurse_result)?;
}
}
};
Ok(())
}

if let Err(err) = result {
term::error(format!("{err}"));
#[tokio::main]
async fn main() -> Result<(), CoffeeError> {
env_logger::init();
let args = CoffeeArgs::parse();
let coffee = CoffeeManager::new(&args).await?;
if let Err(err) = run(args, coffee).await {
term::error(format!("{err}"))
}

Ok(())
}
Loading