1use crate::cli::handlers::CommandCtx;
4use crate::cli::Commands;
5use crate::db::Database;
6use crate::utils::Result;
7use clap_complete::Shell;
8
9pub struct CommandHandler {
10 db: Database,
11}
12
13impl CommandHandler {
14 pub fn new() -> Result<Self> {
15 let db = Database::new()?;
16 Ok(Self { db })
17 }
18
19 #[allow(dead_code)]
20 pub fn from_db(db: Database) -> Self {
21 Self { db }
22 }
23
24 #[allow(dead_code)]
27 pub fn get_db(&self) -> &Database {
28 &self.db
29 }
30
31 pub fn handle(&self, command: Commands) -> Result<()> {
32 let ctx = CommandCtx::new(&self.db);
33 match command {
34 Commands::New {
35 name,
36 description,
37 ticket,
38 ticket_url,
39 template,
40 } => super::handlers::handle_new(
41 &ctx,
42 &name,
43 description.as_deref(),
44 ticket.as_deref(),
45 ticket_url.as_deref(),
46 template.as_deref(),
47 ),
48 Commands::List { all } => super::handlers::handle_list(&ctx, all),
49 Commands::Switch { task_ref } => super::handlers::handle_switch(&ctx, &task_ref),
50 Commands::Status { id, json, all } => super::handlers::handle_info(&ctx, id, json, all),
51 Commands::Desc { description, task } => {
52 super::handlers::handle_desc(&ctx, description.as_deref(), task)
53 }
54 Commands::Ticket {
55 ticket_id,
56 url,
57 task,
58 } => super::handlers::handle_ticket(&ctx, &ticket_id, &url, task),
59 Commands::Archive { task_ref } => {
60 super::handlers::handle_archive(&ctx, task_ref.as_deref())
61 }
62 Commands::Todo(cmd) => super::handlers::handle_todo(&ctx, cmd),
63 Commands::Link(cmd) => super::handlers::handle_link(&ctx, cmd),
64 Commands::Scrap(cmd) => super::handlers::handle_scrap(&ctx, cmd),
65 Commands::Sync => super::handlers::handle_sync(&ctx),
66 Commands::Repo(cmd) => super::handlers::handle_repo(&ctx, cmd),
67 Commands::Alias(cmd) => super::handlers::handle_alias(&ctx, cmd),
68 Commands::LlmHelp => super::handlers::handle_llm_help(&ctx),
69 Commands::Completion { shell, dynamic } => {
70 super::handlers::handle_completion(&ctx, shell, dynamic)
71 }
72 Commands::Complete { completion_type } => {
73 super::handlers::handle_complete(&ctx, completion_type)
74 }
75 Commands::Config(cmd) => super::handlers::handle_config(&ctx, cmd),
76 Commands::Webui { .. } => unreachable!("Webui command is handled in main.rs"),
77 }
78 }
79
80 #[allow(dead_code)]
81 pub(crate) fn handle_llm_help(&self) -> Result<()> {
82 super::handlers::handle_llm_help(&CommandCtx::new(&self.db))
83 }
84
85 #[allow(dead_code)]
86 pub(crate) fn handle_completion(&self, shell: Shell, dynamic: bool) -> Result<()> {
87 super::handlers::handle_completion(&CommandCtx::new(&self.db), shell, dynamic)
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94 use crate::db::Database;
95
96 #[test]
97 fn test_llm_help() {
98 let db = Database::new_in_memory().unwrap();
99 let handler = CommandHandler::from_db(db);
100
101 let result = handler.handle_llm_help();
102 assert!(result.is_ok());
103 }
104
105 #[test]
106 fn test_completion() {
107 let db = Database::new_in_memory().unwrap();
108 let handler = CommandHandler::from_db(db);
109
110 for shell in [
111 clap_complete::Shell::Bash,
112 clap_complete::Shell::Zsh,
113 clap_complete::Shell::Fish,
114 clap_complete::Shell::PowerShell,
115 ] {
116 let result = handler.handle_completion(shell, false);
117 assert!(
118 result.is_ok(),
119 "Static completion generation failed for {:?}",
120 shell
121 );
122 }
123
124 for shell in [clap_complete::Shell::Bash, clap_complete::Shell::Zsh] {
125 let result = handler.handle_completion(shell, true);
126 assert!(
127 result.is_ok(),
128 "Dynamic completion generation failed for {:?}",
129 shell
130 );
131 }
132 }
133}