aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2022-02-08 09:16:44 +0000
committerdiogo464 <[email protected]>2022-02-08 09:18:12 +0000
commit3e5d7e936980575b47acbaa7668082af35bf042a (patch)
treead6cd93be60ef2363723dac1ca06fcf1d5564222 /src/main.rs
parent3d98462d7e026cf27eda3775337f5bd15147bef9 (diff)
snapshot
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs86
1 files changed, 37 insertions, 49 deletions
diff --git a/src/main.rs b/src/main.rs
index b4f951d..7b38357 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -43,7 +43,7 @@ mod depot {
43 } 43 }
44 44
45 #[derive(Debug, Clone, PartialEq, Eq)] 45 #[derive(Debug, Clone, PartialEq, Eq)]
46 pub enum NodeSearchResult { 46 enum NodeSearchResult {
47 Found(NodeID), 47 Found(NodeID),
48 /// the closest NodeID up the the search point. 48 /// the closest NodeID up the the search point.
49 NotFound(NodeID), 49 NotFound(NodeID),
@@ -436,6 +436,7 @@ mod depot {
436 self.link_move_unchecked(link_id, destination) 436 self.link_move_unchecked(link_id, destination)
437 } 437 }
438 438
439 #[allow(unused)]
439 pub fn link_search(&self, path: impl AsRef<Path>) -> Result<SearchResult> { 440 pub fn link_search(&self, path: impl AsRef<Path>) -> Result<SearchResult> {
440 let path = path.as_ref(); 441 let path = path.as_ref();
441 path_verify(path)?; 442 path_verify(path)?;
@@ -568,7 +569,7 @@ mod depot {
568 } 569 }
569 570
570 fn path_parent_or_empty(path: &Path) -> &Path { 571 fn path_parent_or_empty(path: &Path) -> &Path {
571 path.parent().unwrap_or(Path::new("")) 572 path.parent().unwrap_or_else(|| Path::new(""))
572 } 573 }
573 574
574 /// Iterate over the components of a path. 575 /// Iterate over the components of a path.
@@ -879,7 +880,6 @@ pub mod dotup {
879 880
880 #[derive(Debug)] 881 #[derive(Debug)]
881 struct CanonicalPair { 882 struct CanonicalPair {
882 link_id: LinkID,
883 origin: PathBuf, 883 origin: PathBuf,
884 destination: PathBuf, 884 destination: PathBuf,
885 } 885 }
@@ -1028,11 +1028,7 @@ pub mod dotup {
1028 1028
1029 pub fn install(&self, paths: impl Iterator<Item = impl AsRef<Path>>) { 1029 pub fn install(&self, paths: impl Iterator<Item = impl AsRef<Path>>) {
1030 let install_result: anyhow::Result<()> = try { 1030 let install_result: anyhow::Result<()> = try {
1031 let mut link_ids = HashSet::<LinkID>::default(); 1031 let link_ids = self.link_ids_from_paths_iter(paths)?;
1032 for path in paths {
1033 let path = self.prepare_relative_path(path.as_ref())?;
1034 link_ids.extend(self.depot.links_under(&path)?);
1035 }
1036 self.depot.links_verify_install(link_ids.iter().copied())?; 1032 self.depot.links_verify_install(link_ids.iter().copied())?;
1037 1033
1038 for link_id in link_ids { 1034 for link_id in link_ids {
@@ -1045,20 +1041,16 @@ pub mod dotup {
1045 } 1041 }
1046 1042
1047 pub fn uninstall(&self, paths: impl Iterator<Item = impl AsRef<Path>>) { 1043 pub fn uninstall(&self, paths: impl Iterator<Item = impl AsRef<Path>>) {
1048 for origin in paths { 1044 let uninstall_result: anyhow::Result<()> = try {
1049 let uninstall_result: anyhow::Result<()> = try { 1045 let link_ids = self.link_ids_from_paths_iter(paths)?;
1050 let origin = self.prepare_relative_path(origin.as_ref())?; 1046 for link_id in link_ids {
1051 let canonical_pairs = self.canonical_pairs_under(&origin)?; 1047 if self.symlink_is_installed_by_link_id(link_id)? {
1052 for pair in canonical_pairs { 1048 self.symlink_uninstall_by_link_id(link_id)?;
1053 self.symlink_uninstall(&pair.origin, &pair.destination)?;
1054 } 1049 }
1055 };
1056 if let Err(e) = uninstall_result {
1057 println!(
1058 "error while uninstalling {} : {e}",
1059 origin.as_ref().display()
1060 );
1061 } 1050 }
1051 };
1052 if let Err(e) = uninstall_result {
1053 println!("error while uninstalling {e}",);
1062 } 1054 }
1063 } 1055 }
1064 1056
@@ -1135,7 +1127,7 @@ pub mod dotup {
1135 fn status_path_to_item(&self, canonical_path: &Path) -> anyhow::Result<StatusItem> { 1127 fn status_path_to_item(&self, canonical_path: &Path) -> anyhow::Result<StatusItem> {
1136 debug_assert!(canonical_path.is_absolute()); 1128 debug_assert!(canonical_path.is_absolute());
1137 debug_assert!(canonical_path.exists()); 1129 debug_assert!(canonical_path.exists());
1138 let relative_path = self.prepare_relative_path(&canonical_path)?; 1130 let relative_path = self.prepare_relative_path(canonical_path)?;
1139 1131
1140 let item = if canonical_path.is_dir() { 1132 let item = if canonical_path.is_dir() {
1141 if let Some(link_id) = self.depot.link_find(&relative_path)? { 1133 if let Some(link_id) = self.depot.link_find(&relative_path)? {
@@ -1189,19 +1181,17 @@ pub mod dotup {
1189 is_directory: true, 1181 is_directory: true,
1190 } 1182 }
1191 } 1183 }
1184 } else if let Some(link_id) = self.depot.link_find(&relative_path)? {
1185 let destination = self.depot.link_view(link_id).destination().to_owned();
1186 StatusItem::Link {
1187 origin: relative_path,
1188 destination,
1189 is_directory: false,
1190 }
1192 } else { 1191 } else {
1193 if let Some(link_id) = self.depot.link_find(&relative_path)? { 1192 StatusItem::Unlinked {
1194 let destination = self.depot.link_view(link_id).destination().to_owned(); 1193 origin: relative_path,
1195 StatusItem::Link { 1194 is_directory: false,
1196 origin: relative_path,
1197 destination,
1198 is_directory: false,
1199 }
1200 } else {
1201 StatusItem::Unlinked {
1202 origin: relative_path,
1203 is_directory: false,
1204 }
1205 } 1195 }
1206 }; 1196 };
1207 Ok(item) 1197 Ok(item)
@@ -1214,7 +1204,7 @@ pub mod dotup {
1214 } 1204 }
1215 fn origin_color(exists: bool, is_installed: bool) -> Color { 1205 fn origin_color(exists: bool, is_installed: bool) -> Color {
1216 if !exists { 1206 if !exists {
1217 return Color::Red; 1207 Color::Red
1218 } else if is_installed { 1208 } else if is_installed {
1219 Color::Green 1209 Color::Green
1220 } else { 1210 } else {
@@ -1281,14 +1271,16 @@ pub mod dotup {
1281 Ok(relative.to_owned()) 1271 Ok(relative.to_owned())
1282 } 1272 }
1283 1273
1284 // returns the canonical pairs for all links under `path`. 1274 fn link_ids_from_paths_iter(
1285 fn canonical_pairs_under(&self, path: &Path) -> anyhow::Result<Vec<CanonicalPair>> { 1275 &self,
1286 let origin = self.prepare_relative_path(path)?; 1276 paths: impl Iterator<Item = impl AsRef<Path>>,
1287 let mut canonical_pairs = Vec::new(); 1277 ) -> anyhow::Result<Vec<LinkID>> {
1288 for link_id in self.depot.links_under(origin)? { 1278 let mut link_ids = HashSet::<LinkID>::default();
1289 canonical_pairs.push(self.canonical_pair_from_link_id(link_id)); 1279 for path in paths {
1280 let path = self.prepare_relative_path(path.as_ref())?;
1281 link_ids.extend(self.depot.links_under(&path)?);
1290 } 1282 }
1291 Ok(canonical_pairs) 1283 Ok(Vec::from_iter(link_ids.into_iter()))
1292 } 1284 }
1293 1285
1294 fn symlink_is_installed_by_link_id(&self, link_id: LinkID) -> anyhow::Result<bool> { 1286 fn symlink_is_installed_by_link_id(&self, link_id: LinkID) -> anyhow::Result<bool> {
@@ -1382,7 +1374,6 @@ pub mod dotup {
1382 let canonical_origin = self.depot_dir.join(relative_origin); 1374 let canonical_origin = self.depot_dir.join(relative_origin);
1383 let canonical_destination = self.install_base.join(relative_destination); 1375 let canonical_destination = self.install_base.join(relative_destination);
1384 CanonicalPair { 1376 CanonicalPair {
1385 link_id,
1386 origin: canonical_origin, 1377 origin: canonical_origin,
1387 destination: canonical_destination, 1378 destination: canonical_destination,
1388 } 1379 }
@@ -1562,7 +1553,7 @@ mod utils {
1562 ); 1553 );
1563 assert_eq!( 1554 assert_eq!(
1564 PathBuf::from("/home/user/configs/nvim/lua/setup.lua"), 1555 PathBuf::from("/home/user/configs/nvim/lua/setup.lua"),
1565 weakly_canonical_cwd("configs/nvim/lua/setup.lua", cwd.clone()) 1556 weakly_canonical_cwd("configs/nvim/lua/setup.lua", cwd)
1566 ); 1557 );
1567 } 1558 }
1568 } 1559 }
@@ -1622,7 +1613,6 @@ fn main() -> anyhow::Result<()> {
1622 2 => "debug", 1613 2 => "debug",
1623 _ => "trace", 1614 _ => "trace",
1624 }; 1615 };
1625 let log_level = "trace";
1626 1616
1627 Logger::try_with_env_or_str(log_level)? 1617 Logger::try_with_env_or_str(log_level)?
1628 .format(flexi_logger::colored_default_format) 1618 .format(flexi_logger::colored_default_format)
@@ -1678,12 +1668,10 @@ fn command_link(global_flags: Flags, args: LinkArgs) -> anyhow::Result<()> {
1678 let mut dotup = utils::read_dotup(&global_flags)?; 1668 let mut dotup = utils::read_dotup(&global_flags)?;
1679 let origins = if args.directory { 1669 let origins = if args.directory {
1680 vec![args.origin] 1670 vec![args.origin]
1671 } else if args.origin.is_dir() {
1672 utils::collect_files_in_dir_recursive(args.origin)?
1681 } else { 1673 } else {
1682 if args.origin.is_dir() { 1674 vec![args.origin]
1683 utils::collect_files_in_dir_recursive(args.origin)?
1684 } else {
1685 vec![args.origin]
1686 }
1687 }; 1675 };
1688 for origin in origins { 1676 for origin in origins {
1689 dotup.link(origin, &args.destination); 1677 dotup.link(origin, &args.destination);