refactor(homebrew): 重构脚本中的颜色输出和错误处理功能
- 添加了 RED 颜色常量定义和相应的颜色代码调整 - 替换了原有的 echo 命令为新的打印函数(如 print_error、print_warning 等) - 实现了统一的错误消息输出格式,使用 print_error 函数 - 优化了颜色输出的实现方式,使用 printf 替代 echo -e - 在 bootstrap 脚本中也实现了类似的颜色输出和错误处理函数 - 统一了脚本中的消息输出格式和样式
This commit is contained in:
@@ -2,6 +2,26 @@
|
|||||||
# 目标:下载远程升级脚本、执行、销毁临时文件
|
# 目标:下载远程升级脚本、执行、销毁临时文件
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
|
if [[ -t 1 ]]; then
|
||||||
|
RED='\033[31m'
|
||||||
|
YELLOW='\033[33m'
|
||||||
|
CYAN='\033[36m'
|
||||||
|
NC='\033[0m'
|
||||||
|
else
|
||||||
|
RED=''
|
||||||
|
YELLOW=''
|
||||||
|
CYAN=''
|
||||||
|
NC=''
|
||||||
|
fi
|
||||||
|
|
||||||
|
print_info() {
|
||||||
|
printf '%b%s%b\n' "$CYAN" "$*" "$NC"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_error() {
|
||||||
|
printf '%bError:%b %s\n' "$RED" "$NC" "$*" >&2
|
||||||
|
}
|
||||||
|
|
||||||
REMOTE="https://git.orionc.me/orion/script/raw/branch/main/homebrew/brew-upgrade-manager.sh"
|
REMOTE="https://git.orionc.me/orion/script/raw/branch/main/homebrew/brew-upgrade-manager.sh"
|
||||||
TEMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/brew-upgrade-manager.XXXXXX")"
|
TEMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/brew-upgrade-manager.XXXXXX")"
|
||||||
TEMP="$TEMP_DIR/brew-upgrade-manager.sh"
|
TEMP="$TEMP_DIR/brew-upgrade-manager.sh"
|
||||||
@@ -43,7 +63,7 @@ if curl --help all 2>/dev/null | grep -q -- '--retry-all-errors'; then
|
|||||||
curl_retry_all_errors+=(--retry-all-errors)
|
curl_retry_all_errors+=(--retry-all-errors)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "正在下载远程脚本..."
|
print_info "正在下载远程脚本..."
|
||||||
curl -f -sSL \
|
curl -f -sSL \
|
||||||
--connect-timeout "$connect_timeout" \
|
--connect-timeout "$connect_timeout" \
|
||||||
--speed-limit 1024 \
|
--speed-limit 1024 \
|
||||||
@@ -52,16 +72,16 @@ curl -f -sSL \
|
|||||||
--retry-delay "$download_retry_delay" \
|
--retry-delay "$download_retry_delay" \
|
||||||
"${curl_retry_all_errors[@]}" \
|
"${curl_retry_all_errors[@]}" \
|
||||||
"$REMOTE" -o "$TEMP"
|
"$REMOTE" -o "$TEMP"
|
||||||
[[ -s "$TEMP" ]] || { echo "远程脚本为空,已停止执行。" >&2; exit 1; }
|
[[ -s "$TEMP" ]] || { print_error "远程脚本为空,已停止执行。"; exit 1; }
|
||||||
|
|
||||||
if [[ -n "${BREWUP_SHA256:-}" ]]; then
|
if [[ -n "${BREWUP_SHA256:-}" ]]; then
|
||||||
echo "正在校验脚本 SHA256..."
|
print_info "正在校验脚本 SHA256..."
|
||||||
actual_sha256="$(shasum -a 256 "$TEMP")"
|
actual_sha256="$(shasum -a 256 "$TEMP")"
|
||||||
actual_sha256="${actual_sha256%% *}"
|
actual_sha256="${actual_sha256%% *}"
|
||||||
if [[ "$actual_sha256" != "$BREWUP_SHA256" ]]; then
|
if [[ "$actual_sha256" != "$BREWUP_SHA256" ]]; then
|
||||||
echo "脚本 SHA256 不匹配,已停止执行。" >&2
|
print_error "脚本 SHA256 不匹配,已停止执行。"
|
||||||
echo "Expected: $BREWUP_SHA256" >&2
|
printf '%bExpected:%b %s\n' "$YELLOW" "$NC" "$BREWUP_SHA256" >&2
|
||||||
echo "Actual: $actual_sha256" >&2
|
printf '%bActual:%b %s\n' "$RED" "$NC" "$actual_sha256" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@@ -69,7 +89,7 @@ fi
|
|||||||
bash -n "$TEMP"
|
bash -n "$TEMP"
|
||||||
|
|
||||||
if [[ "${BREWUP_DEBUG:-0}" == "1" ]]; then
|
if [[ "${BREWUP_DEBUG:-0}" == "1" ]]; then
|
||||||
echo "Downloaded script first line:"
|
print_info "Downloaded script first line:"
|
||||||
head -n 1 "$TEMP"
|
head -n 1 "$TEMP"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -86,17 +106,18 @@ EOF
|
|||||||
export BREWUP_KEYCHAIN_ACCOUNT="$CURRENT_USER"
|
export BREWUP_KEYCHAIN_ACCOUNT="$CURRENT_USER"
|
||||||
|
|
||||||
if ! /usr/bin/security find-generic-password -a "$CURRENT_USER" -s "$KEYCHAIN_SERVICE" -w >/dev/null 2>&1; then
|
if ! /usr/bin/security find-generic-password -a "$CURRENT_USER" -s "$KEYCHAIN_SERVICE" -w >/dev/null 2>&1; then
|
||||||
printf "首次使用:请输入一次 sudo 密码,将保存到 macOS Keychain:"
|
printf '%b首次使用:请输入一次 sudo 密码,将保存到 macOS Keychain:%b' "$YELLOW" "$NC"
|
||||||
IFS= read -r -s BREWUP_SUDO_PASSWORD
|
IFS= read -r -s BREWUP_SUDO_PASSWORD
|
||||||
printf '\n'
|
printf '\n'
|
||||||
/usr/bin/security add-generic-password -U -a "$CURRENT_USER" -s "$KEYCHAIN_SERVICE" -w "$BREWUP_SUDO_PASSWORD" >/dev/null
|
/usr/bin/security add-generic-password -U -a "$CURRENT_USER" -s "$KEYCHAIN_SERVICE" -w "$BREWUP_SUDO_PASSWORD" >/dev/null
|
||||||
unset BREWUP_SUDO_PASSWORD
|
unset BREWUP_SUDO_PASSWORD
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "正在通过 Keychain 准备 sudo 凭据..."
|
print_info "正在通过 Keychain 准备 sudo 凭据..."
|
||||||
if ! sudo -A -v; then
|
if ! sudo -A -v; then
|
||||||
echo "Keychain 中的 sudo 密码不可用,请删除后重新保存:" >&2
|
print_error "Keychain 中的 sudo 密码不可用,请删除后重新保存:"
|
||||||
echo " security delete-generic-password -a \"$CURRENT_USER\" -s \"$KEYCHAIN_SERVICE\"" >&2
|
printf ' %bsecurity delete-generic-password -a %q -s %q%b\n' \
|
||||||
|
"$CYAN" "$CURRENT_USER" "$KEYCHAIN_SERVICE" "$NC" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,13 +20,15 @@ export PATH
|
|||||||
export HOMEBREW_NO_INSTALL_CLEANUP=1
|
export HOMEBREW_NO_INSTALL_CLEANUP=1
|
||||||
|
|
||||||
if [[ -t 1 ]]; then
|
if [[ -t 1 ]]; then
|
||||||
GREEN='\033[1;32m'
|
RED='\033[31m'
|
||||||
YELLOW='\033[1;33m'
|
GREEN='\033[32m'
|
||||||
BLUE='\033[1;34m'
|
YELLOW='\033[33m'
|
||||||
CYAN='\033[1;36m'
|
BLUE='\033[34m'
|
||||||
|
CYAN='\033[36m'
|
||||||
NC='\033[0m'
|
NC='\033[0m'
|
||||||
export HOMEBREW_COLOR=1
|
export HOMEBREW_COLOR=1
|
||||||
else
|
else
|
||||||
|
RED=''
|
||||||
GREEN=''
|
GREEN=''
|
||||||
YELLOW=''
|
YELLOW=''
|
||||||
BLUE=''
|
BLUE=''
|
||||||
@@ -57,7 +59,7 @@ parse_args() {
|
|||||||
while (($# > 0)); do
|
while (($# > 0)); do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
--width)
|
--width)
|
||||||
[[ $# -ge 2 ]] || { echo "Error: --width requires a value." >&2; exit 2; }
|
[[ $# -ge 2 ]] || { print_error "--width requires a value."; exit 2; }
|
||||||
TERMINAL_WIDTH="$2"
|
TERMINAL_WIDTH="$2"
|
||||||
shift 2
|
shift 2
|
||||||
;;
|
;;
|
||||||
@@ -70,7 +72,7 @@ parse_args() {
|
|||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "Error: Unknown argument: $1" >&2
|
print_error "Unknown argument: $1"
|
||||||
usage >&2
|
usage >&2
|
||||||
exit 2
|
exit 2
|
||||||
;;
|
;;
|
||||||
@@ -88,7 +90,7 @@ resolve_terminal_width() {
|
|||||||
[[ -n "$TERMINAL_WIDTH" ]] || TERMINAL_WIDTH=130
|
[[ -n "$TERMINAL_WIDTH" ]] || TERMINAL_WIDTH=130
|
||||||
|
|
||||||
if [[ ! "$TERMINAL_WIDTH" =~ ^[0-9]+$ ]] || ((TERMINAL_WIDTH < 40 || TERMINAL_WIDTH > 500)); then
|
if [[ ! "$TERMINAL_WIDTH" =~ ^[0-9]+$ ]] || ((TERMINAL_WIDTH < 40 || TERMINAL_WIDTH > 500)); then
|
||||||
echo "Error: terminal width must be an integer between 40 and 500." >&2
|
print_error "terminal width must be an integer between 40 and 500."
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
export COLUMNS="$TERMINAL_WIDTH"
|
export COLUMNS="$TERMINAL_WIDTH"
|
||||||
@@ -96,14 +98,35 @@ resolve_terminal_width() {
|
|||||||
|
|
||||||
separator() {
|
separator() {
|
||||||
local i
|
local i
|
||||||
|
printf '%b' "$BLUE"
|
||||||
for ((i = 0; i < TERMINAL_WIDTH; i++)); do
|
for ((i = 0; i < TERMINAL_WIDTH; i++)); do
|
||||||
printf '='
|
printf '='
|
||||||
done
|
done
|
||||||
printf '\n'
|
printf '%b\n' "$NC"
|
||||||
}
|
}
|
||||||
|
|
||||||
print_header() {
|
print_header() {
|
||||||
echo -e "${BLUE}$1${NC}"
|
printf '%b%s%b\n' "$BLUE" "$1" "$NC"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_error() {
|
||||||
|
printf '%bError:%b %s\n' "$RED" "$NC" "$*" >&2
|
||||||
|
}
|
||||||
|
|
||||||
|
print_warning() {
|
||||||
|
printf '%bWarning:%b %s\n' "$YELLOW" "$NC" "$*"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_notice() {
|
||||||
|
printf '%b%s%b\n' "$YELLOW" "$*" "$NC"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_success() {
|
||||||
|
printf '%b%s%b\n' "$GREEN" "$*" "$NC"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_info() {
|
||||||
|
printf '%b%s%b\n' "$CYAN" "$*" "$NC"
|
||||||
}
|
}
|
||||||
|
|
||||||
is_enabled() {
|
is_enabled() {
|
||||||
@@ -157,17 +180,17 @@ acquire_lock() {
|
|||||||
IFS= read -r existing_pid < "$LOCK_DIR/pid" || true
|
IFS= read -r existing_pid < "$LOCK_DIR/pid" || true
|
||||||
fi
|
fi
|
||||||
if [[ "$existing_pid" =~ ^[0-9]+$ ]] && kill -0 "$existing_pid" 2>/dev/null; then
|
if [[ "$existing_pid" =~ ^[0-9]+$ ]] && kill -0 "$existing_pid" 2>/dev/null; then
|
||||||
echo "Error: another brewup process is already running (PID ${existing_pid})." >&2
|
print_error "another brewup process is already running (PID ${existing_pid})."
|
||||||
exit 75
|
exit 75
|
||||||
fi
|
fi
|
||||||
|
|
||||||
rm -f "$LOCK_DIR/pid" 2>/dev/null || true
|
rm -f "$LOCK_DIR/pid" 2>/dev/null || true
|
||||||
rmdir "$LOCK_DIR" 2>/dev/null || {
|
rmdir "$LOCK_DIR" 2>/dev/null || {
|
||||||
echo "Error: unable to remove stale lock directory: $LOCK_DIR" >&2
|
print_error "unable to remove stale lock directory: $LOCK_DIR"
|
||||||
exit 75
|
exit 75
|
||||||
}
|
}
|
||||||
if ! mkdir "$LOCK_DIR" 2>/dev/null; then
|
if ! mkdir "$LOCK_DIR" 2>/dev/null; then
|
||||||
echo "Error: another brewup process acquired the lock." >&2
|
print_error "another brewup process acquired the lock."
|
||||||
exit 75
|
exit 75
|
||||||
fi
|
fi
|
||||||
LOCK_ACQUIRED=1
|
LOCK_ACQUIRED=1
|
||||||
@@ -216,7 +239,7 @@ run_brew_with_retries() {
|
|||||||
|
|
||||||
for ((attempt = 1; attempt <= attempts; attempt++)); do
|
for ((attempt = 1; attempt <= attempts; attempt++)); do
|
||||||
if ((attempt > 1)); then
|
if ((attempt > 1)); then
|
||||||
echo -e "${YELLOW}Retrying ${label} (${attempt}/${attempts}) after ${delay}s...${NC}"
|
print_info "Retrying ${label} (${attempt}/${attempts}) after ${delay}s..."
|
||||||
sleep "$delay"
|
sleep "$delay"
|
||||||
next_delay=$((delay * 2))
|
next_delay=$((delay * 2))
|
||||||
((next_delay > 300)) && next_delay=300
|
((next_delay > 300)) && next_delay=300
|
||||||
@@ -230,7 +253,7 @@ run_brew_with_retries() {
|
|||||||
pipeline_status=("${PIPESTATUS[@]}")
|
pipeline_status=("${PIPESTATUS[@]}")
|
||||||
status="${pipeline_status[0]}"
|
status="${pipeline_status[0]}"
|
||||||
if ((${pipeline_status[1]:-0} != 0)); then
|
if ((${pipeline_status[1]:-0} != 0)); then
|
||||||
echo "Error: failed to write retry log." >&2
|
print_error "failed to write retry log."
|
||||||
status="${pipeline_status[1]}"
|
status="${pipeline_status[1]}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@@ -251,7 +274,7 @@ run_brew_with_retries() {
|
|||||||
ACTIVE_LOG_FILE=""
|
ACTIVE_LOG_FILE=""
|
||||||
return "$status"
|
return "$status"
|
||||||
fi
|
fi
|
||||||
echo -e "${YELLOW}${label} failed with a retryable error.${NC}"
|
print_notice "${label} failed with a retryable error."
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -272,20 +295,20 @@ run_formula_upgrade() {
|
|||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "${YELLOW}Warning: Formula upgrade returned ${bulk_status}; checking remaining Formulae...${NC}"
|
print_warning "Formula upgrade returned ${bulk_status}; checking remaining Formulae..."
|
||||||
if ! outdated_formulae="$(get_outdated_formulae)"; then
|
if ! outdated_formulae="$(get_outdated_formulae)"; then
|
||||||
echo -e "${YELLOW}Warning: unable to verify outdated Formulae.${NC}"
|
print_warning "unable to verify outdated Formulae."
|
||||||
return "$bulk_status"
|
return "$bulk_status"
|
||||||
fi
|
fi
|
||||||
if [[ -z "$outdated_formulae" ]]; then
|
if [[ -z "$outdated_formulae" ]]; then
|
||||||
echo -e "${YELLOW}No outdated Formulae remain; treating the bulk error as recovered.${NC}"
|
print_notice "No outdated Formulae remain; treating the bulk error as recovered."
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf '%s\n' "$outdated_formulae"
|
printf '%s\n' "$outdated_formulae"
|
||||||
while IFS= read -r formula; do
|
while IFS= read -r formula; do
|
||||||
[[ -n "$formula" ]] || continue
|
[[ -n "$formula" ]] || continue
|
||||||
echo -e "${CYAN}Retrying Formula: ${formula}${NC}"
|
print_info "Retrying Formula: ${formula}"
|
||||||
if run_brew_with_retries "Formula ${formula}" upgrade --formula "$formula"; then
|
if run_brew_with_retries "Formula ${formula}" upgrade --formula "$formula"; then
|
||||||
:
|
:
|
||||||
else
|
else
|
||||||
@@ -294,19 +317,19 @@ run_formula_upgrade() {
|
|||||||
done <<< "$outdated_formulae"
|
done <<< "$outdated_formulae"
|
||||||
|
|
||||||
if ! remaining_formulae="$(get_outdated_formulae)"; then
|
if ! remaining_formulae="$(get_outdated_formulae)"; then
|
||||||
echo -e "${YELLOW}Warning: unable to verify Formulae after individual retries.${NC}"
|
print_warning "unable to verify Formulae after individual retries."
|
||||||
FAILED_FORMULAE=("${failed_formulae[@]}")
|
FAILED_FORMULAE=("${failed_formulae[@]}")
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
if [[ -z "$remaining_formulae" ]]; then
|
if [[ -z "$remaining_formulae" ]]; then
|
||||||
echo -e "${GREEN}All outdated Formulae were upgraded after isolated retries.${NC}"
|
print_success "All outdated Formulae were upgraded after isolated retries."
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
while IFS= read -r formula; do
|
while IFS= read -r formula; do
|
||||||
[[ -n "$formula" ]] && FAILED_FORMULAE+=("$formula")
|
[[ -n "$formula" ]] && FAILED_FORMULAE+=("$formula")
|
||||||
done <<< "$remaining_formulae"
|
done <<< "$remaining_formulae"
|
||||||
echo -e "${YELLOW}Formulae still outdated after retries:${NC}"
|
print_warning "Formulae still outdated after retries:"
|
||||||
printf '%s\n' "$remaining_formulae"
|
printf '%s\n' "$remaining_formulae"
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
@@ -318,7 +341,7 @@ run_cask_upgrade() {
|
|||||||
|
|
||||||
if is_enabled "${HB_CASK_FORCE:-0}"; then
|
if is_enabled "${HB_CASK_FORCE:-0}"; then
|
||||||
bulk_args+=(--force)
|
bulk_args+=(--force)
|
||||||
echo -e "${YELLOW}HB_CASK_FORCE is enabled; existing Cask files may be overwritten.${NC}"
|
print_warning "HB_CASK_FORCE is enabled; existing Cask files may be overwritten."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
brew "${bulk_args[@]}" || bulk_status=$?
|
brew "${bulk_args[@]}" || bulk_status=$?
|
||||||
@@ -326,20 +349,20 @@ run_cask_upgrade() {
|
|||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "${YELLOW}Warning: Cask upgrade returned ${bulk_status}; checking remaining Casks...${NC}"
|
print_warning "Cask upgrade returned ${bulk_status}; checking remaining Casks..."
|
||||||
if ! outdated_casks="$(get_outdated_casks)"; then
|
if ! outdated_casks="$(get_outdated_casks)"; then
|
||||||
echo -e "${YELLOW}Warning: unable to verify outdated Casks.${NC}"
|
print_warning "unable to verify outdated Casks."
|
||||||
return "$bulk_status"
|
return "$bulk_status"
|
||||||
fi
|
fi
|
||||||
if [[ -z "$outdated_casks" ]]; then
|
if [[ -z "$outdated_casks" ]]; then
|
||||||
echo -e "${YELLOW}No outdated Casks remain; treating the bulk error as recovered.${NC}"
|
print_notice "No outdated Casks remain; treating the bulk error as recovered."
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf '%s\n' "$outdated_casks"
|
printf '%s\n' "$outdated_casks"
|
||||||
while IFS= read -r cask; do
|
while IFS= read -r cask; do
|
||||||
[[ -n "$cask" ]] || continue
|
[[ -n "$cask" ]] || continue
|
||||||
echo -e "${CYAN}Retrying Cask: ${cask} (without --force)${NC}"
|
print_info "Retrying Cask: ${cask} (without --force)"
|
||||||
if run_brew_with_retries "Cask ${cask}" upgrade --cask --greedy "$cask"; then
|
if run_brew_with_retries "Cask ${cask}" upgrade --cask --greedy "$cask"; then
|
||||||
:
|
:
|
||||||
else
|
else
|
||||||
@@ -348,19 +371,19 @@ run_cask_upgrade() {
|
|||||||
done <<< "$outdated_casks"
|
done <<< "$outdated_casks"
|
||||||
|
|
||||||
if ! remaining_casks="$(get_outdated_casks)"; then
|
if ! remaining_casks="$(get_outdated_casks)"; then
|
||||||
echo -e "${YELLOW}Warning: unable to verify Casks after individual retries.${NC}"
|
print_warning "unable to verify Casks after individual retries."
|
||||||
FAILED_CASKS=("${failed_casks[@]}")
|
FAILED_CASKS=("${failed_casks[@]}")
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
if [[ -z "$remaining_casks" ]]; then
|
if [[ -z "$remaining_casks" ]]; then
|
||||||
echo -e "${GREEN}All outdated Casks were upgraded after isolated retries.${NC}"
|
print_success "All outdated Casks were upgraded after isolated retries."
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
while IFS= read -r cask; do
|
while IFS= read -r cask; do
|
||||||
[[ -n "$cask" ]] && FAILED_CASKS+=("$cask")
|
[[ -n "$cask" ]] && FAILED_CASKS+=("$cask")
|
||||||
done <<< "$remaining_casks"
|
done <<< "$remaining_casks"
|
||||||
echo -e "${YELLOW}Casks still outdated after retries:${NC}"
|
print_warning "Casks still outdated after retries:"
|
||||||
printf '%s\n' "$remaining_casks"
|
printf '%s\n' "$remaining_casks"
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
@@ -388,7 +411,7 @@ prune_cache_before_today() {
|
|||||||
|
|
||||||
cache_dir="$(brew --cache)"
|
cache_dir="$(brew --cache)"
|
||||||
if [[ ! -d "$cache_dir" || "$cache_dir" == "/" || "$cache_dir" == "$HOME" ]]; then
|
if [[ ! -d "$cache_dir" || "$cache_dir" == "/" || "$cache_dir" == "$HOME" ]]; then
|
||||||
echo "Error: Homebrew returned an unsafe cache path: $cache_dir" >&2
|
print_error "Homebrew returned an unsafe cache path: $cache_dir"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -402,11 +425,11 @@ prune_cache_before_today() {
|
|||||||
|
|
||||||
if ! find "$cache_dir" -mindepth 1 \( -type f -o -type l \) \
|
if ! find "$cache_dir" -mindepth 1 \( -type f -o -type l \) \
|
||||||
! -newer "$ACTIVE_CLEANUP_MARKER" -print0 > "$ACTIVE_CLEANUP_MANIFEST"; then
|
! -newer "$ACTIVE_CLEANUP_MARKER" -print0 > "$ACTIVE_CLEANUP_MANIFEST"; then
|
||||||
echo "Error: failed to enumerate Homebrew cache files to remove." >&2
|
print_error "failed to enumerate Homebrew cache files to remove."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Removed cache files:"
|
printf '%bRemoved cache files:%b\n' "$YELLOW" "$NC"
|
||||||
while IFS= read -r -d '' file; do
|
while IFS= read -r -d '' file; do
|
||||||
if ! file_size="$(stat -f '%z' -- "$file" 2>/dev/null)"; then
|
if ! file_size="$(stat -f '%z' -- "$file" 2>/dev/null)"; then
|
||||||
file_size=0
|
file_size=0
|
||||||
@@ -415,25 +438,26 @@ prune_cache_before_today() {
|
|||||||
if rm -f -- "$file"; then
|
if rm -f -- "$file"; then
|
||||||
removed_count=$((removed_count + 1))
|
removed_count=$((removed_count + 1))
|
||||||
removed_bytes=$((removed_bytes + file_size))
|
removed_bytes=$((removed_bytes + file_size))
|
||||||
printf ' %10s %q\n' "$(format_bytes "$file_size")" "$relative_path"
|
printf ' %b%10s%b %q\n' "$CYAN" "$(format_bytes "$file_size")" "$NC" "$relative_path"
|
||||||
else
|
else
|
||||||
echo "Warning: failed to remove cache file: $file" >&2
|
print_warning "failed to remove cache file: $file" >&2
|
||||||
cleanup_failed=1
|
cleanup_failed=1
|
||||||
fi
|
fi
|
||||||
done < "$ACTIVE_CLEANUP_MANIFEST"
|
done < "$ACTIVE_CLEANUP_MANIFEST"
|
||||||
if ((removed_count == 0)); then
|
if ((removed_count == 0)); then
|
||||||
echo " (none)"
|
printf ' (none)\n'
|
||||||
fi
|
fi
|
||||||
printf 'Removed %d Homebrew cache file(s) dated before %s (%s total).\n' \
|
printf '%bRemoved%b %b%d%b Homebrew cache file(s) dated before %b%s%b (%b%s total%b).\n' \
|
||||||
"$removed_count" "$cutoff_date" "$(format_bytes "$removed_bytes")"
|
"$YELLOW" "$NC" "$CYAN" "$removed_count" "$NC" "$BLUE" "$cutoff_date" "$NC" \
|
||||||
|
"$CYAN" "$(format_bytes "$removed_bytes")" "$NC"
|
||||||
|
|
||||||
if ! find "$cache_dir" -mindepth 1 \( -type f -o -type l \) \
|
if ! find "$cache_dir" -mindepth 1 \( -type f -o -type l \) \
|
||||||
-newer "$ACTIVE_CLEANUP_MARKER" -print0 > "$ACTIVE_CLEANUP_MANIFEST"; then
|
-newer "$ACTIVE_CLEANUP_MARKER" -print0 > "$ACTIVE_CLEANUP_MANIFEST"; then
|
||||||
echo "Error: failed to enumerate today's retained Homebrew cache files." >&2
|
print_error "failed to enumerate today's retained Homebrew cache files."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Retained cache files dated today or later:"
|
printf '%bRetained cache files dated today or later:%b\n' "$GREEN" "$NC"
|
||||||
while IFS= read -r -d '' file; do
|
while IFS= read -r -d '' file; do
|
||||||
if ! file_size="$(stat -f '%z' -- "$file" 2>/dev/null)"; then
|
if ! file_size="$(stat -f '%z' -- "$file" 2>/dev/null)"; then
|
||||||
file_size=0
|
file_size=0
|
||||||
@@ -441,13 +465,14 @@ prune_cache_before_today() {
|
|||||||
relative_path="${file#"$cache_dir"/}"
|
relative_path="${file#"$cache_dir"/}"
|
||||||
kept_count=$((kept_count + 1))
|
kept_count=$((kept_count + 1))
|
||||||
kept_bytes=$((kept_bytes + file_size))
|
kept_bytes=$((kept_bytes + file_size))
|
||||||
printf ' %10s %q\n' "$(format_bytes "$file_size")" "$relative_path"
|
printf ' %b%10s%b %q\n' "$CYAN" "$(format_bytes "$file_size")" "$NC" "$relative_path"
|
||||||
done < "$ACTIVE_CLEANUP_MANIFEST"
|
done < "$ACTIVE_CLEANUP_MANIFEST"
|
||||||
if ((kept_count == 0)); then
|
if ((kept_count == 0)); then
|
||||||
echo " (none)"
|
printf ' (none)\n'
|
||||||
fi
|
fi
|
||||||
printf "Retained %d Homebrew cache file(s) dated %s or later (%s total).\n" \
|
printf '%bRetained%b %b%d%b Homebrew cache file(s) dated %b%s%b or later (%b%s total%b).\n' \
|
||||||
"$kept_count" "$cutoff_date" "$(format_bytes "$kept_bytes")"
|
"$GREEN" "$NC" "$CYAN" "$kept_count" "$NC" "$BLUE" "$cutoff_date" "$NC" \
|
||||||
|
"$CYAN" "$(format_bytes "$kept_bytes")" "$NC"
|
||||||
|
|
||||||
rm -f "$ACTIVE_CLEANUP_MARKER"
|
rm -f "$ACTIVE_CLEANUP_MARKER"
|
||||||
ACTIVE_CLEANUP_MARKER=""
|
ACTIVE_CLEANUP_MARKER=""
|
||||||
@@ -460,7 +485,7 @@ run_cleanup() {
|
|||||||
local cleanup_days="${HB_CLEANUP_DAYS:-}"
|
local cleanup_days="${HB_CLEANUP_DAYS:-}"
|
||||||
|
|
||||||
if is_enabled "${HB_SKIP_CLEANUP:-0}"; then
|
if is_enabled "${HB_SKIP_CLEANUP:-0}"; then
|
||||||
echo -e "${YELLOW}Cleanup skipped because HB_SKIP_CLEANUP is enabled.${NC}"
|
print_notice "Cleanup skipped because HB_SKIP_CLEANUP is enabled."
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
if is_enabled "${HB_CLEANUP_ALL:-0}" || [[ "$cleanup_days" == "all" ]]; then
|
if is_enabled "${HB_CLEANUP_ALL:-0}" || [[ "$cleanup_days" == "all" ]]; then
|
||||||
@@ -469,7 +494,7 @@ run_cleanup() {
|
|||||||
fi
|
fi
|
||||||
if [[ -n "$cleanup_days" ]]; then
|
if [[ -n "$cleanup_days" ]]; then
|
||||||
if [[ ! "$cleanup_days" =~ ^[0-9]+$ ]] || ((cleanup_days > 3650)); then
|
if [[ ! "$cleanup_days" =~ ^[0-9]+$ ]] || ((cleanup_days > 3650)); then
|
||||||
echo "Error: HB_CLEANUP_DAYS must be an integer between 0 and 3650." >&2
|
print_error "HB_CLEANUP_DAYS must be an integer between 0 and 3650."
|
||||||
return 2
|
return 2
|
||||||
fi
|
fi
|
||||||
brew cleanup --prune="$cleanup_days"
|
brew cleanup --prune="$cleanup_days"
|
||||||
@@ -488,14 +513,14 @@ print_summary() {
|
|||||||
|
|
||||||
separator
|
separator
|
||||||
if ((overall_status == 0)); then
|
if ((overall_status == 0)); then
|
||||||
echo -e "${GREEN}All operations completed successfully.${NC}"
|
print_success "All operations completed successfully."
|
||||||
else
|
else
|
||||||
echo -e "${YELLOW}Homebrew upgrade completed with unresolved failures.${NC}"
|
printf '%bHomebrew upgrade completed with unresolved failures.%b\n' "$RED" "$NC"
|
||||||
if ((${#FAILED_FORMULAE[@]} > 0)); then
|
if ((${#FAILED_FORMULAE[@]} > 0)); then
|
||||||
printf 'Failed Formulae: %s\n' "${FAILED_FORMULAE[*]}"
|
printf '%bFailed Formulae:%b %s\n' "$RED" "$NC" "${FAILED_FORMULAE[*]}"
|
||||||
fi
|
fi
|
||||||
if ((${#FAILED_CASKS[@]} > 0)); then
|
if ((${#FAILED_CASKS[@]} > 0)); then
|
||||||
printf 'Failed Casks: %s\n' "${FAILED_CASKS[*]}"
|
printf '%bFailed Casks:%b %s\n' "$RED" "$NC" "${FAILED_CASKS[*]}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
separator
|
separator
|
||||||
@@ -504,8 +529,8 @@ print_summary() {
|
|||||||
parse_args "$@"
|
parse_args "$@"
|
||||||
resolve_terminal_width
|
resolve_terminal_width
|
||||||
|
|
||||||
command -v brew >/dev/null 2>&1 || { echo "Error: Homebrew is not installed or not in PATH." >&2; exit 127; }
|
command -v brew >/dev/null 2>&1 || { print_error "Homebrew is not installed or not in PATH."; exit 127; }
|
||||||
command -v tee >/dev/null 2>&1 || { echo "Error: tee is required." >&2; exit 127; }
|
command -v tee >/dev/null 2>&1 || { print_error "tee is required."; exit 127; }
|
||||||
acquire_lock
|
acquire_lock
|
||||||
|
|
||||||
separator
|
separator
|
||||||
@@ -517,11 +542,11 @@ printf '\n'
|
|||||||
separator
|
separator
|
||||||
print_header "Step 2: Performing health check (brew doctor)"
|
print_header "Step 2: Performing health check (brew doctor)"
|
||||||
if is_enabled "${HB_SKIP_DOCTOR:-0}"; then
|
if is_enabled "${HB_SKIP_DOCTOR:-0}"; then
|
||||||
echo -e "${YELLOW}brew doctor skipped because HB_SKIP_DOCTOR is enabled.${NC}"
|
print_notice "brew doctor skipped because HB_SKIP_DOCTOR is enabled."
|
||||||
elif ! brew doctor; then
|
elif ! brew doctor; then
|
||||||
echo -e "${YELLOW}Warning: 'brew doctor' detected issues. Manual review is recommended.${NC}"
|
print_warning "'brew doctor' detected issues. Manual review is recommended."
|
||||||
else
|
else
|
||||||
echo "Homebrew environment is in good health."
|
print_success "Homebrew environment is in good health."
|
||||||
fi
|
fi
|
||||||
separator
|
separator
|
||||||
printf '\n'
|
printf '\n'
|
||||||
@@ -530,12 +555,14 @@ separator
|
|||||||
print_header "Step 3: Upgrading Formulae and Casks with failure isolation"
|
print_header "Step 3: Upgrading Formulae and Casks with failure isolation"
|
||||||
overall_status=0
|
overall_status=0
|
||||||
|
|
||||||
echo -e "\n${CYAN}>>> [1/2] Upgrading CLI Formulae...${NC}"
|
printf '\n'
|
||||||
|
print_info ">>> [1/2] Upgrading CLI Formulae..."
|
||||||
if ! run_formula_upgrade; then
|
if ! run_formula_upgrade; then
|
||||||
overall_status=1
|
overall_status=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "\n${CYAN}>>> [2/2] Upgrading GUI Casks...${NC}"
|
printf '\n'
|
||||||
|
print_info ">>> [2/2] Upgrading GUI Casks..."
|
||||||
if ! run_cask_upgrade; then
|
if ! run_cask_upgrade; then
|
||||||
overall_status=1
|
overall_status=1
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user