Skip to content

Commit 4a48773

Browse files
committed
fix: foe option
1 parent a909ea4 commit 4a48773

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/main.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ use anyhow::Result;
2121
use clap::CommandFactory;
2222
use clap::Parser;
2323
use std::env;
24-
use std::ops::Not;
2524

2625
pub mod api;
2726
pub mod args;
2827
pub mod display;
2928
pub mod infra;
3029

30+
fn panic_if_err<T>(result: &Result<T>) {
31+
if let Err(e) = result {
32+
panic!("{}", e)
33+
}
34+
}
35+
3136
#[tokio::main(flavor = "multi_thread")]
3237
async fn main() -> Result<()> {
3338
let args_vec = env::args().collect::<Vec<_>>();
@@ -44,18 +49,22 @@ async fn main() -> Result<()> {
4449
let style = &args.style;
4550
let time_style = &args.time_style;
4651
let rev = args.rev;
52+
let foe = args.fail_on_error;
4753

4854
let output = match args {
4955
_ if let Some(pat) = parser::login(&args) => {
5056
let cfg_path = session::login(pat);
57+
foe.then(|| panic_if_err(&cfg_path));
5158
display::login(style, &cfg_path)
5259
}
5360
_ if parser::logout(&args) => {
54-
let cfg_path = &session::logout();
55-
display::logout(style, cfg_path)
61+
let cfg_path = session::logout();
62+
foe.then(|| panic_if_err(&cfg_path));
63+
display::logout(style, &cfg_path)
5664
}
5765
_ if parser::user_info(&args) => {
5866
let user_info = User::new(pat?).get_info().await;
67+
foe.then(|| panic_if_err(&user_info));
5968
display::user_info(style, &user_info)?
6069
}
6170
_ if let Some((skip, take, r#type, align)) = parser::list_ing(&args) => {
@@ -72,59 +81,71 @@ async fn main() -> Result<()> {
7281
.into_iter()
7382
.collect::<Result<Vec<_>>>()?
7483
};
84+
foe.then(|| panic_if_err(&ing_with_comment_list));
7585
display::list_ing(style, time_style, &ing_with_comment_list, rev, align)?
7686
}
7787
_ if let Some(content) = parser::publish_ing(&args) => {
7888
let content = try {
7989
Ing::new(pat?).publish(content).await?;
8090
content
8191
};
92+
foe.then(|| panic_if_err(&content));
8293
display::publish_ing(style, &content)
8394
}
8495
_ if let Some((content, id)) = parser::comment_ing(&args) => {
8596
let content = try {
8697
Ing::new(pat?).comment(id, content.clone(), None, None).await?;
8798
content
8899
};
100+
foe.then(|| panic_if_err(&content));
89101
display::comment_ing(style, &content)
90102
}
91103
_ if let Some(id) = parser::show_post(&args) => {
92104
let entry = Post::new(pat?).get_one(id).await;
105+
foe.then(|| panic_if_err(&entry));
93106
display::show_post(style, &entry)?
94107
}
95108
_ if let Some(id) = parser::show_post_meta(&args) => {
96109
let entry = Post::new(pat?).get_one(id).await;
110+
foe.then(|| panic_if_err(&entry));
97111
display::show_post_meta(style, time_style, &entry)?
98112
}
99113
_ if let Some(id) = parser::show_post_comment(&args) => {
100114
let comment_vec = Post::new(pat?).get_comment_list(id).await;
115+
foe.then(|| panic_if_err(&comment_vec));
101116
display::show_post_comment(style, time_style, &comment_vec, rev)?
102117
}
103118
_ if let Some((skip, take)) = parser::list_post(&args) => {
104119
let meta_vec = Post::new(pat?).get_meta_list(skip, take).await;
120+
foe.then(|| panic_if_err(&meta_vec));
105121
display::list_post(style, &meta_vec, rev)?
106122
}
107123
_ if let Some(id) = parser::delete_post(&args) => {
108124
let id = try {
109125
Post::new(pat?).del_one(id).await?;
110126
id
111127
};
128+
foe.then(|| panic_if_err(&id));
112129
display::delete_post(style, &id)
113130
}
114131
_ if let Some((kw, skip, take)) = parser::search_post(&args) => {
115132
let result = Post::new(pat?).search(skip, take, kw).await;
133+
foe.then(|| panic_if_err(&result));
116134
display::search_post(style, &result, rev)?
117135
}
118136
_ if let Some((title, body, publish)) = parser::create_post(&args) => {
119137
let id = Post::new(pat?).create(title, body, publish).await;
138+
foe.then(|| panic_if_err(&id));
120139
display::create_post(style, &id)
121140
}
122141
_ if let Some((id, title, body, publish)) = parser::update_post(&args) => {
123142
let id = Post::new(pat?).update(id, title, body, publish).await;
143+
foe.then(|| panic_if_err(&id));
124144
display::update_post(style, &id)
125145
}
126146
_ if let Some((skip, take)) = parser::list_news(&args) => {
127147
let news_vec = News::new(pat?).get_list(skip, take).await;
148+
foe.then(|| panic_if_err(&news_vec));
128149
display::list_news(style, time_style, &news_vec, rev)?
129150
}
130151

@@ -135,11 +156,7 @@ async fn main() -> Result<()> {
135156
_ => "Invalid usage, follow '--help' for more information".to_owned()
136157
};
137158

138-
if args.fail_on_error {
139-
panic!("{}", output);
140-
}
141-
if args.quiet.not() {
142-
print!("{}", output);
143-
}
159+
print!("{}", output);
160+
144161
().into_ok()
145162
}

0 commit comments

Comments
 (0)