mirror of
https://github.com/0xrsydn/idx-cli.git
synced 2026-08-07 01:33:52 +00:00
fix: stabilize cache behavior and local xdg paths
This commit is contained in:
parent
e20b41642f
commit
018b8aeb55
2 changed files with 17 additions and 2 deletions
14
src/cache.rs
14
src/cache.rs
|
|
@ -53,7 +53,7 @@ impl Cache {
|
||||||
};
|
};
|
||||||
let age = Utc::now().signed_duration_since(entry.fetched_at);
|
let age = Utc::now().signed_duration_since(entry.fetched_at);
|
||||||
if age
|
if age
|
||||||
> chrono::Duration::from_std(Duration::from_secs(entry.ttl_secs))
|
>= chrono::Duration::from_std(Duration::from_secs(entry.ttl_secs))
|
||||||
.map_err(|e| IdxError::CacheMiss(e.to_string()))?
|
.map_err(|e| IdxError::CacheMiss(e.to_string()))?
|
||||||
{
|
{
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
|
|
@ -204,6 +204,11 @@ impl Cache {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cache_dir() -> Result<PathBuf, IdxError> {
|
pub fn cache_dir() -> Result<PathBuf, IdxError> {
|
||||||
|
if let Ok(dir) = std::env::var("XDG_CACHE_HOME")
|
||||||
|
&& !dir.is_empty()
|
||||||
|
{
|
||||||
|
return Ok(PathBuf::from(dir).join("idx"));
|
||||||
|
}
|
||||||
ProjectDirs::from("", "", "idx")
|
ProjectDirs::from("", "", "idx")
|
||||||
.map(|d| d.cache_dir().to_path_buf())
|
.map(|d| d.cache_dir().to_path_buf())
|
||||||
.ok_or_else(|| IdxError::ConfigError("unable to resolve cache dir".to_string()))
|
.ok_or_else(|| IdxError::ConfigError("unable to resolve cache dir".to_string()))
|
||||||
|
|
@ -212,18 +217,23 @@ pub fn cache_dir() -> Result<PathBuf, IdxError> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use super::{Cache, CacheEntry};
|
use super::{Cache, CacheEntry};
|
||||||
|
|
||||||
|
static TMP_COUNTER: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
||||||
struct T {
|
struct T {
|
||||||
v: i32,
|
v: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tmp() -> std::path::PathBuf {
|
fn tmp() -> std::path::PathBuf {
|
||||||
let p = std::env::temp_dir().join(format!("idx-cache-test-{}", std::process::id()));
|
let suffix = TMP_COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||||
|
let p =
|
||||||
|
std::env::temp_dir().join(format!("idx-cache-test-{}-{suffix}", std::process::id()));
|
||||||
let _ = fs::remove_dir_all(&p);
|
let _ = fs::remove_dir_all(&p);
|
||||||
fs::create_dir_all(&p).expect("create tmp cache dir");
|
fs::create_dir_all(&p).expect("create tmp cache dir");
|
||||||
p
|
p
|
||||||
|
|
|
||||||
|
|
@ -197,6 +197,11 @@ pub fn default_config_toml() -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn config_path() -> Result<PathBuf, IdxError> {
|
pub fn config_path() -> Result<PathBuf, IdxError> {
|
||||||
|
if let Ok(dir) = std::env::var("XDG_CONFIG_HOME")
|
||||||
|
&& !dir.is_empty()
|
||||||
|
{
|
||||||
|
return Ok(PathBuf::from(dir).join("idx").join("config.toml"));
|
||||||
|
}
|
||||||
ProjectDirs::from("", "", "idx")
|
ProjectDirs::from("", "", "idx")
|
||||||
.map(|d| d.config_dir().join("config.toml"))
|
.map(|d| d.config_dir().join("config.toml"))
|
||||||
.ok_or_else(|| IdxError::ConfigError("unable to resolve config dir".to_string()))
|
.ok_or_else(|| IdxError::ConfigError("unable to resolve config dir".to_string()))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue