update.go

  1package update
  2
  3import (
  4	"context"
  5	"encoding/json"
  6	"fmt"
  7	"io"
  8	"net/http"
  9	"regexp"
 10	"strings"
 11	"time"
 12)
 13
 14const (
 15	githubApiUrl = "https://api.github.com/repos/charmbracelet/crush/releases/latest"
 16	userAgent    = "crush/1.0"
 17)
 18
 19// Default is the default [Client].
 20var Default Client = &github{}
 21
 22// Info contains information about an available update.
 23type Info struct {
 24	Current string
 25	Latest  string
 26	URL     string
 27}
 28
 29// Matches a version string like:
 30// v0.0.0-0.20251231235959-06c807842604
 31var goInstallRegexp = regexp.MustCompile(`^v?\d+\.\d+\.\d+-\d+\.\d{14}-[0-9a-f]{12}$`)
 32
 33func (i Info) IsDevelopment() bool {
 34	return i.Current == "devel" || i.Current == "unknown" || strings.Contains(i.Current, "dirty") || goInstallRegexp.MatchString(i.Current)
 35}
 36
 37// Available returns true if there's an update available.
 38//
 39// Returns true if the current and latest versions are different, treating
 40// pre-releases as valid latest versions.
 41func (i Info) Available() bool {
 42	return i.Current != i.Latest
 43}
 44
 45// Check checks if a new version is available.
 46func Check(ctx context.Context, current string, client Client) (Info, error) {
 47	info := Info{
 48		Current: current,
 49		Latest:  current,
 50	}
 51
 52	release, err := client.Latest(ctx)
 53	if err != nil {
 54		return info, fmt.Errorf("failed to fetch latest release: %w", err)
 55	}
 56
 57	info.Latest = strings.TrimPrefix(release.TagName, "v")
 58	info.Current = strings.TrimPrefix(info.Current, "v")
 59	info.URL = release.HTMLURL
 60	return info, nil
 61}
 62
 63// Release represents a GitHub release.
 64type Release struct {
 65	TagName string `json:"tag_name"`
 66	HTMLURL string `json:"html_url"`
 67}
 68
 69// Client is a client that can get the latest release.
 70type Client interface {
 71	Latest(ctx context.Context) (*Release, error)
 72}
 73
 74type github struct{}
 75
 76// Latest implements [Client].
 77func (c *github) Latest(ctx context.Context) (*Release, error) {
 78	client := &http.Client{
 79		Timeout: 30 * time.Second,
 80	}
 81
 82	req, err := http.NewRequestWithContext(ctx, "GET", githubApiUrl, nil)
 83	if err != nil {
 84		return nil, err
 85	}
 86	req.Header.Set("User-Agent", userAgent)
 87	req.Header.Set("Accept", "application/vnd.github.v3+json")
 88
 89	resp, err := client.Do(req)
 90	if err != nil {
 91		return nil, err
 92	}
 93	defer resp.Body.Close()
 94
 95	if resp.StatusCode != http.StatusOK {
 96		body, _ := io.ReadAll(resp.Body)
 97		return nil, fmt.Errorf("github api returned status %d: %s", resp.StatusCode, string(body))
 98	}
 99
100	var release Release
101	if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
102		return nil, err
103	}
104
105	return &release, nil
106}