diff --git a/bin/src/main.rs b/bin/src/main.rs index f9b37ac..52cc89e 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -5,6 +5,7 @@ use pop_launcher_toolkit::plugins; use pop_launcher_toolkit::service; use mimalloc::MiMalloc; +use pop_launcher_toolkit::service::ensure_cache_path; #[global_allocator] static GLOBAL: MiMalloc = MiMalloc; @@ -38,14 +39,7 @@ async fn main() { } fn init_logging(cmd: &str) { - let logdir = match dirs::state_dir() { - Some(dir) => dir.join("pop-launcher/"), - None => dirs::home_dir() - .expect("home directory required") - .join(".cache/pop-launcher"), - }; - - let _ = std::fs::create_dir_all(&logdir); + let logdir = ensure_cache_path().expect("failed to get cache path for saving logs"); let logfile = std::fs::OpenOptions::new() .create(true) diff --git a/service/src/lib.rs b/service/src/lib.rs index d151ba7..351c7d7 100644 --- a/service/src/lib.rs +++ b/service/src/lib.rs @@ -46,36 +46,40 @@ pub struct PluginHelp { } pub fn ensure_cache_path() -> Result> { - let cachepath = dirs::home_dir() - .ok_or("failed to find home dir")? - .join(".cache/pop-launcher"); - std::fs::create_dir_all(&cachepath)?; - Ok(cachepath.join("recent")) + let cache_path = dirs::state_dir() + .unwrap_or( + dirs::home_dir() + .ok_or("failed to find home dir")? + .join(".cache"), + ) + .join("pop-launcher"); + std::fs::create_dir_all(&cache_path)?; + Ok(cache_path) } pub fn store_cache(storage: &RecentUseStorage) { + let cache_path = ensure_cache_path(); let write_recent = || -> Result<(), Box> { - let cachepath = ensure_cache_path()?; Ok(serde_json::to_writer( - std::fs::File::create(cachepath)?, + std::fs::File::create(cache_path?.join("recent"))?, storage, )?) }; if let Err(e) = write_recent() { - eprintln!("could not write to cache file\n{}", e); + eprintln!("could not write to recent file\n{}", e); } } pub async fn main() { - let cachepath = ensure_cache_path(); + let cache_path = ensure_cache_path(); let read_recent = || -> Result> { - let cachepath = std::fs::File::open(cachepath?)?; - Ok(serde_json::from_reader(cachepath)?) + let recent_file = std::fs::File::open(cache_path?.join("recent"))?; + Ok(serde_json::from_reader(recent_file)?) }; let recent = match read_recent() { Ok(r) => r, Err(e) => { - eprintln!("could not read cache file\n{}", e); + eprintln!("could not read recent file\n{}", e); RecentUseStorage::default() } };