From 80bcbb07c92ebe737376ecfb867917c8324d77d8 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Tue, 15 Feb 2022 19:16:27 +0000 Subject: improved link command if the destination path ends with a slash then it is considered a directory and the links will be made under that directory. otherwise all the links will have the same destination. --- src/dotup.rs | 8 +++++++- src/utils.rs | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/dotup.rs b/src/dotup.rs index 5f1287f..8de7920 100644 --- a/src/dotup.rs +++ b/src/dotup.rs @@ -132,7 +132,13 @@ impl Dotup { pub fn link(&mut self, origin: impl AsRef, destination: impl AsRef) { let link_result: anyhow::Result<()> = try { let origin = self.prepare_relative_origin(origin.as_ref())?; - let destination = self.prepare_relative_destination(destination.as_ref())?; + let destination_ends_with_slash = utils::path_ends_with_slash(destination.as_ref()); + let mut destination = self.prepare_relative_destination(destination.as_ref())?; + if destination_ends_with_slash { + if let Some(filename) = origin.file_name() { + destination.push(filename); + } + } self.depot.link_create(origin, destination)?; }; match link_result { diff --git a/src/utils.rs b/src/utils.rs index a7e945a..556bad3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -125,6 +125,13 @@ pub fn current_working_directory() -> PathBuf { std::env::current_dir().expect("Failed to obtain current working directory") } +pub fn path_ends_with_slash(path: impl AsRef) -> bool { + path.as_ref() + .to_str() + .map(|s| s.ends_with('/')) + .unwrap_or_default() +} + #[cfg(test)] mod tests { use super::*; @@ -153,4 +160,19 @@ mod tests { weakly_canonical_cwd("configs/nvim/lua/setup.lua", cwd) ); } + + #[test] + fn test_path_ends_with_slash() { + assert!(!path_ends_with_slash("")); + assert!(!path_ends_with_slash("/f1")); + assert!(!path_ends_with_slash("/f1/f2")); + assert!(!path_ends_with_slash("./f1/f2")); + assert!(!path_ends_with_slash("./f1/f2/../f3")); + + assert!(path_ends_with_slash("/")); + assert!(path_ends_with_slash("/f1/")); + assert!(path_ends_with_slash("f1/")); + assert!(path_ends_with_slash("f1/f2/")); + assert!(path_ends_with_slash("f1/f2/../f3/")); + } } -- cgit