use crate::error::Result;
use std::env;
use std::fs;
use std::path::PathBuf;
pub const SPECIAL_CHAR: char = ':';
pub const PID_FILENAME: &str = "snipt-daemon.pid";
pub const DB_FILENAME: &str = "snipt.json";
pub const EXECUTE_CHAR: char = '!';
pub fn get_config_dir() -> PathBuf {
env::var("HOME")
.map(|home| PathBuf::from(home).join(".snipt"))
.unwrap_or_else(|_| PathBuf::from(".snipt"))
}
pub fn ensure_config_dir() -> Result<PathBuf> {
let config_dir = get_config_dir();
if !config_dir.exists() {
fs::create_dir_all(&config_dir)?;
}
let db_path = get_db_file_path();
if !db_path.exists() {
create_empty_file(&db_path, "database file")?;
}
Ok(config_dir)
}
pub fn create_empty_file(path: &PathBuf, description: &str) -> Result<()> {
println!("Creating {} at: {}", description, path.display());
fs::write(path, "")?;
Ok(())
}
pub fn get_pid_file_path() -> PathBuf {
get_config_dir().join(PID_FILENAME)
}
pub fn get_db_file_path() -> PathBuf {
get_config_dir().join(DB_FILENAME)
}
pub fn db_file_exists() -> bool {
get_db_file_path().exists()
}
pub fn is_daemon_running() -> Result<Option<u32>> {
let pid_file = get_pid_file_path();
if pid_file.exists() {
match fs::read_to_string(&pid_file) {
Ok(contents) => {
match contents.trim().parse::<u32>() {
Ok(pid) => Ok(Some(pid)),
Err(_) => {
let _ = fs::remove_file(&pid_file);
Ok(None)
}
}
}
Err(_) => {
let _ = fs::remove_file(&pid_file);
Ok(None)
}
}
} else {
Ok(None)
}
}