Specify cache dir with BAT_CACHE_DIR

- if set, BAT_CACHE_DIR is used. otherwise use OS default.
This commit is contained in:
Kyle Criddle 2020-03-15 19:52:11 -06:00 committed by David Peter
parent 9a9902bf9b
commit a9a31dca7f

View file

@ -1,8 +1,6 @@
use crate::dirs_rs; use crate::dirs_rs;
use std::path::{Path, PathBuf};
#[cfg(target_os = "macos")]
use std::env; use std::env;
use std::path::{Path, PathBuf};
/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification. /// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
/// This means that the `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` environment variables are /// This means that the `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` environment variables are
@ -14,16 +12,7 @@ pub struct BatProjectDirs {
impl BatProjectDirs { impl BatProjectDirs {
fn new() -> Option<BatProjectDirs> { fn new() -> Option<BatProjectDirs> {
#[cfg(target_os = "macos")] let cache_dir = BatProjectDirs::get_cache_dir()?;
let cache_dir_op = env::var_os("XDG_CACHE_HOME")
.map(PathBuf::from)
.filter(|p| p.is_absolute())
.or_else(|| dirs_rs::home_dir().map(|d| d.join(".cache")));
#[cfg(not(target_os = "macos"))]
let cache_dir_op = dirs_rs::cache_dir();
let cache_dir = cache_dir_op.map(|d| d.join("bat"))?;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
let config_dir_op = env::var_os("XDG_CONFIG_HOME") let config_dir_op = env::var_os("XDG_CONFIG_HOME")
@ -42,6 +31,27 @@ impl BatProjectDirs {
}) })
} }
pub fn get_cache_dir() -> Option<PathBuf> {
// on all OS prefer BAT_CACHE_PATH if set
let cache_dir_op = env::var_os("BAT_CACHE_PATH")
.map(PathBuf::from)
.filter(|p| p.is_absolute());
if !cache_dir_op.is_none() {
return cache_dir_op;
}
#[cfg(target_os = "macos")]
let cache_dir_op = env::var_os("XDG_CACHE_HOME")
.map(PathBuf::from)
.filter(|p| p.is_absolute())
.or_else(|| dirs_rs::home_dir().map(|d| d.join(".cache")));
#[cfg(not(target_os = "macos"))]
let cache_dir_op = dirs_rs::cache_dir();
Some(cache_dir_op.map(|d| d.join("bat")))?
}
pub fn cache_dir(&self) -> &Path { pub fn cache_dir(&self) -> &Path {
&self.cache_dir &self.cache_dir
} }