kasl/commands/mod.rs
1//! Command-line interface commands for kasl application.
2//!
3//! Contains all CLI command implementations for task management, activity monitoring,
4//! reporting, and system configuration.
5//!
6//! ## Features
7//!
8//! - **Core Commands**: Task management, activity monitoring, reporting, export
9//! - **Utility Commands**: Configuration, summaries, pauses, adjustments, updates
10//! - **Advanced Commands**: Templates, tags, database migrations
11//!
12//! ## Usage
13//!
14//! ```bash
15//! kasl watch # Start activity monitoring
16//! kasl task --name "Review code" # Create a new task
17//! kasl report # Generate today's report
18//! kasl export tasks --format csv # Export tasks to CSV
19//! ```
20
21pub mod autostart;
22pub mod export;
23pub mod inbox;
24pub mod init;
25pub mod migrations;
26pub mod pauses;
27pub mod report;
28pub mod sum;
29pub mod tag;
30pub mod task;
31pub mod template;
32pub mod update;
33pub mod watch;
34
35use crate::{db::workdays::Workdays, libs::messages::types::Message, msg_info};
36use anyhow::Result;
37use chrono::Local;
38use clap::{Parser, Subcommand};
39
40/// Defines the main subcommands that the application can execute.
41///
42/// Each variant corresponds to a specific command with its own argument structure.
43/// Commands are organized by functionality and frequency of use.
44#[derive(Debug, Subcommand)]
45enum Commands {
46 /// Manage autostart configuration for system boot
47 ///
48 /// Controls whether kasl automatically starts monitoring when the system boots.
49 /// Supports both system-level and user-level autostart on Windows.
50 #[command(about = "Manage autostart on system boot")]
51 Autostart(autostart::AutostartArgs),
52
53 /// Initialize application configuration interactively
54 ///
55 /// Guides the user through setting up API credentials, monitor settings,
56 /// and other configuration options required for kasl to function properly.
57 #[command(about = "Configuration initialization")]
58 Init(init::InitArgs),
59
60 /// Comprehensive task management command
61 ///
62 /// Handles all task-related operations including creation, editing, deletion,
63 /// viewing, and integration with external services like GitLab and Jira.
64 #[command(about = "Create task")]
65 Task(task::TaskArgs),
66
67 /// Manually end the current workday
68 ///
69 /// Records the end timestamp for today's work session. Typically used
70 /// when the automatic monitoring needs to be manually finalized.
71 #[command(about = "Write end timestamp to database")]
72 End,
73
74 /// Display monthly working hours summary
75 ///
76 /// Shows a comprehensive overview of work hours, productivity metrics,
77 /// and daily breakdowns for the current month.
78 #[command(about = "Get summary")]
79 Sum(sum::SumArgs),
80
81 /// Update application to the latest version
82 ///
83 /// Checks GitHub releases for newer versions and automatically downloads
84 /// and installs updates if available.
85 #[command(about = "Update the application to the latest version")]
86 Update,
87
88 /// Generate and optionally submit work reports
89 ///
90 /// Creates detailed daily reports with work intervals, tasks, and productivity
91 /// metrics. Can automatically submit reports to configured APIs.
92 #[command(about = "Prepare a report")]
93 Report(report::ReportArgs),
94
95 /// Export application data to external formats
96 ///
97 /// Supports exporting tasks, reports, and summaries to CSV, JSON, and Excel
98 /// formats for external analysis or backup purposes.
99 #[command(about = "Export data to various formats")]
100 Export(export::ExportArgs),
101
102 /// Manage reusable task templates
103 ///
104 /// Create, edit, and use templates for frequently created tasks to
105 /// streamline task creation workflow.
106 #[command(about = "Manage task templates")]
107 Template(template::TemplateArgs),
108
109 /// Organize tasks with custom tags
110 ///
111 /// Create and manage tags to categorize and filter tasks by project,
112 /// priority, or any custom criteria.
113 #[command(about = "Manage task tags")]
114 Tag(tag::TagArgs),
115
116 /// Background activity monitoring daemon
117 ///
118 /// Monitors user input activity to automatically detect work sessions,
119 /// breaks, and workday boundaries. Can run as a background service.
120 #[command(about = "Watch user activity in the background to record pauses")]
121 Watch(watch::WatchArgs),
122
123 /// View recorded pauses and record ones the monitor missed
124 ///
125 /// Lists detected pauses for a date, and lets the user add an absence the
126 /// activity monitor did not catch or remove one recorded by mistake.
127 #[command(about = "View pauses and record ones the monitor missed")]
128 Pauses(pauses::PausesArgs),
129
130 /// Jira inbox of assigned open issues
131 ///
132 /// Syncs assigned unresolved Jira issues into a local table, lists them
133 /// by priority, and supports pin / dismiss / open / import into tasks.
134 #[command(about = "Manage Jira inbox issues")]
135 Inbox(inbox::InboxArgs),
136
137 /// Print a shell completion script
138 ///
139 /// Emits the completion script for the chosen shell on stdout; source it
140 /// from the shell profile to get completion for kasl's commands and flags.
141 #[command(about = "Print a shell completion script (source it from your shell profile)")]
142 Completions {
143 /// Target shell
144 shell: clap_complete::Shell,
145 },
146
147 /// Database migration management utilities (debug builds only)
148 ///
149 /// Provides tools for database schema management, migration history,
150 /// and rollback operations. Available only in debug builds for safety.
151 #[cfg(debug_assertions)]
152 #[command(about = "Database migration management")]
153 Migrations(migrations::MigrationsArgs),
154}
155
156/// The main CLI structure that parses command-line arguments.
157///
158/// Uses `clap` to define the application's interface and delegates
159/// command execution to the appropriate subcommand module. The CLI
160/// requires at least one subcommand to be specified.
161///
162/// # Examples
163///
164/// ```bash
165/// # Display help
166/// kasl --help
167///
168/// # Run a specific command
169/// kasl task --name "New task"
170/// ```
171#[derive(Debug, Parser)]
172#[command(name = "kasl", author, version, about, long_about = None)]
173#[command(arg_required_else_help(true))]
174pub struct Cli {
175 #[command(subcommand)]
176 command: Commands,
177}
178
179impl Cli {
180 /// Parses command-line arguments and executes the corresponding command.
181 ///
182 /// This is the main entry point for the CLI logic. It handles command
183 /// routing and provides centralized error handling for all commands.
184 ///
185 /// # Returns
186 ///
187 /// Returns `Ok(())` on successful command execution, or an error if
188 /// the command fails or invalid arguments are provided.
189 ///
190 /// # Examples
191 ///
192 /// ```rust,no_run
193 /// use kasl::commands::Cli;
194 ///
195 /// #[tokio::main]
196 /// async fn main() -> anyhow::Result<()> {
197 /// Cli::menu().await
198 /// }
199 /// ```
200 pub async fn menu() -> Result<()> {
201 let cli = Self::parse();
202
203 match cli.command {
204 Commands::Autostart(args) => autostart::cmd(args),
205 Commands::Init(args) => init::cmd(args),
206 Commands::Task(args) => task::cmd(args).await,
207 Commands::End => {
208 // Manually end the current workday
209 Workdays::new()?.insert_end(Local::now().date_naive())?;
210 msg_info!(Message::WorkdayEnded);
211 Ok(())
212 }
213 Commands::Sum(args) => sum::cmd(args).await,
214 Commands::Report(args) => report::cmd(args).await,
215 Commands::Export(args) => export::cmd(args).await,
216 Commands::Template(args) => template::cmd(args),
217 Commands::Tag(args) => tag::cmd(args).await,
218 Commands::Update => update::cmd().await,
219 Commands::Watch(args) => watch::cmd(args).await,
220 Commands::Pauses(args) => pauses::cmd(args).await,
221 Commands::Completions { shell } => {
222 use clap::CommandFactory;
223 clap_complete::generate(shell, &mut Self::command(), "kasl", &mut std::io::stdout());
224 Ok(())
225 }
226 Commands::Inbox(args) => inbox::cmd(args).await,
227
228 // Database migrations only available in debug builds
229 #[cfg(debug_assertions)]
230 Commands::Migrations(args) => migrations::cmd(args),
231 }
232 }
233}