From de57a20828aca1428e04397cae105133e24ac1ae Mon Sep 17 00:00:00 2001 From: tw93 Date: Sun, 1 Mar 2026 21:04:41 +0800 Subject: [PATCH] refactor(dry-run): unify flag handling in completion and remove --- bin/completion.sh | 11 +++++------ mole | 50 +++++++++++++++++++++++------------------------ 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/bin/completion.sh b/bin/completion.sh index 1feec15..a575929 100755 --- a/bin/completion.sh +++ b/bin/completion.sh @@ -32,13 +32,12 @@ emit_fish_completions() { printf 'complete -c %s -n "not __fish_mole_no_subcommand" -a fish -d "generate fish completion" -n "__fish_see_subcommand_path completion"\n' "$cmd" } -DRY_RUN_MODE=false if [[ $# -gt 0 ]]; then normalized_args=() for arg in "$@"; do case "$arg" in "--dry-run" | "-n") - DRY_RUN_MODE=true + export MOLE_DRY_RUN=1 ;; *) normalized_args+=("$arg") @@ -54,7 +53,7 @@ fi # Auto-install mode when run without arguments if [[ $# -eq 0 ]]; then - if [[ "$DRY_RUN_MODE" == "true" ]]; then + if [[ "${MOLE_DRY_RUN:-0}" == "1" ]]; then echo -e "${YELLOW}${ICON_DRY_RUN} DRY RUN MODE${NC}, shell config files will not be modified" echo "" fi @@ -98,7 +97,7 @@ if [[ $# -eq 0 ]]; then if [[ -z "$completion_name" ]]; then if [[ -f "$config_file" ]] && grep -Eq "(^# Mole shell completion$|(mole|mo)[[:space:]]+completion)" "$config_file" 2> /dev/null; then - if [[ "$DRY_RUN_MODE" == "true" ]]; then + if [[ "${MOLE_DRY_RUN:-0}" == "1" ]]; then echo -e "${GRAY}${ICON_REVIEW} [DRY RUN] Would remove stale completion entries from $config_file${NC}" echo "" else @@ -120,7 +119,7 @@ if [[ $# -eq 0 ]]; then # Check if already installed and normalize to latest line if [[ -f "$config_file" ]] && grep -Eq "(mole|mo)[[:space:]]+completion" "$config_file" 2> /dev/null; then - if [[ "$DRY_RUN_MODE" == "true" ]]; then + if [[ "${MOLE_DRY_RUN:-0}" == "1" ]]; then echo -e "${GRAY}${ICON_REVIEW} [DRY RUN] Would normalize completion entry in $config_file${NC}" echo "" exit 0 @@ -150,7 +149,7 @@ if [[ $# -eq 0 ]]; then echo -e "${GRAY}Will add to ${config_file}:${NC}" echo " $completion_line" echo "" - if [[ "$DRY_RUN_MODE" == "true" ]]; then + if [[ "${MOLE_DRY_RUN:-0}" == "1" ]]; then echo -e "${GREEN}${ICON_SUCCESS}${NC} Dry run complete, no changes made" exit 0 fi diff --git a/mole b/mole index e503965..ae61f6c 100755 --- a/mole +++ b/mole @@ -553,6 +553,31 @@ remove_mole() { exit 0 fi + # Dry-run mode: show preview and exit without confirmation + if [[ "$dry_run_mode" == "true" ]]; then + echo -e "${YELLOW}${ICON_DRY_RUN} DRY RUN MODE${NC}, no files will be removed" + echo "" + echo -e "${YELLOW}Remove Mole${NC}, would delete the following:" + if [[ "$is_homebrew" == "true" ]]; then + echo -e " ${GRAY}${ICON_LIST} Would run: brew uninstall --force mole${NC}" + fi + if [[ ${manual_count:-0} -gt 0 ]]; then + for install in "${manual_installs[@]}"; do + [[ -f "$install" ]] && echo -e " ${GRAY}${ICON_LIST} Would remove: ${install}${NC}" + done + fi + if [[ ${alias_count:-0} -gt 0 ]]; then + for alias in "${alias_installs[@]}"; do + [[ -f "$alias" ]] && echo -e " ${GRAY}${ICON_LIST} Would remove: ${alias}${NC}" + done + fi + [[ -d "$HOME/.cache/mole" ]] && echo -e " ${GRAY}${ICON_LIST} Would remove: $HOME/.cache/mole${NC}" + [[ -d "$HOME/.config/mole" ]] && echo -e " ${GRAY}${ICON_LIST} Would remove: $HOME/.config/mole${NC}" + + printf '\n%s\n\n' "${GREEN}${ICON_SUCCESS}${NC} Dry run complete, no changes made" + exit 0 + fi + echo -e "${YELLOW}Remove Mole${NC}, will delete the following:" if [[ "$is_homebrew" == "true" ]]; then echo " ${ICON_LIST} Mole via Homebrew" @@ -579,31 +604,6 @@ remove_mole() { esac local has_error=false - if [[ "$dry_run_mode" == "true" ]]; then - echo "" - echo -e "${YELLOW}${ICON_DRY_RUN} DRY RUN MODE${NC}, no files will be removed" - - if [[ "$is_homebrew" == "true" ]]; then - echo -e " ${GRAY}${ICON_LIST} Would run: brew uninstall --force mole${NC}" - fi - - if [[ ${manual_count:-0} -gt 0 ]]; then - for install in "${manual_installs[@]}"; do - [[ -f "$install" ]] && echo -e " ${GRAY}${ICON_LIST} Would remove: ${install}${NC}" - done - fi - if [[ ${alias_count:-0} -gt 0 ]]; then - for alias in "${alias_installs[@]}"; do - [[ -f "$alias" ]] && echo -e " ${GRAY}${ICON_LIST} Would remove: ${alias}${NC}" - done - fi - [[ -d "$HOME/.cache/mole" ]] && echo -e " ${GRAY}${ICON_LIST} Would remove: $HOME/.cache/mole${NC}" - [[ -d "$HOME/.config/mole" ]] && echo -e " ${GRAY}${ICON_LIST} Would remove: $HOME/.config/mole${NC}" - - printf '\n%s\n\n' "${GREEN}${ICON_SUCCESS}${NC} Dry run complete, no changes made" - exit 0 - fi - if [[ "$is_homebrew" == "true" ]]; then if [[ -z "$brew_cmd" ]]; then log_error "Homebrew command not found. Please ensure Homebrew is installed and in your PATH."