Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(service): be consistent in getting cache path #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 16 additions & 12 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,40 @@ pub struct PluginHelp {
}

pub fn ensure_cache_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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<RecentUseStorage, Box<dyn std::error::Error>> {
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()
}
};
Expand Down