use home::home_dir;
use once_cell::sync::OnceCell;
use std::{fs, path::PathBuf};
pub struct Config {
pub store_path: PathBuf,
pub sock_path: PathBuf,
pub pid_path: PathBuf,
pub log_dir: PathBuf,
}
static CONFIG: OnceCell<Config> = OnceCell::new();
pub fn init() {
if CONFIG.get().is_some() {
return;
}
let base_dir = home_dir()
.expect("Cannot determine the home directory")
.join(".proses");
let log_dir = base_dir.join("logs");
fs::create_dir_all(&base_dir).expect("Failed to create ~/.proses directory");
fs::create_dir_all(&log_dir).expect("Failed to create ~/.proses/logs directory");
let config = Config {
store_path: base_dir.join("store.json"),
sock_path: base_dir.join("proses.sock"),
pid_path: base_dir.join("daemon.pid"),
log_dir,
};
CONFIG.set(config).ok();
}
#[inline]
pub fn get() -> &'static Config {
CONFIG
.get()
.expect("Config not initialised — call config::init() first")
}