This repository was archived by the owner on Feb 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathversion.go
More file actions
162 lines (134 loc) · 3.51 KB
/
Copy pathversion.go
File metadata and controls
162 lines (134 loc) · 3.51 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
package version
import (
"context"
"strconv"
"strings"
"time"
"github.com/keywaysh/cli/internal/config"
)
const (
// CacheDuration is how long to cache version check results
CacheDuration = 24 * time.Hour
// CheckTimeout is the maximum time to wait for version check
CheckTimeout = 2 * time.Second
)
// UpdateInfo contains information about an available update
type UpdateInfo struct {
Available bool
CurrentVersion string
LatestVersion string
InstallMethod InstallMethod
UpdateCommand string
}
// CheckForUpdate checks if a newer version is available
// Returns nil if no update is available, check is disabled, or on any error
func CheckForUpdate(ctx context.Context, currentVersion string) *UpdateInfo {
if IsUpdateCheckDisabled() {
return nil
}
// Skip update check for self-hosted instances
if config.IsCustomAPIURL() {
return nil
}
// Skip check for dev builds
if currentVersion == "dev" || currentVersion == "" {
return nil
}
// Detect install method once
method := DetectInstallMethod()
// Skip check for npx (always fetches latest)
if method == InstallMethodNPX {
return nil
}
// Check cache first
cached, err := LoadCache()
if err == nil && cached != nil && time.Since(cached.LastCheck) < CacheDuration {
return buildUpdateInfo(currentVersion, cached.LatestVersion, cached.InstallMethod)
}
// Fetch from GitHub
latest, err := FetchLatestVersion(ctx)
if err != nil {
return nil // Silent failure
}
// Save to cache (ignore errors)
_ = SaveCache(&CacheData{
LastCheck: time.Now(),
LatestVersion: latest,
InstallMethod: method,
})
return buildUpdateInfo(currentVersion, latest, method)
}
func buildUpdateInfo(current, latest string, method InstallMethod) *UpdateInfo {
if !IsNewerVersion(latest, current) {
return nil
}
return &UpdateInfo{
Available: true,
CurrentVersion: current,
LatestVersion: latest,
InstallMethod: method,
UpdateCommand: GetUpdateCommand(method),
}
}
// GetUpdateCommand returns the update command for the given install method.
// Returns an empty string for self-hosted instances where standard update
// commands do not apply.
func GetUpdateCommand(method InstallMethod) string {
if config.IsCustomAPIURL() {
return ""
}
switch method {
case InstallMethodNPM:
return "npm update -g @keywaysh/cli"
case InstallMethodHomebrew:
return "brew upgrade keyway"
default:
return "curl -fsSL https://keyway.sh/install.sh | sh"
}
}
// IsNewerVersion returns true if latest is newer than current
// Handles semver format: v1.2.3 or 1.2.3
func IsNewerVersion(latest, current string) bool {
latestParts := parseVersion(latest)
currentParts := parseVersion(current)
if len(latestParts) == 0 || len(currentParts) == 0 {
return false
}
// Compare major, minor, patch
for i := 0; i < 3; i++ {
latestPart := 0
currentPart := 0
if i < len(latestParts) {
latestPart = latestParts[i]
}
if i < len(currentParts) {
currentPart = currentParts[i]
}
if latestPart > currentPart {
return true
}
if latestPart < currentPart {
return false
}
}
return false
}
// parseVersion extracts major, minor, patch from a version string
func parseVersion(v string) []int {
// Strip 'v' prefix
v = strings.TrimPrefix(v, "v")
// Handle dirty/dev suffixes
if idx := strings.IndexAny(v, "-+"); idx != -1 {
v = v[:idx]
}
parts := strings.Split(v, ".")
result := make([]int, 0, 3)
for _, part := range parts {
n, err := strconv.Atoi(part)
if err != nil {
break
}
result = append(result, n)
}
return result
}