-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathupdate.go
More file actions
205 lines (168 loc) · 4.97 KB
/
update.go
File metadata and controls
205 lines (168 loc) · 4.97 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
)
const (
githubAPIURL = "https://api.github.com/repos/git-plm/gitplm/releases/latest"
githubRelURL = "https://github.com/git-plm/gitplm/releases/latest/download"
)
// GitHubRelease represents the GitHub API release response
type GitHubRelease struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
}
// CheckForUpdate checks if a newer version is available, at most once per day.
// Returns a user-facing message if an update is available, or empty string otherwise.
// Errors are silently ignored.
func CheckForUpdate(currentVersion string) string {
current := strings.TrimPrefix(currentVersion, "v")
if current == "Development" || current == "" {
return ""
}
home, err := os.UserHomeDir()
if err != nil {
return ""
}
tsFile := filepath.Join(home, ".gitplm-last-update-check")
if data, err := os.ReadFile(tsFile); err == nil {
if t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(data))); err == nil {
if time.Since(t) < 24*time.Hour {
return ""
}
}
}
latestVersion, err := getLatestVersion()
if err != nil {
return ""
}
// Write timestamp regardless of version comparison
_ = os.WriteFile(tsFile, []byte(time.Now().Format(time.RFC3339)), 0644)
latest := strings.TrimPrefix(latestVersion, "v")
if latest != current {
return fmt.Sprintf("A new version of gitplm is available (v%s). Run 'gitplm -update' to upgrade.", latest)
}
return ""
}
// Update checks for and downloads the latest version of gitplm
func Update(currentVersion string) error {
fmt.Println("Checking for updates...")
latestVersion, err := getLatestVersion()
if err != nil {
return fmt.Errorf("failed to check for updates: %w", err)
}
current := strings.TrimPrefix(currentVersion, "v")
latest := strings.TrimPrefix(latestVersion, "v")
if current == latest {
fmt.Printf("Already running the latest version (%s)\n", currentVersion)
return nil
}
if current == "Development" {
fmt.Printf("Running development version. Latest release is %s\n", latestVersion)
fmt.Println("Proceeding with update...")
} else {
fmt.Printf("Updating from %s to %s\n", currentVersion, latestVersion)
}
if err := downloadAndInstall(latestVersion); err != nil {
return fmt.Errorf("failed to update: %w", err)
}
fmt.Printf("Successfully updated to version %s\n", latestVersion)
return nil
}
func getLatestVersion() (string, error) {
resp, err := http.Get(githubAPIURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("GitHub API returned status %d", resp.StatusCode)
}
var release GitHubRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", err
}
return release.TagName, nil
}
func downloadAndInstall(version string) error {
binaryName := getBinaryName(version)
downloadURL := fmt.Sprintf("%s/%s", githubRelURL, binaryName)
fmt.Printf("Downloading %s...\n", downloadURL)
resp, err := http.Get(downloadURL)
if err != nil {
return fmt.Errorf("failed to download: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("download failed with status %d", resp.StatusCode)
}
execPath, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to get executable path: %w", err)
}
execPath, err = filepath.EvalSymlinks(execPath)
if err != nil {
return fmt.Errorf("failed to resolve symlinks: %w", err)
}
tmpFile, err := os.CreateTemp(filepath.Dir(execPath), "gitplm-update-*")
if err != nil {
return fmt.Errorf("failed to create temp file: %w", err)
}
tmpPath := tmpFile.Name()
defer os.Remove(tmpPath)
if _, err := io.Copy(tmpFile, resp.Body); err != nil {
tmpFile.Close()
return fmt.Errorf("failed to write binary: %w", err)
}
tmpFile.Close()
if err := os.Chmod(tmpPath, 0755); err != nil {
return fmt.Errorf("failed to make binary executable: %w", err)
}
if runtime.GOOS == "windows" {
oldPath := execPath + ".old"
os.Remove(oldPath)
if err := os.Rename(execPath, oldPath); err != nil {
return fmt.Errorf("failed to backup old binary: %w", err)
}
if err := os.Rename(tmpPath, execPath); err != nil {
os.Rename(oldPath, execPath)
return fmt.Errorf("failed to install new binary: %w", err)
}
os.Remove(oldPath)
} else {
if err := os.Rename(tmpPath, execPath); err != nil {
return fmt.Errorf("failed to install new binary: %w", err)
}
}
fmt.Println("Binary updated successfully")
return nil
}
func getBinaryName(version string) string {
goos := runtime.GOOS
goarch := runtime.GOARCH
osName := goos
if goos == "darwin" {
osName = "macos"
}
archName := goarch
if goarch == "amd64" {
archName = "x86_64"
} else if goarch == "386" {
archName = "i386"
}
armVersion := ""
if goarch == "arm" {
armVersion = "7"
if v := os.Getenv("GOARM"); v != "" {
armVersion = v
}
}
return fmt.Sprintf("gitplm-%s-%s-%s%s", version, osName, archName, armVersion)
}