synced with master
This commit is contained in:
		
						commit
						651b439096
					
				|  | @ -14,6 +14,7 @@ import ( | |||
| 	"path" | ||||
| 	"path/filepath" | ||||
| 	"runtime" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| 
 | ||||
| 	"github.com/fatih/color" | ||||
|  | @ -138,9 +139,7 @@ func (p *plugin) update(withBackup bool) error { | |||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
| 	// @todo consider using an external library OR update to check
 | ||||
| 	// to handle cases like v0.16.2 >= v0.16.10
 | ||||
| 	if strings.TrimPrefix(p.currentVersion, "v") >= strings.TrimPrefix(latest.Tag, "v") { | ||||
| 	if compareVersions(strings.TrimPrefix(p.currentVersion, "v"), strings.TrimPrefix(latest.Tag, "v")) <= 0 { | ||||
| 		color.Green("You already have the latest PocketBase %s.", p.currentVersion) | ||||
| 		return nil | ||||
| 	} | ||||
|  | @ -329,3 +328,38 @@ func archiveSuffix(goos, goarch string) string { | |||
| 
 | ||||
| 	return "" | ||||
| } | ||||
| 
 | ||||
| func compareVersions(a, b string) int { | ||||
| 	aSplit := strings.Split(a, ".") | ||||
| 	aTotal := len(aSplit) | ||||
| 
 | ||||
| 	bSplit := strings.Split(b, ".") | ||||
| 	bTotal := len(bSplit) | ||||
| 
 | ||||
| 	limit := aTotal | ||||
| 	if bTotal > aTotal { | ||||
| 		limit = bTotal | ||||
| 	} | ||||
| 
 | ||||
| 	for i := 0; i < limit; i++ { | ||||
| 		var x, y int | ||||
| 
 | ||||
| 		if i < aTotal { | ||||
| 			x, _ = strconv.Atoi(aSplit[i]) | ||||
| 		} | ||||
| 
 | ||||
| 		if i < bTotal { | ||||
| 			y, _ = strconv.Atoi(bSplit[i]) | ||||
| 		} | ||||
| 
 | ||||
| 		if x < y { | ||||
| 			return 1 // b is newer
 | ||||
| 		} | ||||
| 
 | ||||
| 		if x > y { | ||||
| 			return -1 // a is newer
 | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	return 0 // equal
 | ||||
| } | ||||
|  |  | |||
|  | @ -0,0 +1,32 @@ | |||
| package ghupdate | ||||
| 
 | ||||
| import "testing" | ||||
| 
 | ||||
| func TestCompareVersions(t *testing.T) { | ||||
| 	scenarios := []struct { | ||||
| 		a        string | ||||
| 		b        string | ||||
| 		expected int | ||||
| 	}{ | ||||
| 		{"", "", 0}, | ||||
| 		{"0", "", 0}, | ||||
| 		{"1", "1.0.0", 0}, | ||||
| 		{"1.1", "1.1.0", 0}, | ||||
| 		{"1.1", "1.1.1", 1}, | ||||
| 		{"1.1", "1.0.1", -1}, | ||||
| 		{"1.0", "1.0.1", 1}, | ||||
| 		{"1.10", "1.9", -1}, | ||||
| 		{"1.2", "1.12", 1}, | ||||
| 		{"3.2", "1.6", -1}, | ||||
| 		{"3.2", "4.0", 1}, | ||||
| 		{"3.2.4", "3.2.3", -1}, | ||||
| 	} | ||||
| 
 | ||||
| 	for i, s := range scenarios { | ||||
| 		result := compareVersions(s.a, s.b) | ||||
| 
 | ||||
| 		if result != s.expected { | ||||
| 			t.Fatalf("[%d] Expected %q vs %q to result in %d, got %d", i, s.a, s.b, s.expected, result) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
		Loading…
	
		Reference in New Issue