diff --git a/.devcontainer/bin/help b/.devcontainer/bin/help index d71104cb7..7896e5099 100755 --- a/.devcontainer/bin/help +++ b/.devcontainer/bin/help @@ -31,6 +31,9 @@ ${BLUE}Environment Management (Isolated HOME)${NC} ${BOLD}ssback${NC} Exit an isolated shell or go back to workspace. ${BOLD}ssenv${NC} Advanced environment management. +${BLUE}Testing${NC} + ${BOLD}test-brew-upgrade${NC} Simulate Homebrew upgrade path (mock brew). + ${BLUE}Authentication & Security${NC} ${BOLD}credential-helper status${NC} Check GitHub/GitLab token state. ${BOLD}credential-helper off${NC} Temporarily disable all auth for testing. diff --git a/.devcontainer/bin/test-brew-upgrade b/.devcontainer/bin/test-brew-upgrade new file mode 100755 index 000000000..5a6eefa7d --- /dev/null +++ b/.devcontainer/bin/test-brew-upgrade @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Simulate Homebrew upgrade path for visual verification. +# Places the binary in a fake Cellar so isHomebrewInstall() triggers, +# and provides a mock brew script to exercise the tree output. + +WORKSPACE_ROOT="${SKILLSHARE_DEV_WORKSPACE_ROOT:-/workspace}" +GO_BIN="${SKILLSHARE_DEV_GO_BIN:-/usr/local/go/bin/go}" +[ -x "$GO_BIN" ] || GO_BIN="$(command -v go || true)" + +CELLAR="/opt/homebrew/Cellar/skillshare/dev/bin" +FAKE_BIN="/tmp/fake-brew-bin" +BINARY="$CELLAR/skillshare" + +cleanup() { + rm -rf "$FAKE_BIN" /opt/homebrew/Cellar/skillshare + rmdir /opt/homebrew/Cellar 2>/dev/null || true + rmdir /opt/homebrew 2>/dev/null || true +} +trap cleanup EXIT + +# Build and place in fake Cellar +mkdir -p "$CELLAR" "$FAKE_BIN" +(cd "$WORKSPACE_ROOT" && "$GO_BIN" build -o "$BINARY" ./cmd/skillshare) + +# --- Case 1: both succeed --- +cat > "$FAKE_BIN/brew" << 'EOF' +#!/bin/bash +sleep 0.2 +EOF +chmod +x "$FAKE_BIN/brew" + +echo "=== Case 1: success ===" +PATH="$FAKE_BIN:$PATH" "$BINARY" upgrade --cli 2>&1 || true + +# --- Case 2: tap fails, upgrade succeeds --- +cat > "$FAKE_BIN/brew" << 'EOF' +#!/bin/bash +[[ "$1" == "update" ]] && exit 1 +sleep 0.2 +EOF +chmod +x "$FAKE_BIN/brew" + +echo "" +echo "=== Case 2: tap fail, upgrade ok ===" +PATH="$FAKE_BIN:$PATH" "$BINARY" upgrade --cli 2>&1 || true + +# --- Case 3: both fail --- +cat > "$FAKE_BIN/brew" << 'EOF' +#!/bin/bash +echo "Error: something went wrong" >&2 +exit 1 +EOF +chmod +x "$FAKE_BIN/brew" + +echo "" +echo "=== Case 3: both fail ===" +PATH="$FAKE_BIN:$PATH" "$BINARY" upgrade --cli 2>&1 || true diff --git a/cmd/skillshare/upgrade.go b/cmd/skillshare/upgrade.go index a78e7377d..d4b706fad 100644 --- a/cmd/skillshare/upgrade.go +++ b/cmd/skillshare/upgrade.go @@ -146,13 +146,13 @@ func upgradeCLIBinary(dryRun, force bool) error { // Confirm if not forced if !force { - fmt.Println() - fmt.Printf(" Upgrade to v%s? [Y/n]: ", latestVersion) + fmt.Printf("%s\n", ui.TreeLine()) + fmt.Printf("%s Upgrade to v%s? [Y/n]: ", ui.TreeLine(), latestVersion) var input string fmt.Scanln(&input) input = strings.ToLower(strings.TrimSpace(input)) if input == "n" || input == "no" { - ui.Info("Cancelled") + ui.StepEnd("Status", "Cancelled") return nil } } @@ -164,8 +164,8 @@ func upgradeCLIBinary(dryRun, force bool) error { } // Download - fmt.Println() - downloadSpinner := ui.StartSpinner(fmt.Sprintf("Downloading v%s...", latestVersion)) + hasUIDownload := latestVersion != "" + downloadSpinner := ui.StartTreeSpinner(fmt.Sprintf("Downloading v%s...", latestVersion), !hasUIDownload) if err := downloadAndReplace(downloadURL, execPath); err != nil { downloadSpinner.Fail("Failed to download") return fmt.Errorf("failed to upgrade: %w", err) @@ -176,8 +176,8 @@ func upgradeCLIBinary(dryRun, force bool) error { versionpkg.ClearCache() // Pre-download UI assets for the new version (best-effort) - if latestVersion != "" { - uiSpinner := ui.StartSpinner("Downloading UI assets...") + if hasUIDownload { + uiSpinner := ui.StartTreeSpinner("Downloading UI assets...", true) if err := uidist.Download(latestVersion); err != nil { uiSpinner.Warn("UI download skipped (run 'skillshare ui' to retry)") } else { @@ -426,26 +426,36 @@ func isHomebrewInstall(execPath string) bool { } func runBrewUpgrade() error { - // First update the tap to get latest formula - ui.Info("Updating tap...") + // Phase 1: brew update (tap refresh) + tapSpinner := ui.StartTreeSpinner("Updating tap...", false) updateCmd := exec.Command("brew", "update", "--quiet") - updateCmd.Stdout = os.Stdout - updateCmd.Stderr = os.Stderr + var updateBuf bytes.Buffer + updateCmd.Stdout = &updateBuf + updateCmd.Stderr = &updateBuf if err := updateCmd.Run(); err != nil { - ui.Warning("brew update failed, trying upgrade anyway...") + tapSpinner.Fail("Tap update failed (continuing)") + } else { + tapSpinner.Success("Tap updated") } - // Then upgrade - ui.Info("Upgrading...") + // Phase 2: brew upgrade + upgradeSpinner := ui.StartTreeSpinner("Upgrading via Homebrew...", true) cmd := exec.Command("brew", "upgrade", "skillshare") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err := cmd.Run() - if err == nil { - // Clear version cache so next check fetches fresh data - versionpkg.ClearCache() + var buf bytes.Buffer + cmd.Stdout = &buf + cmd.Stderr = &buf + if err := cmd.Run(); err != nil { + upgradeSpinner.Fail("Upgrade failed") + // Show captured output for debugging + if out := strings.TrimSpace(buf.String()); out != "" { + fmt.Println() + fmt.Println(out) + } + return err } - return err + upgradeSpinner.Success("Upgraded") + versionpkg.ClearCache() + return nil } func printUpgradeHelp() { diff --git a/internal/ui/pterm.go b/internal/ui/pterm.go index 1b9ca1c87..a268fa5bc 100644 --- a/internal/ui/pterm.go +++ b/internal/ui/pterm.go @@ -534,6 +534,14 @@ const ( StepCorner = "└" ) +// TreeLine returns the tree continuation character (│) with TTY coloring +func TreeLine() string { + if IsTTY() { + return pterm.Gray(StepLine) + } + return StepLine +} + // StepStart prints the first step (with arrow) func StepStart(label, value string) { if IsTTY() { @@ -665,6 +673,26 @@ func (ts *TreeSpinner) Fail(message string) { } } +// Warn completes the tree spinner with a warning +func (ts *TreeSpinner) Warn(message string) { + elapsed := time.Since(ts.start) + + prefix := StepBranch + "─" + if ts.isLast { + prefix = StepCorner + "─" + } + + if ts.spinner != nil { + ts.spinner.Stop() + } + + if IsTTY() { + fmt.Printf("%s %s %s\n", pterm.Gray(prefix), pterm.Yellow(message), pterm.Gray(fmt.Sprintf("(%.1fs)", elapsed.Seconds()))) + } else { + fmt.Printf("%s %s (%.1fs)\n", prefix, message, elapsed.Seconds()) + } +} + // Update updates the tree spinner text while running. func (ts *TreeSpinner) Update(message string) { message, ok := normalizeSpinnerUpdate(message, ts.lastMessage, ts.lastUpdate)