1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 15:00:07 +00:00

refactor(dry-run): unify flag handling in completion and remove

This commit is contained in:
tw93
2026-03-01 21:04:41 +08:00
parent 241e6a7a34
commit de57a20828
2 changed files with 30 additions and 31 deletions

View File

@@ -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

50
mole
View File

@@ -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."