Skip to content

Commit

Permalink
feat: Allow for both .config and standard ~/Library/... dir on mac
Browse files Browse the repository at this point in the history
  • Loading branch information
elkowar committed Dec 22, 2024
1 parent 2356918 commit ab5e155
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
4 changes: 0 additions & 4 deletions src/script/sysinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ pub struct SystemInfoPaths {
config_dir: PathBuf,
#[rhai_type(readonly)]
home_dir: PathBuf,
#[rhai_type(readonly)]
yolk_dir: PathBuf,
}

impl SystemInfo {
Expand All @@ -50,7 +48,6 @@ impl SystemInfo {
cache_dir: dirs::cache_dir().unwrap_or_else(|| "unknown".into()),
config_dir: dirs::config_dir().unwrap_or_else(|| "unknown".into()),
home_dir: dirs::home_dir().unwrap_or_else(|| "unknown".into()),
yolk_dir: crate::yolk_paths::default_yolk_dir(),
},
}
}
Expand All @@ -63,7 +60,6 @@ impl SystemInfo {
cache_dir: (PathBuf::from("/canonical/cache")),
config_dir: (PathBuf::from("/canonical/config")),
home_dir: (PathBuf::from("/canonical/home")),
yolk_dir: PathBuf::from("/canonical/yolk"),
},
distro: "distro".to_string(),
device_name: "devicename".to_string(),
Expand Down
10 changes: 5 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use cached::UnboundCache;
use miette::{Context as _, IntoDiagnostic as _};
use regex::Regex;

use crate::yolk_paths::default_yolk_dir;

/// Rename or move a file, but only if the destination doesn't exist.
/// This is a safer verison of [`std::fs::rename`] that doesn't overwrite files.
pub fn rename_safely(original: impl AsRef<Path>, new: impl AsRef<Path>) -> miette::Result<()> {
Expand Down Expand Up @@ -58,11 +60,9 @@ impl Path {
///
/// This replaces the home path with `~`, as well as reducing paths that point into the eggs directory to `eggs/rest/of/path`.
fn abbr(&self) -> String {
match (
dirs::home_dir(),
dirs::config_dir().map(|x| x.join("yolk").join("eggs")),
) {
(Some(home), Some(eggs)) => self
let eggs = default_yolk_dir().join("eggs");
match dirs::home_dir() {
Some(home) => self
.strip_prefix(&eggs)
.map(|x| PathBuf::from("eggs").join(x))
.or_else(|_| self.strip_prefix(&home).map(|x| PathBuf::from("~").join(x)))
Expand Down
16 changes: 14 additions & 2 deletions src/yolk_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,21 @@ pub struct YolkPaths {
}

pub fn default_yolk_dir() -> PathBuf {
dirs::config_dir()
let standard_dir = dirs::config_dir()
.expect("No config dir available")
.join("yolk")
.join("yolk");
if standard_dir.exists() {
standard_dir
} else {
let config_dir = if let Ok(config_dir) = std::env::var("XDG_CONFIG_DIR") {
PathBuf::from(config_dir)
} else {
dirs::home_dir()
.expect("No home dir available")
.join(".config")
};
config_dir.join("yolk")
}
}

impl YolkPaths {
Expand Down

0 comments on commit ab5e155

Please sign in to comment.