Expand ~ and shell variables in dirs config (#538)

This commit is contained in:
vaw
2025-10-25 19:52:14 +00:00
committed by GitHub
parent 55456dbc1e
commit 14aa97251c
3 changed files with 58 additions and 16 deletions

10
Cargo.lock generated
View File

@@ -2287,6 +2287,7 @@ dependencies = [
"rpassword", "rpassword",
"serde", "serde",
"serde_json", "serde_json",
"shellexpand",
"sled", "sled",
"temp-dir", "temp-dir",
"thiserror 1.0.69", "thiserror 1.0.69",
@@ -5108,6 +5109,15 @@ dependencies = [
"lazy_static 1.5.0", "lazy_static 1.5.0",
] ]
[[package]]
name = "shellexpand"
version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b1fdf65dd6331831494dd616b30351c38e96e45921a27745cf98490458b90bb"
dependencies = [
"dirs",
]
[[package]] [[package]]
name = "shlex" name = "shlex"
version = "1.3.0" version = "1.3.0"

View File

@@ -65,6 +65,7 @@ url = {version = "^2.2.2", features = ["serde"]}
edit = "0.1.4" edit = "0.1.4"
humansize = "2.0.0" humansize = "2.0.0"
linkify = "0.10.0" linkify = "0.10.0"
shellexpand = "3.1.1"
[dependencies.comrak] [dependencies.comrak]
version = "0.22.0" version = "0.22.0"

View File

@@ -707,11 +707,11 @@ impl DirectoryValues {
#[derive(Clone, Default, Deserialize)] #[derive(Clone, Default, Deserialize)]
pub struct Directories { pub struct Directories {
pub cache: Option<PathBuf>, pub cache: Option<String>,
pub data: Option<PathBuf>, pub data: Option<String>,
pub logs: Option<PathBuf>, pub logs: Option<String>,
pub downloads: Option<PathBuf>, pub downloads: Option<String>,
pub image_previews: Option<PathBuf>, pub image_previews: Option<String>,
} }
impl Directories { impl Directories {
@@ -728,6 +728,11 @@ impl Directories {
fn values(self) -> DirectoryValues { fn values(self) -> DirectoryValues {
let cache = self let cache = self
.cache .cache
.map(|dir| {
let dir = shellexpand::full(&dir)
.expect("unable to expand shell variables in dirs.cache");
Path::new(dir.as_ref()).to_owned()
})
.or_else(|| { .or_else(|| {
let mut dir = dirs::cache_dir()?; let mut dir = dirs::cache_dir()?;
dir.push("iamb"); dir.push("iamb");
@@ -737,6 +742,11 @@ impl Directories {
let data = self let data = self
.data .data
.map(|dir| {
let dir = shellexpand::full(&dir)
.expect("unable to expand shell variables in dirs.cache");
Path::new(dir.as_ref()).to_owned()
})
.or_else(|| { .or_else(|| {
let mut dir = dirs::data_dir()?; let mut dir = dirs::data_dir()?;
dir.push("iamb"); dir.push("iamb");
@@ -744,15 +754,36 @@ impl Directories {
}) })
.expect("no dirs.data value configured!"); .expect("no dirs.data value configured!");
let logs = self.logs.unwrap_or_else(|| { let logs = self
.logs
.map(|dir| {
let dir = shellexpand::full(&dir)
.expect("unable to expand shell variables in dirs.cache");
Path::new(dir.as_ref()).to_owned()
})
.unwrap_or_else(|| {
let mut dir = cache.clone(); let mut dir = cache.clone();
dir.push("logs"); dir.push("logs");
dir dir
}); });
let downloads = self.downloads.or_else(dirs::download_dir); let downloads = self
.downloads
.map(|dir| {
let dir = shellexpand::full(&dir)
.expect("unable to expand shell variables in dirs.cache");
Path::new(dir.as_ref()).to_owned()
})
.or_else(dirs::download_dir);
let image_previews = self.image_previews.unwrap_or_else(|| { let image_previews = self
.image_previews
.map(|dir| {
let dir = shellexpand::full(&dir)
.expect("unable to expand shell variables in dirs.cache");
Path::new(dir.as_ref()).to_owned()
})
.unwrap_or_else(|| {
let mut dir = cache.clone(); let mut dir = cache.clone();
dir.push("image_preview_downloads"); dir.push("image_preview_downloads");
dir dir