fix(upgrade): gracefully handle rate limit, use cached version

- Skip CLI upgrade silently when API rate limited and no cache
- Use cached version to upgrade when API fails but cache exists
- Extract BuildDownloadURL for direct URL construction without API
- Add GetCachedVersion to expose cached latest version
This commit is contained in:
Willie
2026-02-01 08:33:54 +08:00
parent e922f31f5a
commit d084e59c8d
3 changed files with 39 additions and 18 deletions
+16 -5
View File
@@ -104,12 +104,23 @@ func upgradeCLIBinary(dryRun, force bool) error {
// Get latest version from GitHub
treeSpinner := ui.StartTreeSpinner("Checking latest version...", false)
release, err := versionpkg.FetchLatestRelease()
var latestVersion string
if err != nil {
treeSpinner.Fail("Failed to check")
return fmt.Errorf("failed to check latest version: %w", err)
// API failed - try to use cached version
cachedVersion := versionpkg.GetCachedVersion()
if cachedVersion != "" && cachedVersion != version {
latestVersion = cachedVersion
treeSpinner.Success(fmt.Sprintf("Latest: v%s (cached)", latestVersion))
} else {
// No useful cache - skip silently
treeSpinner.Success("Skipped (rate limited)")
return nil
}
} else {
latestVersion = release.Version
treeSpinner.Success(fmt.Sprintf("Latest: v%s", latestVersion))
}
latestVersion := release.Version
treeSpinner.Success(fmt.Sprintf("Latest: v%s", latestVersion))
if version == latestVersion && !force {
ui.StepEnd("Status", "Already up to date ✓")
@@ -135,7 +146,7 @@ func upgradeCLIBinary(dryRun, force bool) error {
}
// Get download URL for current platform
downloadURL, err := release.GetDownloadURL()
downloadURL, err := versionpkg.BuildDownloadURL(latestVersion)
if err != nil {
return fmt.Errorf("failed to get download URL: %w", err)
}
+14 -5
View File
@@ -13,9 +13,9 @@ import (
var Version = "dev"
const (
githubRepo = "runkids/skillshare"
checkInterval = 24 * time.Hour
cacheFileName = "version-check.json"
githubRepo = "runkids/skillshare"
checkInterval = 24 * time.Hour
cacheFileName = "version-check.json"
)
// Cache holds version check cache data
@@ -26,8 +26,8 @@ type Cache struct {
// CheckResult holds the result of a version check
type CheckResult struct {
CurrentVersion string
LatestVersion string
CurrentVersion string
LatestVersion string
UpdateAvailable bool
}
@@ -80,6 +80,15 @@ func ClearCache() {
os.Remove(cachePath)
}
// GetCachedVersion returns the cached latest version if available
func GetCachedVersion() string {
cache, err := loadCache()
if err != nil || cache == nil {
return ""
}
return cache.LatestVersion
}
// loadCache loads the version check cache from disk
func loadCache() (*Cache, error) {
cachePath, err := getCachePath()
+9 -8
View File
@@ -44,6 +44,11 @@ type Asset struct {
// GetDownloadURL returns the download URL for the current platform
func (r *Release) GetDownloadURL() (string, error) {
return BuildDownloadURL(r.Version)
}
// BuildDownloadURL constructs download URL from version string
func BuildDownloadURL(version string) (string, error) {
osName := runtime.GOOS
archName := runtime.GOARCH
@@ -52,15 +57,11 @@ func (r *Release) GetDownloadURL() (string, error) {
if osName == "windows" {
ext = "zip"
}
expectedName := fmt.Sprintf("skillshare_%s_%s_%s.%s", r.Version, osName, archName, ext)
for _, asset := range r.Assets {
if asset.Name == expectedName {
return asset.BrowserDownloadURL, nil
}
}
return "", fmt.Errorf("no release found for %s/%s", osName, archName)
// Construct URL directly (avoids needing asset list from API)
filename := fmt.Sprintf("skillshare_%s_%s_%s.%s", version, osName, archName, ext)
url := fmt.Sprintf("https://github.com/%s/releases/download/v%s/%s", githubRepo, version, filename)
return url, nil
}
// newGitHubClient creates an HTTP client with optional auth