1pub mod commands;
3pub mod helpers;
4pub mod json_output;
5pub mod metadata_extractor;
6pub mod output;
7
8use clap::Subcommand;
9use color_eyre::eyre::{Result, eyre};
10use commands::JournalArgs;
11use commands::note_ops::NoteSubcommand;
12use commands::workspace::WorkspaceSubcommand;
13use helpers::{
14 create_and_init_vault, load_and_resolve_workspace, load_settings, resolve_inbox_path,
15 resolve_quick_note_path,
16};
17use kimun_core::{NoteVault, VaultConfig};
18use output::OutputFormat;
19
20#[derive(Subcommand)]
21pub enum CliCommand {
22 Search {
24 query: String,
25 #[arg(long, value_enum, default_value = "text")]
26 format: OutputFormat,
27 },
28 Notes {
30 #[arg(long, help = "Filter notes by path prefix")]
31 path: Option<String>,
32 #[arg(long, value_enum, default_value = "text")]
33 format: OutputFormat,
34 },
35 Workspace {
37 #[command(subcommand)]
38 subcommand: WorkspaceSubcommand,
39 },
40 Note {
42 #[command(subcommand)]
43 subcommand: NoteSubcommand,
44 },
45 Journal(JournalArgs),
47 Mcp,
49 Labels {
51 #[arg(long, value_enum, default_value = "text")]
52 format: OutputFormat,
53 },
54}
55
56pub async fn run_cli(command: CliCommand, config_path: Option<std::path::PathBuf>) -> Result<()> {
57 match command {
58 CliCommand::Workspace { subcommand } => {
59 let mut settings = load_settings(config_path)?;
60 commands::workspace::run(subcommand, &mut settings).await
61 }
62 CliCommand::Note { subcommand } => {
63 let (settings, workspace_path, workspace_name) =
64 load_and_resolve_workspace(config_path)?;
65 let quick_note_path = resolve_quick_note_path(&settings);
66 let inbox_path = resolve_inbox_path(&settings);
67 let cache_path = settings.cache_path_for(&workspace_name);
68 let mut vault =
69 NoteVault::new(VaultConfig::new(&workspace_path).with_db_path(cache_path)).await?;
70 vault.set_inbox_path(kimun_core::nfs::VaultPath::new(&inbox_path));
71 match vault.validate().await? {
72 kimun_core::DBStatus::Ready => {
73 commands::note_ops::run(subcommand, &vault, &quick_note_path, &workspace_name)
74 .await
75 }
76 status => Err(eyre!(
77 "Workspace index is not ready ({status}).\nRun `kimun workspace reindex` to initialise it."
78 )),
79 }
80 }
81 CliCommand::Search { query, format } => {
82 let (vault, workspace_name) = create_and_init_vault(config_path).await?;
83 commands::search::run(&vault, &query, format, &workspace_name, false).await
84 }
85 CliCommand::Notes { path, format } => {
86 let (vault, workspace_name) = create_and_init_vault(config_path).await?;
87 commands::notes::run(&vault, path.as_deref(), format, &workspace_name, false).await
88 }
89 CliCommand::Journal(args) => {
90 let (vault, workspace_name) = create_and_init_vault(config_path).await?;
91 commands::journal::run(args, &vault, &workspace_name).await
92 }
93 CliCommand::Mcp => commands::mcp::run(config_path).await,
94 CliCommand::Labels { format } => {
95 let (vault, workspace_name) = create_and_init_vault(config_path).await?;
96 commands::labels::run(&vault, format, &workspace_name).await
97 }
98 }
99}