Skip to content

Commit

Permalink
feat: foe option
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 8, 2023
1 parent 9b8f093 commit 43f5f74
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ pub mod args;
pub mod display;
pub mod infra;

fn panic_if_err<T>(result: &Result<T>) {
if let Err(e) = result {
panic!("{}", e)
}
}

#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<()> {
let args_vec = env::args().collect::<Vec<_>>();
Expand All @@ -39,74 +45,86 @@ async fn main() -> Result<()> {
let pat = args.with_pat.clone().or_eval_result(session::get_pat);
let style = &args.style;
let rev = args.rev;
// TODO
let _fail_on_error = args.fail_on_error;
let foe = args.fail_on_error;

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 = try {
User::new(pat?).get_info().await?
};
foe.then(||panic_if_err(&user_info));
display::user_info(style, &user_info);
}
_ if let Some((skip, take)) = parser::list_ing(&args) => {
let ing_type = IngType::Public;
let ing_vec = try {
Ing::new(pat?).get_list(skip, take, &ing_type).await?
};
foe.then(||panic_if_err(&ing_vec));
display::list_ing(style, &ing_vec, rev);
}
_ if let Some(content) = parser::publish_ing(&args) => {
let content = try {
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) => {
let content = try {
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 = try { 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 = try { Post::new(pat?).get_one(id).await? };
foe.then(||panic_if_err(&entry));
display::show_post_meta(style, &entry);
}
_ if let Some((skip, take)) = parser::list_post(&args) => {
let result = try { Post::new(pat?).get_meta_list(skip, take).await? };
foe.then(||panic_if_err(&result));
display::list_post(style, &result, rev);
}
_ if let Some(id) = parser::delete_post(&args) => {
let id = try {
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 = try { 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 = try { 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 = try { Post::new(pat?).update(id,title, body, publish).await? };
foe.then(||panic_if_err(&id));
display::update_post(style, &id);
}

Expand Down

0 comments on commit 43f5f74

Please sign in to comment.