kasl/commands/init.rs
1//! Application configuration initialization command.
2//!
3//! Provides an interactive setup wizard that guides users through configuring kasl for first-time use.
4//!
5//! ## Features
6//!
7//! - **Interactive Setup**: Guided configuration wizard for all settings
8//! - **API Integration**: Configure GitLab, Jira, and custom API credentials
9//! - **Monitoring Settings**: Set up activity thresholds and productivity parameters
10//! - **PATH Integration**: Automatically adds kasl to system PATH
11//! - **Reset Capability**: Remove existing configuration for troubleshooting
12//!
13//! ## Usage
14//!
15//! ```bash
16//! # Run interactive setup wizard
17//! kasl init
18//!
19//! # Reset configuration (remove existing settings)
20//! kasl init --delete
21//! ```
22
23use crate::{
24 libs::{config::Config, daemon, messages::Message},
25 msg_info, msg_success, msg_warning,
26};
27use anyhow::Result;
28use clap::Args;
29
30/// Command-line arguments for the initialization command.
31///
32/// The init command supports an optional `--delete` flag for removing
33/// existing configuration, which can be useful for testing or troubleshooting.
34#[derive(Debug, Args)]
35pub struct InitArgs {
36 /// Remove existing configuration instead of creating new one
37 ///
38 /// When specified, this flag will delete the current configuration file
39 /// and global PATH settings, effectively resetting the application to
40 /// its initial state.
41 #[arg(short, long)]
42 delete: bool,
43}
44
45/// Executes the initialization command.
46///
47/// Handles configuration setup with interactive wizard for first-time setup,
48/// or configuration removal when `--delete` is used.
49///
50/// # Arguments
51///
52/// * `init_args` - Parsed command-line arguments containing options
53///
54/// # Returns
55///
56/// Returns `Ok(())` on successful configuration, or an error if the setup fails.
57pub fn cmd(init_args: InitArgs) -> Result<()> {
58 // Check if watcher is currently running before making changes
59 let watcher_was_running = daemon::is_running();
60 if watcher_was_running {
61 msg_info!(Message::WatcherStoppingForConfig);
62 daemon::stop()?;
63 }
64
65 // Set up global application PATH configuration
66 // This ensures the 'kasl' command is available system-wide
67 match Config::set_app_global() {
68 Ok(()) => {
69 msg_success!(Message::PathConfigured);
70 }
71 Err(e) => {
72 msg_warning!(Message::PathConfigWarning { error: e.to_string() });
73 }
74 }
75
76 // Handle deletion mode - exit early after cleanup
77 if init_args.delete {
78 // Don't restart watcher after deleting configuration
79 msg_info!(Message::ConfigDeleted);
80 return Ok(());
81 }
82
83 // Run interactive configuration wizard
84 // This will prompt the user to select and configure various modules
85 Config::init()?.save()?;
86
87 // Confirm successful configuration
88 msg_success!(Message::ConfigSaved);
89
90 // Restart watcher if it was running before configuration changes
91 if watcher_was_running {
92 msg_info!(Message::WatcherRestartingAfterConfig);
93 match daemon::spawn() {
94 Ok(()) => {
95 msg_success!(Message::WatcherRestarted);
96 }
97 Err(e) => {
98 msg_warning!(Message::WatcherRestartFailed { error: e.to_string() });
99 }
100 }
101 }
102
103 Ok(())
104}