-
Notifications
You must be signed in to change notification settings - Fork 7
Add ambient update notice for interactive TTY sessions #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c57f460
Add plugin version checking and auto-update awareness
jeremy 0e825fa
Address review feedback: flat-map format, wording, conditional tip
jeremy b6963be
Move plugin version check to doctor-only
jeremy 3da51bc
Guard empty home, fix comment wording
jeremy 8250f6f
Add ambient update notice for interactive TTY sessions
jeremy 0c95df4
Address review feedback: skip non-interactive, handle clock skew
jeremy 573334f
Suppress update notice after upgrade command
jeremy ce1094f
Also suppress update notice after doctor command
jeremy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "time" | ||
|
|
||
| "github.com/basecamp/basecamp-cli/internal/config" | ||
| "github.com/basecamp/basecamp-cli/internal/version" | ||
| ) | ||
|
|
||
| // checkInterval is how often we query GitHub for the latest version. | ||
| var checkInterval = 24 * time.Hour | ||
|
|
||
| // stdoutIsTerminal reports whether stdout is a terminal. Extracted for testability. | ||
| var stdoutIsTerminal = func() bool { | ||
| fi, err := os.Stdout.Stat() | ||
| if err != nil { | ||
| return false | ||
| } | ||
| return (fi.Mode() & os.ModeCharDevice) != 0 | ||
| } | ||
jeremy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // UpdateCheck holds state for a non-blocking version check. | ||
| type UpdateCheck struct { | ||
| latest string | ||
| done chan struct{} | ||
| } | ||
|
|
||
| // StartUpdateCheck begins a background version check if the cache is stale. | ||
| // Returns nil if the check should be skipped (dev build, opted out, etc.). | ||
| func StartUpdateCheck() *UpdateCheck { | ||
| if version.IsDev() { | ||
| return nil | ||
| } | ||
| if os.Getenv("BASECAMP_NO_UPDATE_CHECK") == "1" { | ||
| return nil | ||
| } | ||
|
|
||
| // Skip for non-interactive sessions — no point fetching if we won't display | ||
| if !stdoutIsTerminal() { | ||
| return nil | ||
| } | ||
|
|
||
| uc := &UpdateCheck{done: make(chan struct{})} | ||
| cached := readUpdateCache() | ||
|
|
||
| if cached != nil { | ||
| age := time.Since(cached.CheckedAt) | ||
| if age >= 0 && age < checkInterval { | ||
| // Cache is fresh — use it directly, no goroutine needed | ||
| uc.latest = cached.LatestVersion | ||
| close(uc.done) | ||
| return uc | ||
| } | ||
| } | ||
|
|
||
| // Cache is stale or missing — fetch in the background | ||
| go func() { | ||
| defer close(uc.done) | ||
| latest, err := versionChecker() | ||
| if err != nil || latest == "" { | ||
jeremy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return | ||
| } | ||
| uc.latest = latest | ||
| writeUpdateCache(latest) | ||
| }() | ||
|
|
||
| return uc | ||
| } | ||
|
|
||
| // Notice returns a formatted update notice, or "" if no update is available | ||
| // or the check hasn't completed. Never blocks. | ||
| func (uc *UpdateCheck) Notice() string { | ||
| if uc == nil { | ||
| return "" | ||
| } | ||
|
|
||
| // Non-blocking check: if the goroutine hasn't finished, skip | ||
| select { | ||
| case <-uc.done: | ||
| default: | ||
| return "" | ||
| } | ||
|
|
||
| if uc.latest == "" || uc.latest == version.Version { | ||
| return "" | ||
jeremy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return fmt.Sprintf( | ||
| "Update available: %s → %s — Run \"basecamp upgrade\" to update", | ||
| version.Version, uc.latest, | ||
| ) | ||
jeremy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // updateCache is the on-disk format for the version check result. | ||
| type updateCache struct { | ||
| LatestVersion string `json:"latest_version"` | ||
| CheckedAt time.Time `json:"checked_at"` | ||
| } | ||
|
|
||
| func updateCachePath() string { | ||
| return filepath.Join(config.GlobalConfigDir(), ".update-check") | ||
| } | ||
|
|
||
| func readUpdateCache() *updateCache { | ||
| data, err := os.ReadFile(updateCachePath()) | ||
| if err != nil { | ||
| return nil | ||
| } | ||
| var c updateCache | ||
| if err := json.Unmarshal(data, &c); err != nil { | ||
| return nil | ||
| } | ||
| if c.LatestVersion == "" || c.CheckedAt.IsZero() { | ||
| return nil | ||
| } | ||
| return &c | ||
| } | ||
|
|
||
| func writeUpdateCache(latestVersion string) { | ||
| c := updateCache{ | ||
| LatestVersion: latestVersion, | ||
| CheckedAt: time.Now().UTC(), | ||
| } | ||
| data, err := json.Marshal(c) | ||
| if err != nil { | ||
| return | ||
| } | ||
| dir := filepath.Dir(updateCachePath()) | ||
| _ = os.MkdirAll(dir, 0o755) //nolint:gosec // G301: config dir | ||
jeremy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _ = os.WriteFile(updateCachePath(), data, 0o644) //nolint:gosec // G306: not a secret | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.