kimun-notes 0.11.2

A terminal-based notes application
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// tui/src/cli/commands/workspace.rs
//
// Workspace management CLI commands: init, list, use, rename, remove, reindex.

use std::path::PathBuf;

use clap::Subcommand;
use color_eyre::eyre::{Result, eyre};
use kimun_core::error::VaultError;
use kimun_core::{NoteVault, VaultConfig};

use crate::settings::{
    AppSettings, config_migration::CURRENT_CONFIG_VERSION, workspace_config::WorkspaceConfig,
};

#[derive(Subcommand, Debug)]
pub enum WorkspaceSubcommand {
    /// Initialize a new workspace
    Init {
        /// Name for the workspace (defaults to "default" for first workspace)
        #[arg(long)]
        name: Option<String>,
        /// Path to the workspace directory
        path: PathBuf,
    },
    /// List all configured workspaces
    List,
    /// Switch to a different workspace
    Use {
        /// Name of the workspace to switch to
        name: String,
    },
    /// Rename a workspace
    Rename {
        /// Current workspace name
        old_name: String,
        /// New workspace name
        new_name: String,
    },
    /// Remove a workspace from the configuration
    Remove {
        /// Name of the workspace to remove
        name: String,
    },
    /// Reindex a workspace
    Reindex {
        /// Workspace name (defaults to current workspace)
        #[arg(long)]
        name: Option<String>,
    },
}

pub async fn run(subcommand: WorkspaceSubcommand, settings: &mut AppSettings) -> Result<()> {
    match subcommand {
        WorkspaceSubcommand::Init { name, path } => run_init(settings, name, path).await,
        WorkspaceSubcommand::List => run_list(settings),
        WorkspaceSubcommand::Use { name } => run_use(settings, name),
        WorkspaceSubcommand::Rename { old_name, new_name } => {
            run_rename(settings, old_name, new_name)
        }
        WorkspaceSubcommand::Remove { name } => run_remove(settings, name),
        WorkspaceSubcommand::Reindex { name } => run_reindex(settings, name).await,
    }
}

async fn run_init(settings: &mut AppSettings, name: Option<String>, path: PathBuf) -> Result<()> {
    // Ensure workspace_config exists
    if settings.workspace_config.is_none() {
        settings.workspace_config = Some(WorkspaceConfig::new_empty());
    }

    let ws_config = settings
        .workspace_config
        .as_ref()
        .expect("workspace_config must exist after init");

    // Workspace name is lowercased here because it backs case-insensitive
    // cache and history filenames; same lowering must apply to the DB path
    // computed below and the eventual add_workspace key.
    let workspace_name = match name {
        Some(n) => n.to_lowercase(),
        None => {
            if ws_config.workspaces.is_empty() {
                "default".to_string()
            } else {
                return Err(eyre!(
                    "A workspace name is required when other workspaces already exist. \
                     Use: kimun workspace init --name <name> <path>"
                ));
            }
        }
    };

    if ws_config.workspaces.contains_key(&workspace_name) {
        let existing_path = &ws_config.workspaces[&workspace_name].path;
        return Err(eyre!(
            "Workspace '{}' already exists at {}. \
             Use a different name or remove the existing workspace first.",
            workspace_name,
            existing_path.display()
        ));
    }

    // Validate/create the target path
    let created = !path.exists();
    let canonical_path = kimun_core::ensure_dir_exists(&path).map_err(|e| {
        eyre!(
            "Failed to create workspace directory {}: {}",
            path.display(),
            e
        )
    })?;
    if created {
        println!("Created directory: {}", path.display());
    }

    println!("Initializing workspace database...");
    let cache_path = settings.cache_path_for(&workspace_name);
    let vault = NoteVault::new(VaultConfig::new(&canonical_path).with_db_path(cache_path))
        .await
        .map_err(|e| {
            eyre!(
                "Failed to create vault at {}: {}",
                canonical_path.display(),
                e
            )
        })?;
    vault
        .validate_and_init()
        .await
        .map_err(|e| eyre!("Failed to initialize vault database: {}", e))?;

    let ws_config_mut = settings
        .workspace_config
        .as_mut()
        .expect("workspace_config must exist after init");
    ws_config_mut
        .add_workspace(workspace_name.clone(), canonical_path.clone())
        .map_err(|e| eyre!("{}", e))?;

    settings.config_version = CURRENT_CONFIG_VERSION;
    settings.save_to_disk()?;

    println!(
        "Workspace '{}' initialized at {}",
        workspace_name,
        canonical_path.display()
    );

    let ws_config = settings
        .workspace_config
        .as_ref()
        .expect("workspace_config must exist after init");
    if ws_config.global.current_workspace == workspace_name {
        println!("Set as current workspace.");
    }

    Ok(())
}

fn run_list(settings: &AppSettings) -> Result<()> {
    match &settings.workspace_config {
        None => {
            println!("No workspaces configured. Run 'kimun workspace init <path>' to create one.");
        }
        Some(ws_config) => {
            if ws_config.workspaces.is_empty() {
                println!(
                    "No workspaces configured. Run 'kimun workspace init <path>' to create one."
                );
            } else {
                println!("Configured workspaces:");
                let mut names: Vec<&String> = ws_config.workspaces.keys().collect();
                names.sort();
                for name in names {
                    let entry = &ws_config.workspaces[name];
                    let marker = if name == &ws_config.global.current_workspace {
                        "* "
                    } else {
                        "  "
                    };
                    println!("{}{}  ({})", marker, name, entry.path.display());
                }
            }
        }
    }
    Ok(())
}

fn run_use(settings: &mut AppSettings, name: String) -> Result<()> {
    let ws_config = settings
        .workspace_config
        .as_ref()
        .ok_or_else(|| eyre!("No workspaces configured."))?;

    let entry = ws_config.get_workspace(&name).ok_or_else(|| {
        let available: Vec<&String> = ws_config.workspaces.keys().collect();
        eyre!(
            "Workspace '{}' not found. Available workspaces: {}",
            name,
            available
                .iter()
                .map(|s| s.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        )
    })?;

    // Validate workspace path still exists
    if !entry.effective_path().exists() {
        return Err(eyre!(
            "Workspace '{}' path no longer exists: {}. \
             Update the path or remove this workspace.",
            name,
            entry.effective_path().display()
        ));
    }

    settings
        .workspace_config
        .as_mut()
        .expect("workspace_config must exist")
        .global
        .current_workspace = name.clone();
    settings.save_to_disk()?;

    println!("Switched to workspace '{}'.", name);
    Ok(())
}

fn run_rename(settings: &mut AppSettings, old_name: String, new_name: String) -> Result<()> {
    let new_name = new_name.to_lowercase();
    kimun_core::nfs::filename::validate_filename(&new_name).map_err(|e| eyre!("{}", e))?;

    let ws_config = settings
        .workspace_config
        .as_ref()
        .ok_or_else(|| eyre!("No workspaces configured."))?;

    if !ws_config.workspaces.contains_key(&old_name) {
        return Err(eyre!("Workspace '{}' not found.", old_name));
    }

    if ws_config.workspaces.contains_key(&new_name) {
        return Err(eyre!(
            "Workspace '{}' already exists. Choose a different name.",
            new_name
        ));
    }

    // Move cache and history files BEFORE mutating config so a failed
    // file move doesn't leave the config pointing at a workspace whose
    // cache is in the wrong place.
    let old_cache = settings.cache_path_for(&old_name);
    let new_cache = settings.cache_path_for(&new_name);
    let old_history = settings.history_path_for(&old_name);
    let new_history = settings.history_path_for(&new_name);

    if new_cache.exists() {
        return Err(eyre!(
            "Destination cache already exists at {}. Refusing to overwrite.",
            new_cache.display()
        ));
    }
    if new_history.exists() {
        return Err(eyre!(
            "Destination history already exists at {}. Refusing to overwrite.",
            new_history.display()
        ));
    }
    if old_cache.exists() {
        std::fs::rename(&old_cache, &new_cache).map_err(|e| {
            eyre!(
                "failed to move cache {} -> {}: {}",
                old_cache.display(),
                new_cache.display(),
                e
            )
        })?;
    }
    if old_history.exists() {
        std::fs::rename(&old_history, &new_history).map_err(|e| {
            eyre!(
                "failed to move history {} -> {}: {}",
                old_history.display(),
                new_history.display(),
                e
            )
        })?;
    }

    let ws_config_mut = settings
        .workspace_config
        .as_mut()
        .expect("workspace_config must exist after init");

    let entry = ws_config_mut
        .workspaces
        .remove(&old_name)
        .expect("entry must exist (checked above)");
    ws_config_mut.workspaces.insert(new_name.clone(), entry);

    if ws_config_mut.global.current_workspace == old_name {
        ws_config_mut.global.current_workspace = new_name.clone();
    }

    settings.save_to_disk()?;

    println!("Workspace '{}' renamed to '{}'.", old_name, new_name);
    Ok(())
}

fn run_remove(settings: &mut AppSettings, name: String) -> Result<()> {
    let ws_config = settings
        .workspace_config
        .as_ref()
        .ok_or_else(|| eyre!("No workspaces configured."))?;

    if !ws_config.workspaces.contains_key(&name) {
        return Err(eyre!("Workspace '{}' not found.", name));
    }

    if ws_config.global.current_workspace == name {
        return Err(eyre!(
            "Cannot remove the current workspace '{}'. \
             Switch to a different workspace first with: kimun workspace use <name>",
            name
        ));
    }

    let cache_path = settings.cache_path_for(&name);
    let history_path = settings.history_path_for(&name);

    settings
        .workspace_config
        .as_mut()
        .expect("workspace_config must exist")
        .workspaces
        .remove(&name);

    settings.save_to_disk()?;

    for path in [&cache_path, &history_path] {
        if path.exists() {
            match std::fs::remove_file(path) {
                Ok(()) => tracing::info!("removed {}", path.display()),
                Err(e) => tracing::warn!("failed to remove {}: {}", path.display(), e),
            }
        }
    }

    println!("Workspace '{}' removed.", name);
    Ok(())
}

async fn run_reindex(settings: &AppSettings, name: Option<String>) -> Result<()> {
    let ws_config = settings
        .workspace_config
        .as_ref()
        .ok_or_else(|| eyre!("No workspaces configured."))?;

    let workspace_name = match name {
        Some(n) => n,
        None => ws_config.global.current_workspace.clone(),
    };

    if workspace_name.is_empty() {
        return Err(eyre!("No current workspace set. Specify a workspace name."));
    }

    let entry = ws_config
        .get_workspace(&workspace_name)
        .ok_or_else(|| eyre!("Workspace '{}' not found.", workspace_name))?;

    if !entry.effective_path().exists() {
        return Err(eyre!(
            "Workspace '{}' path no longer exists: {}",
            workspace_name,
            entry.effective_path().display()
        ));
    }

    println!("Reindexing workspace '{}'...", workspace_name);

    let cache_path = settings.cache_path_for(&workspace_name);
    let workspace_path = entry.effective_path().clone();
    let vault = NoteVault::new(VaultConfig::new(&workspace_path).with_db_path(cache_path))
        .await
        .map_err(|e| {
            eyre!(
                "Failed to open vault at {}: {}",
                workspace_path.display(),
                e
            )
        })?;

    let report = match vault.recreate_index().await {
        Ok(r) => r,
        Err(VaultError::CaseConflict { conflicts }) => {
            eprintln!(
                "Error: vault '{}' has case-sensitivity conflicts:",
                workspace_name
            );
            for c in &conflicts {
                eprintln!("  {}", c);
            }
            eprintln!(
                "\nResolve the conflicts on disk, then run `kimun workspace use {}` to re-select the vault.",
                workspace_name
            );
            return Err(eyre!(
                "Vault '{}' has case-sensitivity conflicts",
                workspace_name
            ));
        }
        Err(e) => {
            return Err(eyre!(
                "Failed to reindex workspace '{}': {}",
                workspace_name,
                e
            ));
        }
    };

    let _ = report; // IndexReport only contains timing info
    println!("Reindex complete for workspace '{}'.", workspace_name);

    Ok(())
}