-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
102 lines (82 loc) · 2.59 KB
/
update.go
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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path"
"time"
"golang.org/x/mod/semver"
)
const checkForUpdatesInterval = 24 * time.Hour
type UpdateInfo struct {
CurrentVersion string
LatestVersion string
}
func IsUpdateAvailable(ctx context.Context, currentVersion string) (*UpdateInfo, error) {
latestVersion, err := getLatestVersion(ctx)
if err != nil {
return nil, fmt.Errorf("could not get latest version: %w", err)
}
if semver.Compare(latestVersion, currentVersion) > 0 {
return &UpdateInfo{
CurrentVersion: currentVersion,
LatestVersion: latestVersion,
}, nil
}
return nil, nil
}
func getLatestVersion(ctx context.Context) (string, error) {
cacheDir, err := os.UserCacheDir()
if err != nil {
return "", fmt.Errorf("could not get cache dir: %w", err)
}
latestVersionPath := path.Join(cacheDir, "kubectl-request-version.txt")
fi, err := os.Stat(latestVersionPath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return "", fmt.Errorf("coult not access version file: %w", err)
}
if fi != nil && fi.ModTime().After(time.Now().Add(-checkForUpdatesInterval)) {
latestVersion, err := os.ReadFile(latestVersionPath)
if err != nil {
return "", fmt.Errorf("could not read version info: %w", err)
}
return string(latestVersion), nil
}
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.github.com/repos/spreadshirt/kube-request-access/releases/latest", nil)
if err != nil {
return "", fmt.Errorf("could not create request: %w", err)
}
req.Header.Set("User-Agent", "kubectl-request / https://github.com/spreadshirt/kube-request-access")
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", fmt.Errorf("could not send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
data, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("expected status code %d but got %d: %q", http.StatusOK, resp.StatusCode, data)
}
var releaseInfo struct {
TagName string `json:"tag_name"`
}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&releaseInfo)
if err != nil {
return "", fmt.Errorf("could not parse release info: %w", err)
}
if releaseInfo.TagName == "" {
return "", fmt.Errorf("could not determine release version, maybe the GitHub API changed?")
}
err = os.WriteFile(latestVersionPath, []byte(releaseInfo.TagName), 0644)
if err != nil {
return "", fmt.Errorf("could not cache release info: %w", err)
}
return releaseInfo.TagName, nil
}