aboutsummaryrefslogtreecommitdiff
path: root/src/dots.rs
diff options
context:
space:
mode:
author[object Object] <max@bossi.ng>2025-07-16 11:51:18 +0200
committerGitHub <noreply@github.com>2025-07-16 11:51:18 +0200
commitb284510c1b66c9b830883d04097239756694221c (patch)
treed96c3975d78c4cf2ba0203e89d8d3bf8d56155dc /src/dots.rs
parentc40e7f89ecacce4fccf3403124f187f64ce7131b (diff)
feat!: dots-unlinking and ux changes
* start of unlink implementation * refactor: move dot logic out of main mod * feat: finish implementing unlinking of dots * chore: add test files to gitignore * chore: "use" cleanup * chore: add explanation and additional info to cli * chore: add crate description * chore: bump version to 2.0.0
Diffstat (limited to 'src/dots.rs')
-rw-r--r--src/dots.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/dots.rs b/src/dots.rs
new file mode 100644
index 0000000..5b75254
--- /dev/null
+++ b/src/dots.rs
@@ -0,0 +1,48 @@
+use std::fs::{remove_file, symlink_metadata};
+use std::os::unix::fs::symlink;
+use std::path::PathBuf;
+use dirs::home_dir;
+use crate::config::Dot;
+
+pub fn deploy_dots(dots: Vec<Dot>, dots_dir: PathBuf) {
+ let prepended_dots = dots.iter().map(|m|
+ Dot {
+ source: dots_dir.join(&m.source),
+ destination: prepend_user_dir(&m.destination)
+ }
+ );
+
+ for dot in prepended_dots {
+ println!("linking from {} to {}", dot.source.display(), dot.destination.display());
+ let _ = symlink(&dot.source, &dot.destination).map_err(|err|
+ eprintln!("failed to symlink: {}", err.to_string())
+ );
+ }
+}
+
+pub fn unlink_dots(dots: Vec<Dot>, dots_dir: PathBuf) {
+ let prepended_dots = dots.iter().map(|m|
+ Dot {
+ source: dots_dir.join(&m.source),
+ destination: prepend_user_dir(&m.destination)
+ }
+ );
+
+ for dot in prepended_dots {
+ println!("unlinking {}", dot.destination.display());
+ let metadata = symlink_metadata(dot.destination.clone());
+ if metadata.is_err() {
+ eprintln!("failed to query metadata for {}: {}", dot.destination.display(), metadata.err().unwrap());
+ continue
+ }
+ if metadata.ok().unwrap().is_symlink() {
+ let _ = remove_file(dot.destination).map_err(|err|
+ eprintln!("failed to remove symlink: {}", err)
+ );
+ }
+ }
+}
+
+fn prepend_user_dir(path: &PathBuf) -> PathBuf {
+ home_dir().unwrap().join(path)
+} \ No newline at end of file