aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2022-02-15 19:16:27 +0000
committerdiogo464 <[email protected]>2022-02-15 19:16:27 +0000
commit80bcbb07c92ebe737376ecfb867917c8324d77d8 (patch)
tree1c62926a0a04df045e1fb46d9a32c428c4a0180d
parent2b04d646aec90e0fe28e84356b13758b8a956e8a (diff)
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.
-rw-r--r--src/dotup.rs8
-rw-r--r--src/utils.rs22
2 files changed, 29 insertions, 1 deletions
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 {
132 pub fn link(&mut self, origin: impl AsRef<Path>, destination: impl AsRef<Path>) { 132 pub fn link(&mut self, origin: impl AsRef<Path>, destination: impl AsRef<Path>) {
133 let link_result: anyhow::Result<()> = try { 133 let link_result: anyhow::Result<()> = try {
134 let origin = self.prepare_relative_origin(origin.as_ref())?; 134 let origin = self.prepare_relative_origin(origin.as_ref())?;
135 let destination = self.prepare_relative_destination(destination.as_ref())?; 135 let destination_ends_with_slash = utils::path_ends_with_slash(destination.as_ref());
136 let mut destination = self.prepare_relative_destination(destination.as_ref())?;
137 if destination_ends_with_slash {
138 if let Some(filename) = origin.file_name() {
139 destination.push(filename);
140 }
141 }
136 self.depot.link_create(origin, destination)?; 142 self.depot.link_create(origin, destination)?;
137 }; 143 };
138 match link_result { 144 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 {
125 std::env::current_dir().expect("Failed to obtain current working directory") 125 std::env::current_dir().expect("Failed to obtain current working directory")
126} 126}
127 127
128pub fn path_ends_with_slash(path: impl AsRef<Path>) -> bool {
129 path.as_ref()
130 .to_str()
131 .map(|s| s.ends_with('/'))
132 .unwrap_or_default()
133}
134
128#[cfg(test)] 135#[cfg(test)]
129mod tests { 136mod tests {
130 use super::*; 137 use super::*;
@@ -153,4 +160,19 @@ mod tests {
153 weakly_canonical_cwd("configs/nvim/lua/setup.lua", cwd) 160 weakly_canonical_cwd("configs/nvim/lua/setup.lua", cwd)
154 ); 161 );
155 } 162 }
163
164 #[test]
165 fn test_path_ends_with_slash() {
166 assert!(!path_ends_with_slash(""));
167 assert!(!path_ends_with_slash("/f1"));
168 assert!(!path_ends_with_slash("/f1/f2"));
169 assert!(!path_ends_with_slash("./f1/f2"));
170 assert!(!path_ends_with_slash("./f1/f2/../f3"));
171
172 assert!(path_ends_with_slash("/"));
173 assert!(path_ends_with_slash("/f1/"));
174 assert!(path_ends_with_slash("f1/"));
175 assert!(path_ends_with_slash("f1/f2/"));
176 assert!(path_ends_with_slash("f1/f2/../f3/"));
177 }
156} 178}