mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
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:
+11
-50
@@ -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
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package version
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -126,28 +124,9 @@ func saveCache(cache *Cache) error {
|
||||
}
|
||||
|
||||
// fetchLatestVersion fetches the latest version from GitHub
|
||||
// Uses the shared FetchLatestVersionOnly which supports GITHUB_TOKEN
|
||||
func fetchLatestVersion() (string, error) {
|
||||
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", githubRepo)
|
||||
|
||||
client := &http.Client{Timeout: 5 * time.Second}
|
||||
resp, err := client.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 struct {
|
||||
TagName string `json:"tag_name"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return strings.TrimPrefix(release.TagName, "v"), nil
|
||||
return FetchLatestVersionOnly()
|
||||
}
|
||||
|
||||
// compareVersions returns true if v1 < v2 (proper semver comparison)
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RateLimitError indicates GitHub API rate limit was exceeded
|
||||
type RateLimitError struct {
|
||||
Limit string
|
||||
Remaining string
|
||||
Reset string
|
||||
}
|
||||
|
||||
func (e *RateLimitError) Error() string {
|
||||
msg := "GitHub API rate limit exceeded"
|
||||
if e.Remaining == "0" {
|
||||
msg += fmt.Sprintf(" (0/%s remaining)", e.Limit)
|
||||
}
|
||||
msg += "\n\nTo fix this, either:\n"
|
||||
msg += " 1. Wait for rate limit to reset\n"
|
||||
msg += " 2. Set GITHUB_TOKEN environment variable for higher limits (5000/hr)\n"
|
||||
msg += " export GITHUB_TOKEN=ghp_your_token_here"
|
||||
return msg
|
||||
}
|
||||
|
||||
// Release holds GitHub release information
|
||||
type Release struct {
|
||||
TagName string `json:"tag_name"`
|
||||
Version string // Parsed from TagName (without 'v' prefix)
|
||||
Assets []Asset `json:"assets"`
|
||||
}
|
||||
|
||||
// Asset holds GitHub release asset information
|
||||
type Asset struct {
|
||||
Name string `json:"name"`
|
||||
BrowserDownloadURL string `json:"browser_download_url"`
|
||||
}
|
||||
|
||||
// GetDownloadURL returns the download URL for the current platform
|
||||
func (r *Release) GetDownloadURL() (string, error) {
|
||||
osName := runtime.GOOS
|
||||
archName := runtime.GOARCH
|
||||
expectedName := fmt.Sprintf("skillshare_%s_%s_%s.tar.gz", r.Version, osName, archName)
|
||||
|
||||
for _, asset := range r.Assets {
|
||||
if asset.Name == expectedName {
|
||||
return asset.BrowserDownloadURL, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("no release found for %s/%s", osName, archName)
|
||||
}
|
||||
|
||||
// newGitHubClient creates an HTTP client with optional auth
|
||||
func newGitHubClient() *http.Client {
|
||||
return &http.Client{Timeout: 10 * time.Second}
|
||||
}
|
||||
|
||||
// newGitHubRequest creates a request with auth header if token is available
|
||||
func newGitHubRequest(url string) (*http.Request, error) {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/vnd.github.v3+json")
|
||||
|
||||
// Check for GitHub token in environment
|
||||
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
|
||||
req.Header.Set("Authorization", "token "+token)
|
||||
} else if token := os.Getenv("GH_TOKEN"); token != "" {
|
||||
// gh CLI uses GH_TOKEN
|
||||
req.Header.Set("Authorization", "token "+token)
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// checkRateLimit checks response for rate limit errors
|
||||
func checkRateLimit(resp *http.Response) error {
|
||||
if resp.StatusCode == 403 || resp.StatusCode == 429 {
|
||||
return &RateLimitError{
|
||||
Limit: resp.Header.Get("X-RateLimit-Limit"),
|
||||
Remaining: resp.Header.Get("X-RateLimit-Remaining"),
|
||||
Reset: resp.Header.Get("X-RateLimit-Reset"),
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FetchLatestRelease fetches the latest release from GitHub with full asset info
|
||||
func FetchLatestRelease() (*Release, error) {
|
||||
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", githubRepo)
|
||||
|
||||
req, err := newGitHubRequest(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client := newGitHubClient()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("network error: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Check for rate limiting
|
||||
if err := checkRateLimit(resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("GitHub API returned %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var release Release
|
||||
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse response: %w", err)
|
||||
}
|
||||
|
||||
release.Version = strings.TrimPrefix(release.TagName, "v")
|
||||
return &release, nil
|
||||
}
|
||||
|
||||
// FetchLatestVersionOnly fetches just the version string (for background checks)
|
||||
func FetchLatestVersionOnly() (string, error) {
|
||||
release, err := FetchLatestRelease()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return release.Version, nil
|
||||
}
|
||||
Reference in New Issue
Block a user