use anyhow::{Context, Result};
use clap::Parser;
use judo::{
app::App,
cli::{
args::{Cli, Commands, DbCommands, ItemCommands, ListCommands},
ops,
},
};
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
let app = App::new().await;
match cli.command {
Some(Commands::Dbs { command }) => match command {
Some(DbCommands::Show) => {
ops::list_dbs(&app).with_context(|| "Failed to list databases")?;
}
Some(DbCommands::Add { name }) => {
ops::add_db(app, name)
.await
.with_context(|| "Failed to add database")?;
}
None => {}
},
Some(Commands::Lists { command }) => match command {
Some(ListCommands::Show { name }) => {
ops::list_lists(&app, name)
.await
.with_context(|| "Failed to list to-do lists")?;
}
Some(ListCommands::Add { name, db }) => {
ops::add_list(&app, name, &db)
.await
.with_context(|| "Failed to add to-do list")?;
}
Some(ListCommands::Delete { name, id, db }) => {
ops::delete_list(&app, name, id, &db)
.await
.with_context(|| "Failed to delete to-do list")?;
}
None => {}
},
Some(Commands::Items { command }) => match command {
Some(ItemCommands::Show) => {
ops::list_items(&app)
.await
.with_context(|| "Failed to list to-do items")?;
}
Some(ItemCommands::Add {
name,
db,
list_name,
list_id,
}) => {
ops::add_item(&app, name, &db, list_id, list_name)
.await
.with_context(|| "Failed to add to-do item")?;
}
Some(ItemCommands::Delete { id, db }) => {
ops::delete_item(&app, id, &db)
.await
.with_context(|| "Failed to delete to-do item")?;
}
Some(ItemCommands::ToggleDone { id, db }) => {
ops::toggle_done_item(&app, id, &db)
.await
.with_context(|| "Failed to toggle to-do item status")?;
}
None => {}
},
None => {
let mut terminal = ratatui::init();
let app_result = app.run(&mut terminal).await;
ratatui::restore();
return app_result;
}
}
Ok(())
}