Skip to content

Commit 3a51410

Browse files
committed
versioning update
1 parent 64a3b13 commit 3a51410

3 files changed

Lines changed: 90 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
# Go workspace file
2121
go.work
2222
.DS_Store
23+
base

commands/upgrade.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,17 +396,33 @@ func getTargetVersion(allowMajor bool) (*version.Release, string, error) {
396396
if !allowMajor {
397397
// Get current version info to determine what major version to stay within
398398
info := version.GetBuildInfo()
399-
currentMajor := strings.Split(strings.TrimPrefix(info.Version, "v"), ".")[0]
399+
currentVersion := strings.TrimPrefix(info.Version, "v")
400+
currentMajor := strings.Split(currentVersion, ".")[0]
400401

401402
// If the latest version is in the same major version, use it
402403
if strings.HasPrefix(latestVersion, currentMajor+".") {
403404
return release, latestVersion, nil
404405
}
405406

406-
// Otherwise, we need to find the latest version within the current major version
407-
// For now, return the current version (no upgrade available within major version)
408-
// In a full implementation, you'd query all releases and find the latest within the major version
409-
return release, info.Version, nil
407+
// Otherwise, find the latest version within the current major version
408+
// Query all releases to find the latest patch/minor within current major
409+
allReleases, err := version.GetAllReleases()
410+
if err != nil {
411+
// Fallback: return current version if we can't get all releases
412+
return release, currentVersion, nil
413+
}
414+
415+
latestInMajor := currentVersion
416+
for _, r := range allReleases {
417+
releaseVersion := strings.TrimPrefix(r.TagName, "v")
418+
if strings.HasPrefix(releaseVersion, currentMajor+".") {
419+
if version.CompareVersions(releaseVersion, latestInMajor) > 0 {
420+
latestInMajor = releaseVersion
421+
}
422+
}
423+
}
424+
425+
return release, latestInMajor, nil
410426
}
411427

412428
return release, latestVersion, nil

version/version.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,74 @@ func HasUpdate(current, latest string) bool {
101101
return current != latest
102102
}
103103

104+
// GetAllReleases gets all releases from GitHub
105+
func GetAllReleases() ([]Release, error) {
106+
url := "https://api.github.com/repos/base-go/cmd/releases"
107+
resp, err := http.Get(url)
108+
if err != nil {
109+
return nil, err
110+
}
111+
defer resp.Body.Close()
112+
113+
body, err := io.ReadAll(resp.Body)
114+
if err != nil {
115+
return nil, err
116+
}
117+
118+
var releases []Release
119+
if err := json.Unmarshal(body, &releases); err != nil {
120+
return nil, err
121+
}
122+
123+
return releases, nil
124+
}
125+
126+
// CompareVersions compares two semantic versions
127+
// Returns: -1 if v1 < v2, 0 if v1 == v2, 1 if v1 > v2
128+
func CompareVersions(v1, v2 string) int {
129+
// Remove 'v' prefix if present
130+
v1 = strings.TrimPrefix(v1, "v")
131+
v2 = strings.TrimPrefix(v2, "v")
132+
133+
if v1 == v2 {
134+
return 0
135+
}
136+
137+
// Split versions into parts
138+
parts1 := strings.Split(v1, ".")
139+
parts2 := strings.Split(v2, ".")
140+
141+
// Pad shorter version with zeros
142+
maxLen := len(parts1)
143+
if len(parts2) > maxLen {
144+
maxLen = len(parts2)
145+
}
146+
147+
for len(parts1) < maxLen {
148+
parts1 = append(parts1, "0")
149+
}
150+
for len(parts2) < maxLen {
151+
parts2 = append(parts2, "0")
152+
}
153+
154+
// Compare each part numerically
155+
for i := 0; i < maxLen; i++ {
156+
// Convert to integers for proper numeric comparison
157+
var num1, num2 int
158+
fmt.Sscanf(parts1[i], "%d", &num1)
159+
fmt.Sscanf(parts2[i], "%d", &num2)
160+
161+
if num1 < num2 {
162+
return -1
163+
}
164+
if num1 > num2 {
165+
return 1
166+
}
167+
}
168+
169+
return 0
170+
}
171+
104172
// FormatUpdateMessage returns a formatted update message
105173
func FormatUpdateMessage(current, latest, releaseURL, releaseNotes string) string {
106174
var sb strings.Builder

0 commit comments

Comments
 (0)