use std::process::Command;
use crate::config::{AppConfig, GlobalConfig};
use crate::db;
pub fn run(note_identifier: String) {
let global = GlobalConfig::load();
let Some(notebook_path) = global.notebook_path() else {
eprintln!("Notebook not initialized");
std::process::exit(1);
};
let config = AppConfig::load();
let mut file_path = notebook_path.join(¬e_identifier);
if !file_path.exists() {
match db::list_notes(¬ebook_path) {
Ok(notes) => {
if let Some(note) = notes.iter().find(|n| n.title == note_identifier || n.filename == note_identifier) {
file_path = notebook_path.join(¬e.filename);
} else {
eprintln!("Note not found: {}", note_identifier);
std::process::exit(1);
}
}
Err(e) => {
eprintln!("Database error: {}", e);
std::process::exit(1);
}
}
}
let status = Command::new(&config.smarana.editor)
.arg(&file_path)
.status();
match status {
Ok(s) if !s.success() => {
eprintln!("Editor exited with non-zero status");
}
Err(e) => {
eprintln!("Failed to open editor '{}': {}", config.smarana.editor, e);
}
_ => {}
}
db::sync_file(¬ebook_path, &file_path);
}