use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};
pub const LOG_RETENTION_DAYS: u64 = 7;
const LOG_FILE_PREFIX: &str = "omnyssh.log";
pub fn ssh_config_path() -> Option<PathBuf> {
dirs::home_dir().map(|h| h.join(".ssh").join("config"))
}
pub fn app_config_dir() -> Option<PathBuf> {
dirs::config_dir().map(|d| d.join("omnyssh"))
}
pub fn app_config_path() -> Option<PathBuf> {
app_config_dir().map(|d| d.join("config.toml"))
}
pub fn hosts_config_path() -> Option<PathBuf> {
app_config_dir().map(|d| d.join("hosts.toml"))
}
pub fn snippets_config_path() -> Option<PathBuf> {
app_config_dir().map(|d| d.join("snippets.toml"))
}
pub fn cleanup_old_logs(log_dir: &Path, max_age_days: u64) -> usize {
let entries = match std::fs::read_dir(log_dir) {
Ok(entries) => entries,
Err(_) => return 0,
};
let max_age = Duration::from_secs(max_age_days * 24 * 60 * 60);
let now = SystemTime::now();
let mut removed = 0;
for entry in entries.flatten() {
let path = entry.path();
let is_log_file = path
.file_name()
.and_then(|name| name.to_str())
.map(|name| name.starts_with(LOG_FILE_PREFIX))
.unwrap_or(false);
if !is_log_file {
continue;
}
let modified = match entry.metadata().and_then(|meta| meta.modified()) {
Ok(modified) => modified,
Err(_) => continue,
};
if let Ok(age) = now.duration_since(modified) {
if age > max_age && std::fs::remove_file(&path).is_ok() {
removed += 1;
}
}
}
removed
}