use crate::models::TodoList;
use clap::{Parser, Subcommand};
use sqlx::SqlitePool;
use std::path::PathBuf;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
#[arg(short, long, default_value = None)]
pub data_dir: Option<String>,
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
Add { content: String },
Finish { content: String },
Edit {
find: String,
replace: String,
},
Clean,
List,
Tui,
Path,
}
pub async fn execute_command(
cli: Cli,
mut tdlist: TodoList,
pool: &SqlitePool,
data_dir: &PathBuf,
) -> color_eyre::Result<TodoList> {
match cli.command {
Commands::Add { content } => {
tdlist.add_todo_db(content, pool).await?;
}
Commands::Finish { content } => {
tdlist.finish_todo_db(content, pool).await?;
}
Commands::Edit { find, replace } => {
tdlist.edit_todo_db(find, replace, pool).await?;
}
Commands::Clean => {
tdlist.clean_todo_db(pool).await?;
}
Commands::List => {
tdlist.list_todos();
}
Commands::Tui => {
tdlist = crate::tui::app::run_tui(tdlist)?;
}
Commands::Path => {
let db_path = data_dir.join("todos.db");
let toml_path = data_dir.join("todos.toml");
println!("Data directory: {}", data_dir.display());
println!("Database: {}", db_path.display());
println!("TOML export: {}", toml_path.display());
}
}
tdlist.sync_to_db(pool).await?;
Ok(tdlist)
}
#[cfg(test)]
mod args_test {
use super::*;
#[test]
fn test_add() {
let args = vec!["oflow", "add", "Buy milk"];
let cli = Cli::try_parse_from(args).expect("Failed to parse arguments");
match cli.command {
Commands::Add { content } => {
assert_eq!(content, "Buy milk");
}
_ => panic!("Expected Add command"),
}
}
#[test]
fn test_finish() {
let args = vec!["oflow", "finish", "Buy milk"];
let cli = Cli::try_parse_from(args).expect("Failed to parse arguments");
match cli.command {
Commands::Finish { content } => {
assert_eq!(content, "Buy milk");
}
_ => panic!("Expect Finish command"),
}
}
#[test]
fn test_edit() {
let args = vec!["oflow", "edit", "Buy milk", "Buy eggs"];
let cli = Cli::try_parse_from(args).expect("Failed to parse arguments");
match cli.command {
Commands::Edit { find, replace } => {
assert_eq!(find, "Buy milk");
assert_eq!(replace, "Buy eggs");
}
_ => panic!("Expect Edit command"),
}
}
}