kordophoned: better daemon bootstrapping
This commit is contained in:
@@ -1,9 +1,46 @@
|
||||
use directories::ProjectDirs;
|
||||
use std::path::PathBuf;
|
||||
use anyhow::Result;
|
||||
|
||||
use kordophone_db::{
|
||||
database::Database,
|
||||
settings::Settings,
|
||||
models::Conversation,
|
||||
};
|
||||
|
||||
pub struct Daemon {
|
||||
pub version: String,
|
||||
database: Database,
|
||||
}
|
||||
|
||||
impl Daemon {
|
||||
pub fn new() -> Self {
|
||||
Self { version: "0.1.0".to_string() }
|
||||
pub fn new() -> Result<Self> {
|
||||
let database_path = Self::get_database_path();
|
||||
log::info!("Database path: {}", database_path.display());
|
||||
|
||||
// Create the database directory if it doesn't exist
|
||||
let database_dir = database_path.parent().unwrap();
|
||||
std::fs::create_dir_all(database_dir)?;
|
||||
|
||||
let database = Database::new(&database_path.to_string_lossy())?;
|
||||
Ok(Self { version: "0.1.0".to_string(), database })
|
||||
}
|
||||
|
||||
pub fn get_version(&self) -> String {
|
||||
self.version.clone()
|
||||
}
|
||||
|
||||
pub fn get_conversations(&mut self) -> Vec<Conversation> {
|
||||
self.database.with_repository(|r| r.all_conversations().unwrap())
|
||||
}
|
||||
|
||||
fn get_database_path() -> PathBuf {
|
||||
if let Some(proj_dirs) = ProjectDirs::from("com", "kordophone", "kordophone") {
|
||||
let data_dir = proj_dirs.data_dir();
|
||||
data_dir.join("database.db")
|
||||
} else {
|
||||
// Fallback to a local path if we can't get the system directories
|
||||
PathBuf::from("database.db")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user