Skip to main content

kimun_notes/cli/
helpers.rs

1// tui/src/cli/helpers.rs
2//
3// Common helper functions for CLI operations to reduce code duplication.
4
5use std::path::PathBuf;
6use color_eyre::eyre::Result;
7use kimun_core::NoteVault;
8use kimun_core::nfs::{VaultPath, PATH_SEPARATOR};
9use crate::settings::AppSettings;
10
11/// Load settings from either a specific config file path or the default location.
12pub fn load_settings(config_path: Option<PathBuf>) -> Result<AppSettings> {
13    match config_path {
14        Some(path) => AppSettings::load_from_file(path),
15        None => AppSettings::load_from_disk(),
16    }
17}
18
19/// Resolve workspace configuration from settings, returning the workspace path and name.
20///
21/// Returns an error if no workspace is configured.
22pub fn resolve_workspace_config(settings: &AppSettings) -> Result<(PathBuf, String)> {
23    // Check legacy workspace_dir first (Phase 1 compatibility)
24    if let Some(dir) = &settings.workspace_dir {
25        return Ok((dir.clone(), "default".to_string()));
26    }
27
28    // Check Phase 2 workspace configuration
29    if let Some(ref ws_config) = settings.workspace_config {
30        if let Some(entry) = ws_config.get_current_workspace() {
31            let name = ws_config.global.current_workspace.clone();
32            return Ok((entry.path.clone(), name));
33        }
34    }
35
36    Err(color_eyre::eyre::eyre!("No workspace configured. Run 'kimun' to set up a workspace."))
37}
38
39/// Load settings and resolve workspace configuration in one operation.
40///
41/// This is a convenience function that combines loading settings and resolving
42/// the workspace configuration, which is a common pattern in CLI commands.
43pub fn load_and_resolve_workspace(config_path: Option<PathBuf>) -> Result<(AppSettings, PathBuf, String)> {
44    let settings = load_settings(config_path)?;
45    let (workspace_path, workspace_name) = resolve_workspace_config(&settings)?;
46    Ok((settings, workspace_path, workspace_name))
47}
48
49/// Returns the configured quick_note_path for the active workspace.
50/// Falls back to VaultPath::root() for Phase 1 workspaces (no WorkspaceEntry) or if not configured.
51pub fn resolve_quick_note_path(settings: &AppSettings) -> String {
52    let root = kimun_core::nfs::VaultPath::root().to_string();
53    // Phase 1 legacy: workspace_dir only, no WorkspaceEntry
54    if settings.workspace_dir.is_some() {
55        return root;
56    }
57    // Phase 2: workspace_config
58    if let Some(ref ws_config) = settings.workspace_config {
59        if let Some(entry) = ws_config.get_current_workspace() {
60            return entry.effective_quick_note_path();
61        }
62    }
63    root
64}
65
66/// Resolve a user-provided note path string into a VaultPath.
67///
68/// Rules:
69/// - Empty or whitespace-only input → error
70/// - Starts with PATH_SEPARATOR → absolute from vault root (quick_note_path ignored)
71/// - Otherwise → relative, joined with quick_note_path using PATH_SEPARATOR
72/// - VaultPath::note_path_from normalizes path and ensures .md extension
73pub fn resolve_note_path(input: &str, quick_note_path: &str) -> Result<VaultPath> {
74    let trimmed = input.trim();
75    if trimmed.is_empty() {
76        return Err(color_eyre::eyre::eyre!("Note path cannot be empty or whitespace-only"));
77    }
78    if trimmed.len() == 1 && trimmed.starts_with(PATH_SEPARATOR) {
79        return Err(color_eyre::eyre::eyre!("Note path cannot be the root separator alone"));
80    }
81    let raw = if trimmed.starts_with(PATH_SEPARATOR) {
82        trimmed.to_string()
83    } else {
84        let base = if quick_note_path.trim().is_empty() {
85            VaultPath::root().to_string()
86        } else {
87            quick_note_path.trim_end_matches(PATH_SEPARATOR).to_string()
88        };
89        format!("{}{}{}", base, PATH_SEPARATOR, trimmed)
90    };
91    Ok(VaultPath::note_path_from(&raw))
92}
93
94/// Create and initialize a vault from workspace configuration.
95///
96/// This handles the common pattern of creating a NoteVault from workspace settings
97/// and initializing/validating its database.
98pub async fn create_and_init_vault(
99    config_path: Option<PathBuf>
100) -> Result<(NoteVault, String)> {
101    let (_settings, workspace_path, workspace_name) = load_and_resolve_workspace(config_path)?;
102
103    let vault = NoteVault::new(&workspace_path).await?;
104    vault.validate_and_init().await?;
105
106    Ok((vault, workspace_name))
107}