Skip to content

Commit e0aac90

Browse files
authored
Fix bug not being able to upgrade from 0.99.x to 0.100.x (#951)
2 parents c9410bb + 1fb0648 commit e0aac90

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

internal/cmd/update.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/exec"
77
"path/filepath"
8+
"strconv"
89
"strings"
910

1011
"github.com/spf13/cobra"
@@ -36,6 +37,37 @@ func IsUnderHomebrew() bool {
3637
return strings.HasPrefix(binary, brewBinPrefix)
3738
}
3839

40+
func semverCompare(a, b string) int {
41+
a = strings.TrimPrefix(a, "v")
42+
b = strings.TrimPrefix(b, "v")
43+
44+
partsA := strings.Split(a, ".")
45+
partsB := strings.Split(b, ".")
46+
47+
// fall back to lexicographic comparison if both don't have 3 parts
48+
if len(partsA) != 3 || len(partsB) != 3 {
49+
return strings.Compare(a, b)
50+
}
51+
52+
// compare each part, and again fall back to lexicographic comparison if any part is not an integer
53+
for i := 0; i < 3; i++ {
54+
numA, err := strconv.Atoi(partsA[i])
55+
if err != nil {
56+
return strings.Compare(a, b)
57+
}
58+
numB, err := strconv.Atoi(partsB[i])
59+
if err != nil {
60+
return strings.Compare(a, b)
61+
}
62+
if numA < numB {
63+
return -1
64+
} else if numA > numB {
65+
return 1
66+
}
67+
}
68+
return 0
69+
}
70+
3971
var updateCmd = &cobra.Command{
4072
Use: "update",
4173
Short: "Update the CLI to the latest version",
@@ -52,7 +84,7 @@ var updateCmd = &cobra.Command{
5284
if version == "dev" {
5385
fmt.Println("You're compiling from source. How much more up2date do you want to be?")
5486
return nil
55-
} else if version >= latest {
87+
} else if semverCompare(version, latest) >= 0 {
5688
fmt.Printf("version %s is already latest\n", internal.Emph(version))
5789
return nil
5890
}

0 commit comments

Comments
 (0)