fix(upgrade): add GitHub token support and improve rate limit handling

Refactored GitHub API logic into shared internal/version/github.go:
- Support GITHUB_TOKEN and GH_TOKEN environment variables
- Clear error message when rate limit exceeded with fix instructions
- Removed duplicate API code from upgrade.go and check.go
This commit is contained in:
Willie Hung
2026-01-31 03:10:55 +08:00
parent 4419a63401
commit f5798fc0ea
3 changed files with 151 additions and 73 deletions
+11 -50
View File
@@ -3,24 +3,20 @@ package main
import (
"archive/tar"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"skillshare/internal/config"
"skillshare/internal/install"
"skillshare/internal/ui"
versioncheck "skillshare/internal/version"
versionpkg "skillshare/internal/version"
)
const githubRepo = "runkids/skillshare"
func cmdUpgrade(args []string) error {
dryRun := false
force := false
@@ -104,11 +100,12 @@ func upgradeCLIBinary(dryRun, force bool) error {
// Get latest version from GitHub
treeSpinner := ui.StartTreeSpinner("Checking latest version...", false)
latestVersion, downloadURL, err := getLatestRelease()
release, err := versionpkg.FetchLatestRelease()
if err != nil {
treeSpinner.Fail("Failed to check")
return fmt.Errorf("failed to check latest version: %w", err)
}
latestVersion := release.Version
treeSpinner.Success(fmt.Sprintf("Latest: v%s", latestVersion))
if version == latestVersion && !force {
@@ -133,6 +130,12 @@ func upgradeCLIBinary(dryRun, force bool) error {
}
}
// Get download URL for current platform
downloadURL, err := release.GetDownloadURL()
if err != nil {
return fmt.Errorf("failed to get download URL: %w", err)
}
// Download
fmt.Println()
downloadSpinner := ui.StartSpinner(fmt.Sprintf("Downloading v%s...", latestVersion))
@@ -143,7 +146,7 @@ func upgradeCLIBinary(dryRun, force bool) error {
downloadSpinner.Success(fmt.Sprintf("Upgraded to v%s", latestVersion))
// Clear version cache so next check fetches fresh data
versioncheck.ClearCache()
versionpkg.ClearCache()
return nil
}
@@ -221,48 +224,6 @@ func upgradeSkillshareSkill(dryRun, force bool) error {
return nil
}
type githubRelease struct {
TagName string `json:"tag_name"`
Assets []struct {
Name string `json:"name"`
BrowserDownloadURL string `json:"browser_download_url"`
} `json:"assets"`
}
func getLatestRelease() (version string, downloadURL string, err error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", githubRepo)
resp, err := http.Get(url)
if err != nil {
return "", "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", "", fmt.Errorf("GitHub API returned %d", resp.StatusCode)
}
var release githubRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", "", err
}
version = strings.TrimPrefix(release.TagName, "v")
// Find matching asset
osName := runtime.GOOS
archName := runtime.GOARCH
expectedName := fmt.Sprintf("skillshare_%s_%s_%s.tar.gz", version, osName, archName)
for _, asset := range release.Assets {
if asset.Name == expectedName {
return version, asset.BrowserDownloadURL, nil
}
}
return "", "", fmt.Errorf("no release found for %s/%s", osName, archName)
}
func downloadAndReplace(url, destPath string) error {
// Download tarball
resp, err := http.Get(url)
@@ -359,7 +320,7 @@ func runBrewUpgrade() error {
err := cmd.Run()
if err == nil {
// Clear version cache so next check fetches fresh data
versioncheck.ClearCache()
versionpkg.ClearCache()
}
return err
}