From 6cf6a995cd2cbe9f180e7bd259effc5871123845 Mon Sep 17 00:00:00 2001 From: Tw93 Date: Wed, 31 Dec 2025 00:17:40 +0800 Subject: [PATCH] Fix: Improve Homebrew uninstallation feedback in 'mo remove' When 'mole remove' is used and Mole was installed via Homebrew, the script now provides more explicit feedback if the 'brew uninstall --force mole' command fails. Previously, errors were silently ignored. This change ensures that if Homebrew uninstallation encounters an issue, the user is informed with the error output and instructed on how to manually complete the uninstallation, preventing inconsistencies where Homebrew still believes Mole is installed. Additionally, a minor improvement to config_dir resolution in update_mole was included for robustness. --- mole | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/mole b/mole index b40e8fc..032ef53 100755 --- a/mole +++ b/mole @@ -432,11 +432,15 @@ update_mole() { # Run installer with visible output (but capture for error handling) local install_output local update_tag="V${latest#V}" - if install_output=$(MOLE_VERSION="$update_tag" "$tmp_installer" --prefix "$install_dir" --config "$HOME/.config/mole" --update 2>&1); then + local config_dir="${MOLE_CONFIG_DIR:-$SCRIPT_DIR}" + if [[ ! -f "$config_dir/lib/core/common.sh" ]]; then + config_dir="$HOME/.config/mole" + fi + if install_output=$(MOLE_VERSION="$update_tag" "$tmp_installer" --prefix "$install_dir" --config "$config_dir" --update 2>&1); then process_install_output "$install_output" else # Retry without --update flag - if install_output=$(MOLE_VERSION="$update_tag" "$tmp_installer" --prefix "$install_dir" --config "$HOME/.config/mole" 2>&1); then + if install_output=$(MOLE_VERSION="$update_tag" "$tmp_installer" --prefix "$install_dir" --config "$config_dir" 2>&1); then process_install_output "$install_output" else if [[ -t 1 ]]; then stop_inline_spinner; fi @@ -461,11 +465,27 @@ remove_mole() { fi local is_homebrew=false + local brew_cmd="" + local brew_has_mole="false" local -a manual_installs=() local -a alias_installs=() + if command -v brew > /dev/null 2>&1; then + brew_cmd="brew" + elif [[ -x "/opt/homebrew/bin/brew" ]]; then + brew_cmd="/opt/homebrew/bin/brew" + elif [[ -x "/usr/local/bin/brew" ]]; then + brew_cmd="/usr/local/bin/brew" + fi + + if [[ -n "$brew_cmd" ]]; then + if "$brew_cmd" list --formula 2> /dev/null | grep -q "^mole$"; then + brew_has_mole="true" + fi + fi + # Check Homebrew - if is_homebrew_install; then + if [[ "$brew_has_mole" == "true" ]] || is_homebrew_install; then is_homebrew=true fi @@ -557,11 +577,25 @@ remove_mole() { ;; esac - # Remove Homebrew installation (silent) + # Remove Homebrew installation local has_error=false if [[ "$is_homebrew" == "true" ]]; then - if ! brew uninstall --force mole > /dev/null 2>&1; then + if [[ -z "$brew_cmd" ]]; then + log_error "Homebrew command not found. Please ensure Homebrew is installed and in your PATH." + log_warning "You may need to manually run: brew uninstall --force mole" + exit 1 + fi + + log_admin "Attempting to uninstall Mole via Homebrew..." + local brew_uninstall_output + if ! brew_uninstall_output=$("$brew_cmd" uninstall --force mole 2>&1); then has_error=true + log_error "Homebrew uninstallation failed:" + printf "%s\n" "$brew_uninstall_output" | sed "s/^/${RED} | ${NC}/" >&2 + log_warning "Please manually run: ${YELLOW}brew uninstall --force mole${NC}" + echo "" # Add a blank line for readability + else + log_success "Mole uninstalled via Homebrew." fi fi # Remove manual installations