diff --git a/CHANGELOG.md b/CHANGELOG.md index e1dc6354..709b8146 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). +## 3.9.1 + +- Fix version comparison mechanism for `update` command. + Not it compares each fragment separately (major, minor, patch) instead of comparing two versions as strings lexicographically. + ## 3.9.0 - New sources: [Mangakakalot](https://mangakakalot.com) & [Mangapill](https://mangapill.com) diff --git a/constant/build.go b/constant/build.go index c3c3a6d2..e52aed43 100644 --- a/constant/build.go +++ b/constant/build.go @@ -1,7 +1,5 @@ package constant -const Version = "3.9.0" - var ( BuiltAt = "Unknown" BuiltBy = "Unknown" diff --git a/constant/constant.go b/constant/constant.go index d3f50701..85fdef70 100644 --- a/constant/constant.go +++ b/constant/constant.go @@ -1,9 +1,9 @@ package constant const ( - Mangal = "mangal" - UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" - InstallScriptURL = "https://raw.githubusercontent.com/metafates/mangal/main/scripts/install" + Mangal = "mangal" + Version = "3.9.1" + UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" ) const ( diff --git a/updater/update.go b/updater/update.go index 769e05b4..6a6d96cd 100644 --- a/updater/update.go +++ b/updater/update.go @@ -40,7 +40,13 @@ func Update() (err error) { } erase() - if constant.Version >= version { + + comp, err := util.CompareVersions(constant.Version, version) + if err != nil { + return err + } + + if comp >= 0 { fmt.Printf( "%s %s %s\n", style.Green("Congrats!"), @@ -50,6 +56,8 @@ func Update() (err error) { return } + fmt.Printf("%s %s is out! You're on %s\n", style.Bold(constant.Mangal), style.Cyan(version), style.Blue(constant.Version)) + method := DetectInstallationMethod() switch method { @@ -74,8 +82,7 @@ func Update() (err error) { return } - fmt.Printf(`Updated. - + fmt.Printf(` %s Report any bugs: diff --git a/util/util.go b/util/util.go index 53489ec7..e4ea4619 100644 --- a/util/util.go +++ b/util/util.go @@ -152,3 +152,41 @@ func PrintErasable(msg string) (eraser func()) { func Capitalize(s string) string { return strings.ToUpper(s[:1]) + s[1:] } + +func CompareVersions(a, b string) (int, error) { + type version struct { + major, minor, patch int + } + + parse := func(s string) (version, error) { + var v version + _, err := fmt.Sscanf(strings.TrimPrefix(s, "v"), "%d.%d.%d", &v.major, &v.minor, &v.patch) + return v, err + } + + av, err := parse(a) + if err != nil { + return 0, err + } + + bv, err := parse(b) + if err != nil { + return 0, err + } + + for _, pair := range []lo.Tuple2[int, int]{ + {av.major, bv.major}, + {av.minor, bv.minor}, + {av.patch, bv.patch}, + } { + if pair.A > pair.B { + return 1, nil + } + + if pair.A < pair.B { + return -1, nil + } + } + + return 0, nil +} diff --git a/util/util_test.go b/util/util_test.go index 7c620064..0f7c37ac 100644 --- a/util/util_test.go +++ b/util/util_test.go @@ -129,3 +129,87 @@ func TestSanitizeFilename(t *testing.T) { func TestTerminalSize(t *testing.T) { t.Skipf("Cannot test terminal size") } + +func TestCompareVersions(t *testing.T) { + Convey("Given two versions with different patches", t, func() { + v1, v2 := "1.0.0", "1.0.1" + Convey("When comparing "+v1+" to "+v2, func() { + result, err := CompareVersions(v1, v2) + Convey("Error should be nil", func() { + So(err, ShouldBeNil) + Convey("Then the result should be -1", func() { + So(result, ShouldEqual, -1) + }) + }) + }) + + Convey("When comparing "+v2+" to "+v1, func() { + result, err := CompareVersions(v2, v1) + Convey("Error should be nil", func() { + So(err, ShouldBeNil) + Convey("Then the result should be 1", func() { + So(result, ShouldEqual, 1) + }) + }) + }) + }) + + Convey("Given two versions with different minor versions", t, func() { + v1, v2 := "1.0.0", "1.1.0" + Convey("When comparing "+v1+" to "+v2, func() { + result, err := CompareVersions(v1, v2) + Convey("Error should be nil", func() { + So(err, ShouldBeNil) + Convey("Then the result should be -1", func() { + So(result, ShouldEqual, -1) + }) + }) + }) + + Convey("When comparing "+v2+" to "+v1, func() { + result, err := CompareVersions(v2, v1) + Convey("Error should be nil", func() { + So(err, ShouldBeNil) + Convey("Then the result should be 1", func() { + So(result, ShouldEqual, 1) + }) + }) + }) + }) + + Convey("Given two versions with different major versions", t, func() { + v1, v2 := "1.0.0", "2.0.0" + Convey("When comparing "+v1+" to "+v2, func() { + result, err := CompareVersions(v1, v2) + Convey("Error should be nil", func() { + So(err, ShouldBeNil) + Convey("Then the result should be -1", func() { + So(result, ShouldEqual, -1) + }) + }) + }) + + Convey("When comparing "+v2+" to "+v1, func() { + result, err := CompareVersions(v2, v1) + Convey("Error should be nil", func() { + So(err, ShouldBeNil) + Convey("Then the result should be 1", func() { + So(result, ShouldEqual, 1) + }) + }) + }) + }) + + Convey("Given two same versions", t, func() { + v1, v2 := "1.0.0", "1.0.0" + Convey("When comparing "+v1+" to "+v2, func() { + result, err := CompareVersions(v1, v2) + Convey("Error should be nil", func() { + So(err, ShouldBeNil) + Convey("Then the result should be 0", func() { + So(result, ShouldEqual, 0) + }) + }) + }) + }) +}