From 4a48773a43f12817e485dd1651446b2c769a4ed3 Mon Sep 17 00:00:00 2001 From: Thaumy Date: Mon, 25 Sep 2023 13:15:37 +0800 Subject: [PATCH] fix: foe option --- src/main.rs | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 697962d..7ceef7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,13 +21,18 @@ use anyhow::Result; use clap::CommandFactory; use clap::Parser; use std::env; -use std::ops::Not; pub mod api; pub mod args; pub mod display; pub mod infra; +fn panic_if_err(result: &Result) { + if let Err(e) = result { + panic!("{}", e) + } +} + #[tokio::main(flavor = "multi_thread")] async fn main() -> Result<()> { let args_vec = env::args().collect::>(); @@ -44,18 +49,22 @@ async fn main() -> Result<()> { let style = &args.style; let time_style = &args.time_style; let rev = args.rev; + let foe = args.fail_on_error; let output = match args { _ if let Some(pat) = parser::login(&args) => { let cfg_path = session::login(pat); + foe.then(|| panic_if_err(&cfg_path)); display::login(style, &cfg_path) } _ if parser::logout(&args) => { - let cfg_path = &session::logout(); - display::logout(style, cfg_path) + let cfg_path = session::logout(); + foe.then(|| panic_if_err(&cfg_path)); + display::logout(style, &cfg_path) } _ if parser::user_info(&args) => { let user_info = User::new(pat?).get_info().await; + foe.then(|| panic_if_err(&user_info)); display::user_info(style, &user_info)? } _ if let Some((skip, take, r#type, align)) = parser::list_ing(&args) => { @@ -72,6 +81,7 @@ async fn main() -> Result<()> { .into_iter() .collect::>>()? }; + foe.then(|| panic_if_err(&ing_with_comment_list)); display::list_ing(style, time_style, &ing_with_comment_list, rev, align)? } _ if let Some(content) = parser::publish_ing(&args) => { @@ -79,6 +89,7 @@ async fn main() -> Result<()> { Ing::new(pat?).publish(content).await?; content }; + foe.then(|| panic_if_err(&content)); display::publish_ing(style, &content) } _ if let Some((content, id)) = parser::comment_ing(&args) => { @@ -86,22 +97,27 @@ async fn main() -> Result<()> { Ing::new(pat?).comment(id, content.clone(), None, None).await?; content }; + foe.then(|| panic_if_err(&content)); display::comment_ing(style, &content) } _ if let Some(id) = parser::show_post(&args) => { let entry = Post::new(pat?).get_one(id).await; + foe.then(|| panic_if_err(&entry)); display::show_post(style, &entry)? } _ if let Some(id) = parser::show_post_meta(&args) => { let entry = Post::new(pat?).get_one(id).await; + foe.then(|| panic_if_err(&entry)); display::show_post_meta(style, time_style, &entry)? } _ if let Some(id) = parser::show_post_comment(&args) => { let comment_vec = Post::new(pat?).get_comment_list(id).await; + foe.then(|| panic_if_err(&comment_vec)); display::show_post_comment(style, time_style, &comment_vec, rev)? } _ if let Some((skip, take)) = parser::list_post(&args) => { let meta_vec = Post::new(pat?).get_meta_list(skip, take).await; + foe.then(|| panic_if_err(&meta_vec)); display::list_post(style, &meta_vec, rev)? } _ if let Some(id) = parser::delete_post(&args) => { @@ -109,22 +125,27 @@ async fn main() -> Result<()> { Post::new(pat?).del_one(id).await?; id }; + foe.then(|| panic_if_err(&id)); display::delete_post(style, &id) } _ if let Some((kw, skip, take)) = parser::search_post(&args) => { let result = Post::new(pat?).search(skip, take, kw).await; + foe.then(|| panic_if_err(&result)); display::search_post(style, &result, rev)? } _ if let Some((title, body, publish)) = parser::create_post(&args) => { let id = Post::new(pat?).create(title, body, publish).await; + foe.then(|| panic_if_err(&id)); display::create_post(style, &id) } _ if let Some((id, title, body, publish)) = parser::update_post(&args) => { let id = Post::new(pat?).update(id, title, body, publish).await; + foe.then(|| panic_if_err(&id)); display::update_post(style, &id) } _ if let Some((skip, take)) = parser::list_news(&args) => { let news_vec = News::new(pat?).get_list(skip, take).await; + foe.then(|| panic_if_err(&news_vec)); display::list_news(style, time_style, &news_vec, rev)? } @@ -135,11 +156,7 @@ async fn main() -> Result<()> { _ => "Invalid usage, follow '--help' for more information".to_owned() }; - if args.fail_on_error { - panic!("{}", output); - } - if args.quiet.not() { - print!("{}", output); - } + print!("{}", output); + ().into_ok() }