55 "os"
66 "os/exec"
77 "path/filepath"
8+ "strconv"
89 "strings"
910
1011 "github.com/spf13/cobra"
@@ -36,6 +37,39 @@ func IsUnderHomebrew() bool {
3637 return strings .HasPrefix (binary , brewBinPrefix )
3738}
3839
40+ func semverCompare (a , b string ) int {
41+ // cut v suffix
42+ a = strings .TrimPrefix (a , "v" )
43+ b = strings .TrimPrefix (b , "v" )
44+
45+ // compare
46+ partsA := strings .Split (a , "." )
47+ partsB := strings .Split (b , "." )
48+
49+ // fall back to lexicographic comparison if both don't have 3 parts
50+ if len (partsA ) != 3 || len (partsB ) != 3 {
51+ return strings .Compare (a , b )
52+ }
53+
54+ // compare each part, and again fall back to lexicographic comparison if any part is not an integer
55+ for i := 0 ; i < 3 ; i ++ {
56+ numA , err := strconv .Atoi (partsA [i ])
57+ if err != nil {
58+ return strings .Compare (a , b )
59+ }
60+ numB , err := strconv .Atoi (partsB [i ])
61+ if err != nil {
62+ return strings .Compare (a , b )
63+ }
64+ if numA < numB {
65+ return - 1
66+ } else if numA > numB {
67+ return 1
68+ }
69+ }
70+ return 0
71+ }
72+
3973var updateCmd = & cobra.Command {
4074 Use : "update" ,
4175 Short : "Update the CLI to the latest version" ,
@@ -52,7 +86,7 @@ var updateCmd = &cobra.Command{
5286 if version == "dev" {
5387 fmt .Println ("You're compiling from source. How much more up2date do you want to be?" )
5488 return nil
55- } else if version >= latest {
89+ } else if semverCompare ( version , latest ) >= 0 {
5690 fmt .Printf ("version %s is already latest\n " , internal .Emph (version ))
5791 return nil
5892 }
0 commit comments