Skip to content

Commit 79bb7be

Browse files
cmd: improve the error formatting
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 638698f commit 79bb7be

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

coffee_cmd/src/main.rs

+29-38
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@ use crate::cmd::CoffeeArgs;
1414
use crate::cmd::CoffeeCommand;
1515
use crate::cmd::RemoteAction;
1616

17-
#[tokio::main]
18-
async fn main() -> Result<(), CoffeeError> {
19-
env_logger::init();
20-
let args = CoffeeArgs::parse();
21-
let mut coffee = CoffeeManager::new(&args).await?;
22-
let result = match args.command {
17+
async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeError> {
18+
match args.command {
2319
CoffeeCommand::Install {
2420
plugin,
2521
verbose,
@@ -40,7 +36,6 @@ async fn main() -> Result<(), CoffeeError> {
4036
} else if result.is_ok() {
4137
term::success!("Plugin {plugin} Compiled and Installed")
4238
}
43-
result
4439
}
4540
CoffeeCommand::Remove { plugin } => {
4641
let mut spinner = term::spinner(format!("Uninstalling plugin {plugin}"));
@@ -51,11 +46,10 @@ async fn main() -> Result<(), CoffeeError> {
5146
}
5247
spinner.message("Plugin uninstalled!");
5348
spinner.finish();
54-
Ok(())
5549
}
5650
CoffeeCommand::List {} => {
5751
let remotes = coffee.list().await;
58-
coffee_term::show_list(remotes)
52+
coffee_term::show_list(remotes)?;
5953
}
6054
CoffeeCommand::Upgrade { repo, verbose } => {
6155
let spinner = if !verbose {
@@ -83,7 +77,6 @@ async fn main() -> Result<(), CoffeeError> {
8377
return Err(err);
8478
}
8579
}
86-
Ok(())
8780
}
8881
CoffeeCommand::Remote {
8982
action,
@@ -92,7 +85,7 @@ async fn main() -> Result<(), CoffeeError> {
9285
} => {
9386
if plugins {
9487
let result = coffee.get_plugins_in_remote(&name.unwrap()).await;
95-
coffee_term::show_list(result)
88+
coffee_term::show_list(result)?;
9689
} else {
9790
match action {
9891
Some(RemoteAction::Add { name, url }) => {
@@ -104,7 +97,6 @@ async fn main() -> Result<(), CoffeeError> {
10497
}
10598
spinner.message("Remote added!");
10699
spinner.finish();
107-
Ok(())
108100
}
109101
Some(RemoteAction::Rm { name }) => {
110102
let mut spinner = term::spinner(format!("Removing remote {name}"));
@@ -115,11 +107,10 @@ async fn main() -> Result<(), CoffeeError> {
115107
}
116108
spinner.message("Remote removed!");
117109
spinner.finish();
118-
Ok(())
119110
}
120111
Some(RemoteAction::List {}) => {
121112
let remotes = coffee.list_remotes().await;
122-
coffee_term::show_remote_list(remotes)
113+
coffee_term::show_remote_list(remotes)?;
123114
}
124115
None => {
125116
// This is the case when the user does not provides the
@@ -144,51 +135,51 @@ async fn main() -> Result<(), CoffeeError> {
144135
let remote = Ok(CoffeeRemote {
145136
remotes: Some(vec![remote.clone()]),
146137
});
147-
coffee_term::show_remote_list(remote)
138+
coffee_term::show_remote_list(remote)?;
148139
}
149140
}
150141
}
151142
}
152143
CoffeeCommand::Setup { cln_conf } => {
153144
// FIXME: read the core lightning config
154145
// and the coffee script
155-
coffee.setup(&cln_conf).await
146+
coffee.setup(&cln_conf).await?;
147+
}
148+
CoffeeCommand::Show { plugin } => {
149+
let val = coffee.show(&plugin).await?;
150+
151+
// FIXME: modify the radicle_term markdown
152+
let val = val.readme.as_str();
153+
term::markdown(val);
154+
}
155+
CoffeeCommand::Search { plugin } => {
156+
let val = coffee.search(&plugin).await?;
157+
let repository_url = val.repository_url.as_str();
158+
term::success!("found plugin {plugin} in remote repository {repository_url}");
156159
}
157-
CoffeeCommand::Show { plugin } => match coffee.show(&plugin).await {
158-
Ok(val) => {
159-
// FIXME: modify the radicle_term markdown
160-
let val = val.readme.as_str();
161-
term::markdown(val);
162-
Ok(())
163-
}
164-
Err(err) => Err(err),
165-
},
166-
CoffeeCommand::Search { plugin } => match coffee.search(&plugin).await {
167-
Ok(val) => {
168-
let repository_url = val.repository_url.as_str();
169-
term::success!("found plugin {plugin} in remote repository {repository_url}");
170-
Ok(())
171-
}
172-
Err(err) => Err(err),
173-
},
174160
CoffeeCommand::Nurse { verify } => {
175161
if verify {
176162
let result = coffee.nurse_verify().await?;
177163
term::info!("{}", result);
178164
if !result.is_sane() {
179165
term::info!("Coffee local directory is damaged, please run `coffee nurse` to try to fix it");
180166
}
181-
Ok(())
182167
} else {
183168
let nurse_result = coffee.nurse().await;
184-
coffee_term::show_nurse_result(nurse_result)
169+
coffee_term::show_nurse_result(nurse_result)?;
185170
}
186171
}
187172
};
173+
Ok(())
174+
}
188175

189-
if let Err(err) = result {
190-
term::error(format!("{err}"));
176+
#[tokio::main]
177+
async fn main() -> Result<(), CoffeeError> {
178+
env_logger::init();
179+
let args = CoffeeArgs::parse();
180+
let coffee = CoffeeManager::new(&args).await?;
181+
if let Err(err) = run(args, coffee).await {
182+
term::error(format!("{err}"))
191183
}
192-
193184
Ok(())
194185
}

0 commit comments

Comments
 (0)