From 4ac75c4c93c75ee86fe4506c1ff5902bba3119ab Mon Sep 17 00:00:00 2001 From: Marco Vermeulen Date: Sat, 13 May 2023 17:28:56 +0100 Subject: [PATCH] Improvements on uninstall command error handling. --- src/bin/uninstall/main.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/bin/uninstall/main.rs b/src/bin/uninstall/main.rs index 8ee5a96..05a9c5b 100644 --- a/src/bin/uninstall/main.rs +++ b/src/bin/uninstall/main.rs @@ -37,22 +37,28 @@ fn main() { let version_path = sdkman_dir.join(&candidate_path).join(&version); let current_link_path = candidate_path.join(CURRENT_DIR); if current_link_path.is_dir() { - let canonical_link_path = - fs::read_link(current_link_path.to_owned()).expect("panic! can't read link"); - if version_path == canonical_link_path && force { - remove_symlink_dir(current_link_path).expect("panic! can't remove current symlink"); - } else if version_path == canonical_link_path && !force { - eprint!( - "\n{} {} is the {} version and should not be removed.", - candidate, - version, - "current".bold(), - ); - println!( - "\n\nOverride with {}, but leaves the candidate unusable!", - "--force".italic() - ); - process::exit(1); + match fs::read_link(current_link_path.to_owned()) { + Ok(relative_resolved_dir) => { + let resolved_link_path = candidate_path.join(relative_resolved_dir); + if (version_path == resolved_link_path) && force { + remove_symlink_dir(current_link_path).expect("can't remove current symlink"); + } else if (version_path == resolved_link_path) && !force { + eprintln!( + "\n{} {} is the {} version and should not be removed.", + candidate.bold(), + version.bold(), + "current".italic(), + ); + println!( + "\n\nOverride with {}, but leaves the candidate unusable!", + "--force".italic() + ); + process::exit(1); + } + } + Err(e) => { + eprintln!("current link broken, stepping over: {}", e.to_string()); + } } }