This repository was archived by the owner on Aug 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
443 lines (366 loc) · 11.3 KB
/
Copy pathmain.go
File metadata and controls
443 lines (366 loc) · 11.3 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/spf13/cobra"
)
// Version information
var (
Version = "1.0.0"
Commit = "development" // This can be set via build flags
Date = "unknown" // This can be set via build flags
)
type GitHubRelease struct {
TagName string `json:"tag_name"`
}
type GitHubTag struct {
Name string `json:"name"`
}
type PKGBUILDInfo struct {
Name string
Version string
URL string
Source []string
}
// Variable substitution map
type Variables map[string]string
func main() {
var rootCmd = &cobra.Command{
Use: "aurvt [package-directory]",
Short: "Check for newer versions of AUR packages on GitHub",
Long: `A CLI tool to check if newer versions are available for AUR packages hosted on GitHub.`,
Version: Version,
Args: cobra.ExactArgs(1),
Run: runVersionCheck,
}
// Add version command
rootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Print version information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("aurvt version %s\n", Version)
fmt.Printf("Commit: %s\n", Commit)
fmt.Printf("Build Date: %s\n", Date)
},
})
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func runVersionCheck(cmd *cobra.Command, args []string) {
packageDir := args[0]
pkgbuildPath := fmt.Sprintf("%s/PKGBUILD", packageDir)
// Check if package is in .aurvtignore file
packageName := filepath.Base(packageDir)
if isPackageIgnored(packageName) {
fmt.Printf("📋 Package '%s' is ignored (found in .aurvtignore)\n", packageName)
return
}
// Parse PKGBUILD
pkgInfo, err := parsePKGBUILD(pkgbuildPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing PKGBUILD: %v\n", err)
os.Exit(1)
}
fmt.Printf("Package: %s\n", pkgInfo.Name)
fmt.Printf("Current version: %s\n", pkgInfo.Version)
fmt.Printf("Repository URL: %s\n", pkgInfo.URL)
if len(pkgInfo.Source) > 0 {
fmt.Printf("Source URLs:\n")
for i, source := range pkgInfo.Source {
fmt.Printf(" [%d] %s\n", i+1, source)
}
}
// Check if it's a GitHub repository
if !strings.Contains(pkgInfo.URL, "github.com") {
fmt.Println("❌ Not a GitHub repository, version checking not supported")
return
}
// Get latest version from GitHub
latestVersion, err := getLatestGitHubVersion(pkgInfo.URL)
if err != nil {
fmt.Fprintf(os.Stderr, "Error fetching latest version: %v\n", err)
os.Exit(1)
}
// Check if source URLs use archive format and suggest tags endpoint
checkAndSuggestURLFormat(pkgInfo.Source, pkgInfo.URL)
// https://github.com/pyhunspell/pyhunspell/tags
// https://github.com/pyhunspell/pyhunspell/archive/refs/tags/0.5.5.tar.gz
if latestVersion == "" {
fmt.Println("❌ Could not fetch latest version")
return
}
fmt.Printf("Latest version: %s\n", latestVersion)
// Compare versions
if latestVersion == pkgInfo.Version {
fmt.Println("✅ Package is up to date")
} else {
fmt.Printf("🔄 New version available: %s → %s\n", pkgInfo.Version, latestVersion)
}
}
func parsePKGBUILD(filepath string) (*PKGBUILDInfo, error) {
content, err := os.ReadFile(filepath)
if err != nil {
return nil, fmt.Errorf("failed to read PKGBUILD: %w", err)
}
text := string(content)
// Parse all variables first
variables := parseVariables(text)
// Extract pkgname with variable substitution
pkgname := extractValue(text, `pkgname\s*=\s*(.+)`)
if pkgname == "" {
return nil, fmt.Errorf("could not find pkgname in PKGBUILD")
}
pkgname = substituteVariables(cleanValue(pkgname), variables)
// Extract pkgver
pkgver := extractValue(text, `pkgver\s*=\s*(.+)`)
if pkgver == "" {
return nil, fmt.Errorf("could not find pkgver in PKGBUILD")
}
pkgver = cleanValue(pkgver)
// Extract url with variable substitution
url := extractValue(text, `url\s*=\s*(.+)`)
if url == "" {
return nil, fmt.Errorf("could not find url in PKGBUILD")
}
url = substituteVariables(cleanValue(url), variables)
// Extract source array with variable substitution
source := extractSourceArray(text, variables)
return &PKGBUILDInfo{
Name: pkgname,
Version: pkgver,
URL: url,
Source: source,
}, nil
}
func parseVariables(text string) Variables {
variables := make(Variables)
// Match variable assignments: variable=value
re := regexp.MustCompile(`^(\w+)\s*=\s*(.+)$`)
lines := strings.Split(text, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
matches := re.FindStringSubmatch(line)
if len(matches) >= 3 {
varName := matches[1]
varValue := cleanValue(matches[2])
variables[varName] = varValue
}
}
// Now substitute variables within variables (handle nested substitutions)
for varName, varValue := range variables {
substitutedValue := substituteVariables(varValue, variables)
variables[varName] = substitutedValue
}
return variables
}
func substituteVariables(value string, variables Variables) string {
// Replace variables in the format $_variable or ${variable}
result := value
// Replace $_variable format
for varName, varValue := range variables {
pattern := fmt.Sprintf(`\$%s\b`, varName)
re := regexp.MustCompile(pattern)
result = re.ReplaceAllString(result, varValue)
}
// Replace ${variable} format
for varName, varValue := range variables {
pattern := fmt.Sprintf(`\$\{%s\}`, varName)
re := regexp.MustCompile(pattern)
result = re.ReplaceAllString(result, varValue)
}
return result
}
func extractSourceArray(text string, variables Variables) []string {
// Find source array definition
re := regexp.MustCompile(`source\s*=\s*\(([^)]+)\)`)
matches := re.FindStringSubmatch(text)
if len(matches) < 2 {
return nil
}
sourceContent := matches[1]
// Split by newlines and clean up
lines := strings.Split(sourceContent, "\n")
var sources []string
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Remove quotes and clean up
line = strings.Trim(line, `"'`)
line = strings.TrimSpace(line)
if line != "" {
// Substitute variables
line = substituteVariables(line, variables)
sources = append(sources, line)
}
}
return sources
}
func extractValue(text, pattern string) string {
re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(text)
if len(matches) > 1 {
return matches[1]
}
return ""
}
func cleanValue(value string) string {
// Remove quotes and trim whitespace
value = strings.Trim(value, `"'`)
return strings.TrimSpace(value)
}
func getLatestGitHubVersion(repoURL string) (string, error) {
// Extract owner/repo from GitHub URL
re := regexp.MustCompile(`github\.com/([^/]+)/([^/]+)`)
matches := re.FindStringSubmatch(repoURL)
if len(matches) < 3 {
return "", fmt.Errorf("invalid GitHub URL format")
}
owner := matches[1]
repo := matches[2]
// Try releases first, then tags if releases fails
version, err := getLatestFromReleases(owner, repo)
if err != nil {
// Fallback to tags
version, err = getLatestFromTags(owner, repo)
if err != nil {
return "", fmt.Errorf("failed to fetch version from both releases and tags: %w", err)
}
}
return version, nil
}
func getLatestFromReleases(owner, repo string) (string, error) {
apiURL := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", owner, repo)
resp, err := http.Get(apiURL)
if err != nil {
return "", fmt.Errorf("failed to fetch from GitHub releases API: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("GitHub releases API returned status %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
var release GitHubRelease
if err := json.Unmarshal(body, &release); err != nil {
return "", fmt.Errorf("failed to parse JSON response: %w", err)
}
// Remove 'v' prefix if present
version := strings.TrimPrefix(release.TagName, "v")
return version, nil
}
func getLatestFromTags(owner, repo string) (string, error) {
apiURL := fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", owner, repo)
resp, err := http.Get(apiURL)
if err != nil {
return "", fmt.Errorf("failed to fetch from GitHub tags API: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("GitHub tags API returned status %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
var tags []GitHubTag
if err := json.Unmarshal(body, &tags); err != nil {
return "", fmt.Errorf("failed to parse JSON response: %w", err)
}
if len(tags) == 0 {
return "", fmt.Errorf("no tags found")
}
// Get the first (latest) tag
version := strings.TrimPrefix(tags[0].Name, "v")
return version, nil
}
func checkAndSuggestURLFormat(sourceUrls []string, currentUrl string) {
for _, sourceUrl := range sourceUrls {
// Check if this is an archive URL (contains archive/refs/tags/)
if strings.Contains(sourceUrl, "archive/refs/tags/") {
// Extract the base URL from the current URL
baseURL := strings.TrimSuffix(currentUrl, "/")
fmt.Printf("⚠️ Source URL uses archive format: %s\n", sourceUrl)
fmt.Printf(" Consider using tags endpoint: %s/tags\n", baseURL)
fmt.Printf(" Example: %s/tags\n", baseURL)
fmt.Println("")
// Check tags page for newer versions
checkTagsPageForNewerVersion(baseURL, sourceUrl)
}
}
}
func isPackageIgnored(packageName string) bool {
ignoreFile := ".aurvtignore"
// Check if .aurvtignore exists
if _, err := os.Stat(ignoreFile); os.IsNotExist(err) {
return false
}
// Read .aurvtignore file
file, err := os.Open(ignoreFile)
if err != nil {
return false
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// Skip empty lines and comments
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Check if package name matches
if line == packageName {
return true
}
}
return false
}
func checkTagsPageForNewerVersion(baseURL, sourceUrl string) {
// Extract current version from source URL
// Format: filename-version.tar.gz::url/archive/refs/tags/version.tar.gz
re := regexp.MustCompile(`archive/refs/tags/([^/]+)\.tar\.gz`)
matches := re.FindStringSubmatch(sourceUrl)
if len(matches) < 2 {
fmt.Println(" Could not extract current version from source URL")
return
}
currentVersion := matches[1]
// Extract owner/repo from base URL
urlRe := regexp.MustCompile(`github\.com/([^/]+)/([^/]+)`)
urlMatches := urlRe.FindStringSubmatch(baseURL)
if len(urlMatches) < 3 {
fmt.Println(" Could not extract owner/repo from URL")
return
}
owner := urlMatches[1]
repo := urlMatches[2]
// Get latest version from tags API
latestVersion, err := getLatestFromTags(owner, repo)
if err != nil {
// Instead of showing an error, just return silently
// This handles cases where the tags API might not be available or accessible
return
}
// Compare versions
if latestVersion != currentVersion {
fmt.Printf(" 🔄 New version available on tags page: %s → %s\n", currentVersion, latestVersion)
fmt.Printf(" Tags page: %s/tags\n", baseURL)
} else {
fmt.Printf(" ✅ Current version %s is up to date on tags page\n", currentVersion)
}
}