Files
System-Sync-Restore/lib/brew.sh
T
Oliver Surke 8489180d51 Fix ssr restore: skip iCloud prompt when available, bail early on missing sudo
lib/brew.sh:
  - Before running the Homebrew installer, check `sudo -n true`. If sudo
    is not available, print a clear actionable error message with the
    manual install command and exit instead of running NONINTERACTIVE=1
    and crashing with "Need sudo access ... Administrator!" mid-way.

bin/ssr-restore:
  - Drop the unconditional "Configure iCloud / cloud drive?" prompt.
    Instead check whether the cloud directory is already mounted; if it
    is, proceed silently (the common case on an already-configured Mac).
    The prompt only appears when the directory is missing, i.e. iCloud
    is not yet signed in or the drive is not mounted — with an
    appropriate message explaining what to do.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-11 20:51:18 +02:00

71 lines
2.5 KiB
Bash

#!/usr/bin/env bash
# lib/brew.sh — Homebrew install + Brewfile dump/restore.
ssr::brew::ensure_installed() {
if command -v brew >/dev/null 2>&1; then
return 0
fi
# Homebrew's install script requires sudo on macOS to write to /opt/homebrew
# (Apple Silicon) or /usr/local (Intel). Check before running it so we can
# give a clear error instead of a cryptic failure.
if ! sudo -n true 2>/dev/null; then
ssr::warn "Homebrew is not installed and sudo access is required to install it."
ssr::warn "Please run the following as an admin user and re-run ssr restore:"
ssr::warn " /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
ssr::die "Cannot install Homebrew without sudo access."
fi
ssr::log "Homebrew not found — installing"
# NONINTERACTIVE=1 skips the confirmation prompt; sudo is available (checked above).
NONINTERACTIVE=1 /bin/bash -c \
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Ensure brew is on PATH for both Apple Silicon and Intel layouts.
if [[ -x /opt/homebrew/bin/brew ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [[ -x /usr/local/bin/brew ]]; then
eval "$(/usr/local/bin/brew shellenv)"
fi
command -v brew >/dev/null 2>&1 || ssr::die "brew still not on PATH after install"
}
ssr::brew::update() {
ssr::log "brew update"
brew update --quiet
}
ssr::brew::upgrade() {
[[ "${SSR_BREW_UPGRADE:-true}" == "true" ]] || return 0
ssr::log "brew upgrade"
brew upgrade --quiet || ssr::warn "brew upgrade reported errors (continuing)"
}
ssr::brew::cleanup() {
[[ "${SSR_BREW_CLEANUP:-true}" == "true" ]] || return 0
ssr::log "brew cleanup"
brew cleanup --quiet || true
}
# Dumps Brewfile to the cloud backup dir with a timestamped rotation copy.
ssr::brew::dump() {
local backup_dir="$1" brewfile="$1/Brewfile"
ssr::ensure_dir "$backup_dir"
if [[ -f "$brewfile" ]]; then
cp -f "$brewfile" "$brewfile.bak"
fi
ssr::log "brew bundle dump → $brewfile"
(
cd "$backup_dir"
brew bundle dump --describe --quiet --force --file=Brewfile
)
ssr::ok "Brewfile written ($(wc -l < "$brewfile" | tr -d ' ') lines)"
}
ssr::brew::restore() {
local brewfile="$1"
[[ -f "$brewfile" ]] || ssr::die "Brewfile not found: $brewfile"
ssr::log "brew bundle install ← $brewfile"
brew bundle install --file="$brewfile" --no-lock --verbose
}