Skip to content

Commit

Permalink
added group fallback for symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphGL committed Jan 17, 2025
1 parent 7861f54 commit 79b9cbb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
26 changes: 26 additions & 0 deletions src/dotfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,32 @@ pub const VALID_TARGETS: &[&str] = &[
"_windows",
];

pub fn get_highest_priority_target_idx(targets: &[impl AsRef<str>]) -> Option<usize> {
if targets.is_empty() {
return None;
}

let mut highest_priority = 0;
let mut highest_idx = 0;

for (idx, target) in targets.iter().enumerate() {
let target = target.as_ref();

let target_priority = match target {
"_unix" | "_windows" => 2,
_ if !group_ends_with_target_name(target) => 0,
_ => 1,
};

if target_priority >= highest_priority {
highest_priority = target_priority;
highest_idx = idx;
}
}

Some(highest_idx)
}

// Exit codes
/// Couldn't find the dotfiles directory
pub enum ReturnCode {
Expand Down
14 changes: 11 additions & 3 deletions src/symlinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,30 @@ impl SymlinkHandler {

/// Symlinks all the files of a group to the user's $HOME
fn add(&self, group: &str) {
let Some(groups) =
let Some(mut groups) =
self.get_related_conditional_groups(group, SymlinkType::NotSymlinked.into())
else {
return;
};

for group in groups {
loop {
let Some(idx) = dotfiles::get_highest_priority_target_idx(&groups) else {
break;
};

let group = &groups[idx];
let group = Dotfile::try_from(self.dotfiles_dir.join("Configs").join(&group)).unwrap();
if group.path.exists() {
// iterate through all the files in group_dir
group.try_iter().unwrap().for_each(|f| symlink_file(f.path));
group.try_iter().unwrap().for_each(|f| symlink_file(f.path))
} else {
eprintln!(
"{}",
t!("errors.no_dotfiles_for_group", group = group.group_name).red()
);
}

groups.remove(idx);
}
}

Expand Down Expand Up @@ -533,6 +540,7 @@ pub fn add_cmd(
}
}
};

// Symlink dotfile by force
if force {
remove_files_and_decide_if_adopt(&sym.not_owned, false);
Expand Down

0 comments on commit 79b9cbb

Please sign in to comment.