Skip to content

Commit 43f5f74

Browse files
committed
feat: foe option
1 parent 9b8f093 commit 43f5f74

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/main.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ pub mod args;
2424
pub mod display;
2525
pub mod infra;
2626

27+
fn panic_if_err<T>(result: &Result<T>) {
28+
if let Err(e) = result {
29+
panic!("{}", e)
30+
}
31+
}
32+
2733
#[tokio::main(flavor = "multi_thread")]
2834
async fn main() -> Result<()> {
2935
let args_vec = env::args().collect::<Vec<_>>();
@@ -39,74 +45,86 @@ async fn main() -> Result<()> {
3945
let pat = args.with_pat.clone().or_eval_result(session::get_pat);
4046
let style = &args.style;
4147
let rev = args.rev;
42-
// TODO
43-
let _fail_on_error = args.fail_on_error;
48+
let foe = args.fail_on_error;
4449

4550
match args {
4651
_ if let Some(pat) = parser::login(&args) => {
4752
let cfg_path = session::login(pat);
53+
foe.then(||panic_if_err(&cfg_path));
4854
display::login(style, &cfg_path);
4955
}
5056
_ if parser::logout(&args) => {
51-
let cfg_path = session::logout();
52-
display::logout(style, &cfg_path);
57+
let cfg_path = &session::logout();
58+
foe.then(||panic_if_err(cfg_path));
59+
display::logout(style, cfg_path);
5360
}
5461
_ if parser::user_info(&args) => {
5562
let user_info = try {
5663
User::new(pat?).get_info().await?
5764
};
65+
foe.then(||panic_if_err(&user_info));
5866
display::user_info(style, &user_info);
5967
}
6068
_ if let Some((skip, take)) = parser::list_ing(&args) => {
6169
let ing_type = IngType::Public;
6270
let ing_vec = try {
6371
Ing::new(pat?).get_list(skip, take, &ing_type).await?
6472
};
73+
foe.then(||panic_if_err(&ing_vec));
6574
display::list_ing(style, &ing_vec, rev);
6675
}
6776
_ if let Some(content) = parser::publish_ing(&args) => {
6877
let content = try {
6978
Ing::new(pat?).publish(content).await?;
7079
content
7180
};
81+
foe.then(||panic_if_err(&content));
7282
display::publish_ing(style, &content);
7383
}
7484
_ if let Some((content, id))= parser::comment_ing(&args) => {
7585
let content = try {
7686
Ing::new(pat?).comment(id, content.clone(), None, None).await?;
7787
content
7888
};
89+
foe.then(||panic_if_err(&content));
7990
display::comment_ing(style, &content);
8091
}
8192
_ if let Some(id) = parser::show_post(&args) => {
8293
let entry = try { Post::new(pat?).get_one(id).await? };
94+
foe.then(||panic_if_err(&entry));
8395
display::show_post(style, &entry);
8496
}
8597
_ if let Some(id) = parser::show_post_meta(&args) => {
8698
let entry = try { Post::new(pat?).get_one(id).await? };
99+
foe.then(||panic_if_err(&entry));
87100
display::show_post_meta(style, &entry);
88101
}
89102
_ if let Some((skip, take)) = parser::list_post(&args) => {
90103
let result = try { Post::new(pat?).get_meta_list(skip, take).await? };
104+
foe.then(||panic_if_err(&result));
91105
display::list_post(style, &result, rev);
92106
}
93107
_ if let Some(id) = parser::delete_post(&args) => {
94108
let id = try {
95109
Post::new(pat?).del_one(id).await?;
96110
id
97111
};
112+
foe.then(||panic_if_err(&id));
98113
display::delete_post(style, &id);
99114
}
100115
_ if let Some((kw, skip, take)) = parser::search_post(&args) => {
101116
let result = try { Post::new(pat?).search(skip, take, kw).await? };
117+
foe.then(||panic_if_err(&result));
102118
display::search_post(style, &result, rev);
103119
}
104120
_ if let Some((title, body, publish)) = parser::create_post(&args) => {
105121
let id = try { Post::new(pat?).create(title, body, publish).await? };
122+
foe.then(||panic_if_err(&id));
106123
display::create_post(style, &id);
107124
}
108125
_ if let Some((id, title, body, publish)) = parser::update_post(&args) => {
109126
let id = try { Post::new(pat?).update(id,title, body, publish).await? };
127+
foe.then(||panic_if_err(&id));
110128
display::update_post(style, &id);
111129
}
112130

0 commit comments

Comments
 (0)