Skip to main content

kasl/
lib.rs

1//! # Kasl - Key Activity Synchronization and Logging
2//!
3//! A command-line utility for tracking work activities, managing tasks,
4//! and generating productivity reports.
5//!
6//! ## Usage
7//!
8//! ```rust,no_run
9//! use kasl::commands::Cli;
10//!
11//! #[tokio::main]
12//! async fn main() -> anyhow::Result<()> {
13//!     Cli::menu().await
14//! }
15//! ```
16
17pub mod api;
18pub mod commands;
19pub mod db;
20pub mod libs;
21
22/// Runs the CLI: the shared entry point behind both the `kasl` and `ka` binaries.
23///
24/// Sets up tracing, handles the internal `--daemon-run` mode used when the
25/// watcher spawns itself, and otherwise dispatches the parsed command.
26///
27/// # Errors
28///
29/// Propagates whatever the executed command fails with.
30pub fn run() -> anyhow::Result<()> {
31    let runtime = tokio::runtime::Runtime::new()?;
32    runtime.block_on(async {
33        // Initialize tracing only if debug mode is enabled; otherwise log output
34        // would clutter normal CLI usage.
35        if std::env::var("KASL_DEBUG").is_ok() || std::env::var("RUST_LOG").is_ok() {
36            tracing_subscriber::fmt()
37                .with_env_filter(tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "kasl=debug".into()))
38                .init();
39        }
40
41        // Intercepted before clap: this flag is how the background monitoring
42        // process is launched, not part of the public command surface.
43        let args: Vec<String> = std::env::args().collect();
44        if args.len() > 1 && args[1] == "--daemon-run" {
45            commands::watch::run_as_daemon().await?;
46        } else {
47            // Non-blocking; only surfaces a notification when one is due.
48            libs::update::Updater::show_update_notification().await;
49
50            commands::Cli::menu().await?;
51        }
52
53        Ok(())
54    })
55}