-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathversion.go
More file actions
153 lines (133 loc) · 3.94 KB
/
Copy pathversion.go
File metadata and controls
153 lines (133 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package version
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"slices"
"strings"
"time"
"github.com/goccy/go-json"
"golang.org/x/mod/semver"
)
const (
repoOwner = "bootdotdev"
repoName = "bootdev"
)
type VersionInfo struct {
CurrentVersion string
LatestVersion string
IsOutdated bool
IsUpdateRequired bool
FailedToFetch error
}
func FetchUpdateInfo(currentVersion string) VersionInfo {
latest, err := getLatestVersion()
if err != nil {
return VersionInfo{
FailedToFetch: err,
}
}
isUpdateRequired := isUpdateRequired(currentVersion, latest)
isOutdated := isOutdated(currentVersion, latest)
return VersionInfo{
IsUpdateRequired: isUpdateRequired,
IsOutdated: isOutdated,
CurrentVersion: currentVersion,
LatestVersion: latest,
}
}
func (v *VersionInfo) PromptUpdateIfAvailable() {
if v.IsOutdated {
fmt.Fprintln(os.Stderr, "A new version of the bootdev CLI is available!")
fmt.Fprintln(os.Stderr, "Please run the following command to update:")
fmt.Fprintln(os.Stderr, " bootdev upgrade")
fmt.Fprintln(os.Stderr, "or")
fmt.Fprintf(os.Stderr, " go install github.com/bootdotdev/bootdev@%s\n\n", v.LatestVersion)
}
}
// Returns true if the current version is older than the latest.
func isOutdated(current string, latest string) bool {
return semver.Compare(current, latest) < 0
}
// Returns true if the latest version has a higher major or minor
// number than the current version. If you don't want to force
// an update, you can increment the patch number instead.
func isUpdateRequired(current string, latest string) bool {
latestMajorMinor := semver.MajorMinor(latest)
currentMajorMinor := semver.MajorMinor(current)
return semver.Compare(currentMajorMinor, latestMajorMinor) < 0
}
func getLatestVersion() (string, error) {
goproxyDefault := "https://proxy.golang.org"
goproxy := goproxyDefault
cmd := exec.Command("go", "env", "GOPROXY")
output, err := cmd.Output()
if err == nil {
goproxy = strings.TrimSpace(string(output))
}
proxies := strings.Split(goproxy, ",")
if !slices.Contains(proxies, goproxyDefault) {
proxies = append(proxies, goproxyDefault)
}
client := &http.Client{Timeout: 10 * time.Second}
var lastErr error
for _, proxy := range proxies {
proxy = strings.TrimSpace(proxy)
proxy = strings.TrimRight(proxy, "/")
if proxy == "direct" || proxy == "off" {
continue
}
url := fmt.Sprintf("%s/github.com/%s/%s/@latest", proxy, repoOwner, repoName)
version, err := fetchLatestWithRetry(client, url)
if err == nil {
return version, nil
}
lastErr = err
}
if lastErr != nil {
return "", fmt.Errorf("failed to fetch latest version: %w", lastErr)
}
return "", fmt.Errorf("failed to fetch latest version")
}
// fetchLatestWithRetry queries a single proxy, retrying a few times because
// proxy.golang.org occasionally resets the connection or returns a transient
// 5xx. A single drop should not fail the whole version check.
func fetchLatestWithRetry(client *http.Client, url string) (string, error) {
const maxAttempts = 3
var lastErr error
for attempt := 1; attempt <= maxAttempts; attempt++ {
version, err := fetchLatestFromProxy(client, url)
if err == nil {
return version, nil
}
lastErr = err
if attempt < maxAttempts {
time.Sleep(time.Duration(attempt) * 200 * time.Millisecond)
}
}
return "", lastErr
}
func fetchLatestFromProxy(client *http.Client, url string) (string, error) {
resp, err := client.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected status %s from %s", resp.Status, url)
}
var version struct{ Version string }
if err := json.Unmarshal(body, &version); err != nil {
return "", fmt.Errorf("invalid response from %s: %w", url, err)
}
if version.Version == "" {
return "", fmt.Errorf("empty version in response from %s", url)
}
return version.Version, nil
}