Skip to content

feat(update): add wt update subcommand for self-upgrade via Homebrew - #4

Merged
sahil87 merged 3 commits into
mainfrom
260508-4c7g-wt-update-subcommand
May 8, 2026
Merged

sahil87 merged 3 commits into
mainfrom
260508-4c7g-wt-update-subcommand

Conversation

@sahil-noon

Copy link
Copy Markdown
Collaborator

Summary

  • Adds wt update subcommand that self-upgrades the binary via brew upgrade sahil87/tap/wt when installed through Homebrew, with graceful fallbacks for non-Homebrew installs and missing brew.
  • New internal/update package (logic, formula constant, version normalization, brew-install detection, sentinel error for the brew-not-found case) plus paired cmd/wt/update.go cobra wiring; uses os/exec directly per wt's existing convention rather than introducing an internal/proc wrapper.
  • Registers the subcommand in cmd/wt/main.go, documents it in docs/specs/cli-surface.md (## wt update), and adds test coverage for cobra wiring (NoArgs, --help, non-brew branch) and for internal/update (table-driven normalizeVersion, non-brew flow, brew-detector smoke test).

Test plan

  • go test ./... passes locally
  • wt update on a non-brew binary prints the manual-install hint and exits 0 (covered by TestUpdateCobraWiring and TestRunNonBrewInstall)
  • wt update extra rejected per cobra.NoArgs (covered by TestUpdateRejectsArgs)
  • wt --help lists update (covered by TestUpdateAppearsInHelp)
  • Brew-success path (Updated to v{latest}.) was not exercisable in CI — verified by code review against the spec's exact output strings (Requirement: brew upgrade execution, scenario "Successful upgrade")
  • Brew-not-found path (wt update: brew not found on PATH., single-line stderr, exit 1) similarly verified by code review against the sentinel-suppression design (Design Decision 2)

Spec coverage

See fab/changes/260508-4c7g-wt-update-subcommand/spec.md. 13 requirements / 29 acceptance scenarios, all satisfied.

sahil87 added 2 commits May 9, 2026 01:08
Adds `wt update`, a self-upgrade command that runs `brew upgrade
sahil87/tap/wt` when the binary was installed via Homebrew, ports
hop's update logic adapted for wt's lack of an internal/proc wrapper
(uses os/exec directly per wt's existing convention).

User-facing outcomes:
- Upgrade success: prints brew progress + "Updated to v{latest}."
- Already up to date: prints "Already up to date ({currentVersion})."
- Non-Homebrew install: prints manual-update hint pointing to
  `brew install sahil87/tap/wt`, exits 0 without invoking brew
- brew not on PATH: prints "wt update: brew not found on PATH." to
  stderr, exits with ExitGeneralError without a duplicate cobra error
  line (suppressed via update.ErrBrewNotFound sentinel)

Tap formula: sahil87/tap/wt (matches docs/specs/build-and-release.md).
Timeouts: 30s brew update, 30s brew info, 120s brew upgrade.

New: src/cmd/wt/update.go (cobra wiring, NoArgs), src/internal/update/
update.go (logic), paired _test.go files. Modified: src/cmd/wt/main.go
(register updateCmd), docs/specs/cli-surface.md (## wt update entry).

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new wt update subcommand that self-upgrades wt when installed via Homebrew, with safe fallbacks for non-Homebrew installs and missing brew, plus accompanying docs/spec updates and tests.

Changes:

  • Introduces internal/update package to detect Homebrew installs, query latest version via brew info, and run brew update/brew upgrade with timeouts.
  • Wires the new update cobra subcommand into the CLI and adds command-level and package-level tests.
  • Documents wt update behavior and adds a FAB change spec/plan/intake for the feature.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/internal/update/update.go Implements Homebrew-based self-update logic and install detection.
src/internal/update/update_test.go Adds tests for version normalization and non-brew fast path.
src/cmd/wt/update.go Registers wt update cobra command and maps brew-not-found sentinel to exit code.
src/cmd/wt/update_test.go Adds CLI wiring tests for update, --help, and NoArgs.
src/cmd/wt/main.go Registers the new update subcommand on the root command.
docs/specs/cli-surface.md Documents wt update behavior and exit codes.
fab/changes/260508-4c7g-wt-update-subcommand/spec.md Feature spec (noted: contains future date and a brew-not-found handling mismatch).
fab/changes/260508-4c7g-wt-update-subcommand/plan.md Execution plan (noted: mismatch with implemented brew-not-found handling).
fab/changes/260508-4c7g-wt-update-subcommand/intake.md Intake doc (noted: future date and mismatch with implemented brew-not-found exit behavior).
fab/changes/260508-4c7g-wt-update-subcommand/.status.yaml FAB status metadata for the change.
fab/changes/260508-4c7g-wt-update-subcommand/.history.jsonl FAB history log entries for the change.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/internal/update/update.go Outdated
Comment on lines +5 to +6
// uses os/exec directly per wt convention (no internal/proc wrapper); the
// constitution does not mandate centralized subprocess routing.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — trimmed the constitution clause from the package doc comment; the doc now focuses on behavior (os/exec direct, no internal/proc wrapper) without process-level framing. (8af2d4b)

Comment on lines +205 to +208
2. **Brew-not-found returns `nil` from cobra wrapper**:
- *Why*: hop suppresses double-printing via an `errSilent` sentinel; wt's `main.go` has no such sentinel. The cleanest port is for the `update.Run` function to write the user-visible hint to its `errOut` parameter, return a sentinel error (e.g., `update.ErrBrewNotFound`), and have `cmd/wt/update.go`'s `RunE` map that sentinel to `nil`. The user sees one hint line; the binary still exits 1 because we explicitly call `os.Exit(ExitGeneralError)` from the cobra wrapper after printing OR we accept exit 0 if treating the missing-brew case as a "manual update required" no-op.
- *Decision detail*: To preserve the documented exit-code semantic ("brew failure → 1"), the cobra wrapper SHALL detect the sentinel and explicitly call `wt.ExitWithError(wt.ExitGeneralError, ...)` after the hint has been printed by `internal/update`. This bypasses cobra's automatic error printing while preserving the exit code. Implementation detail belongs in `cmd/wt/update.go`'s `RunE`.
- *Rejected*: Returning the sentinel error from `RunE` and accepting one extra cobra-printed error line — noisier, contradicts the user-facing hint that already appears.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — Design Decision 2 now describes the implemented approach (os.Exit(wt.ExitGeneralError) directly from the cobra wrapper) and explicitly rejects wt.ExitWithError because it would emit a second structured stderr line and violate the single-line contract. Assumption #5 updated to match. (8af2d4b)

### Phase 2: Core Implementation

- [x] T002 Implement `src/internal/update/update.go`: package doc comment (wt-flavored, drops hop's Constitution Principle I citation), `brewFormula = "sahil87/tap/wt"` constant, timeout constants (30s/30s/120s), exported sentinel `ErrBrewNotFound`, `Run(currentVersion string, out, errOut io.Writer) error`, `brewLatestVersion()`, `isBrewInstalled()`, and `normalizeVersion(v string) string`. Use `os/exec` directly (no `internal/proc` wrapper). For `brew update --quiet` and `brew info --json=v2`: `cmd.Stderr = os.Stderr; out, _ := cmd.Output()` (capture stdout, pass stderr through). For `brew upgrade`: wire `cmd.Stdin/Stdout/Stderr` to `os.Stdin/Stdout/Stderr` (inherit all three). Map `errors.Is(err, exec.ErrNotFound)` from `cmd.Run()`/`cmd.Output()` to the package-local `ErrBrewNotFound` sentinel.
- [x] T003 Implement `src/cmd/wt/update.go`: `updateCmd()` returning `*cobra.Command` with `Use: "update"`, `Short: "self-update the wt binary via Homebrew"`, `Args: cobra.NoArgs`, and `RunE` that calls `update.Run(version, cmd.OutOrStdout(), cmd.ErrOrStderr())`. On `errors.Is(err, update.ErrBrewNotFound)`, call `wt.ExitWithError(wt.ExitGeneralError, ...)` directly to bypass cobra's error print (process terminates). Otherwise propagate the error verbatim.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — T003 description now matches the implemented os.Exit(wt.ExitGeneralError) approach and explains why wt.ExitWithError is unsuitable here (it would print a second stderr line). (8af2d4b)

Comment on lines +122 to +124
**`errSilent` equivalent**: `wt`'s `main.go` uses a simpler error path (`fmt.Fprintf(os.Stderr, "%s\n", err); os.Exit(wt.ExitGeneralError)`) with no `errSilent` sentinel like `hop` has. To avoid a redundant error line when brew is missing, the cobra wrapper in `update.go` SHOULD print its own hint (already done in `internal/update`) and return `nil` after a brew-not-found, rather than propagating the error. The `internal/update` package writes the user-facing hint to `errOut` before returning the sentinel; the cmd wrapper checks `errors.Is(err, update.ErrBrewNotFound)` and returns `nil` so `main.go` doesn't double-print. This preserves the user experience from `hop` without introducing `errSilent` machinery.

Alternatively, return a non-nil error and accept one extra "brew not found on PATH" line from `main.go` — that's still acceptable but slightly noisier. The intake assumes the cleaner suppress-via-nil approach; the spec stage may revisit.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — corrected the errSilent equivalent prose: the cobra wrapper calls os.Exit(wt.ExitGeneralError) directly (not return-nil) so the binary exits 1 with exactly one stderr line. (8af2d4b)

@sahil87
sahil87 merged commit 2cc77ba into main May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants