Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions pkg/cli/version_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -124,6 +125,51 @@ func normalizeVersion(version string) string {
return strings.TrimPrefix(version, "v")
}

// parseVersion parses a version string into its numeric components
func parseVersion(version string) []int {
parts := strings.Split(normalizeVersion(version), ".")
result := make([]int, len(parts))
for i, part := range parts {
num, err := strconv.Atoi(part)
if err != nil {
result[i] = 0
} else {
result[i] = num
}
}
return result
}

// compareVersions compares two version strings numerically
// Returns: -1 if v1 < v2, 0 if v1 == v2, 1 if v1 > v2
func compareVersions(v1, v2 string) int {
parts1 := parseVersion(v1)
parts2 := parseVersion(v2)

maxLen := len(parts1)
if len(parts2) > maxLen {
maxLen = len(parts2)
}

for i := 0; i < maxLen; i++ {
var p1, p2 int
if i < len(parts1) {
p1 = parts1[i]
}
if i < len(parts2) {
p2 = parts2[i]
}

if p1 < p2 {
return -1
}
if p1 > p2 {
return 1
}
}
return 0
}

// CheckForUpdate checks if a newer version is available and prints a notification
// This function is designed to fail silently if offline or if there are errors
func CheckForUpdate() {
Expand Down Expand Up @@ -154,15 +200,7 @@ func CheckForUpdate() {

// compareAndNotify compares versions and prints update notification if needed
func compareAndNotify(latestVersion string) {
current := normalizeVersion(Version)
latest := normalizeVersion(latestVersion)

if current == latest {
return
}

// Simple version comparison (assumes semantic versioning)
if current < latest {
if compareVersions(Version, latestVersion) < 0 {
fmt.Fprintf(os.Stderr, "\n⚠️ New version available: %s (current: %s)\n", latestVersion, Version)
fmt.Fprintf(os.Stderr, " Update with: brew upgrade llmkube\n")
fmt.Fprintf(os.Stderr, " Or download from: https://github.com/defilantech/LLMKube/releases/latest\n\n")
Expand Down
82 changes: 82 additions & 0 deletions pkg/cli/version_check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2025.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cli

import "testing"

func TestCompareVersions(t *testing.T) {
tests := []struct {
name string
v1 string
v2 string
expected int
}{
{"equal versions", "1.0.0", "1.0.0", 0},
{"v1 less than v2 patch", "1.0.0", "1.0.1", -1},
{"v1 greater than v2 patch", "1.0.1", "1.0.0", 1},
{"v1 less than v2 minor", "1.0.0", "1.1.0", -1},
{"v1 greater than v2 minor", "1.1.0", "1.0.0", 1},
{"v1 less than v2 major", "1.0.0", "2.0.0", -1},
{"v1 greater than v2 major", "2.0.0", "1.0.0", 1},
{"double digit patch comparison", "0.4.9", "0.4.10", -1},
{"double digit patch comparison reverse", "0.4.10", "0.4.9", 1},
{"triple digit patch", "1.0.99", "1.0.100", -1},
{"with v prefix", "v1.0.0", "v1.0.1", -1},
{"mixed v prefix", "v1.0.0", "1.0.1", -1},
{"double digit minor", "1.9.0", "1.10.0", -1},
{"equal with v prefix", "v0.4.10", "0.4.10", 0},
{"real world case", "0.4.10", "0.4.9", 1},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := compareVersions(tt.v1, tt.v2)
if result != tt.expected {
t.Errorf("compareVersions(%q, %q) = %d, expected %d", tt.v1, tt.v2, result, tt.expected)
}
})
}
}

func TestParseVersion(t *testing.T) {
tests := []struct {
name string
version string
expected []int
}{
{"simple version", "1.2.3", []int{1, 2, 3}},
{"with v prefix", "v1.2.3", []int{1, 2, 3}},
{"double digits", "1.10.100", []int{1, 10, 100}},
{"two parts", "1.2", []int{1, 2}},
{"single part", "1", []int{1}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := parseVersion(tt.version)
if len(result) != len(tt.expected) {
t.Errorf("parseVersion(%q) returned %d parts, expected %d", tt.version, len(result), len(tt.expected))
return
}
for i, v := range result {
if v != tt.expected[i] {
t.Errorf("parseVersion(%q)[%d] = %d, expected %d", tt.version, i, v, tt.expected[i])
}
}
})
}
}