From 271be3456d14ea605f9d857df21f7ebdccb25909 Mon Sep 17 00:00:00 2001 From: Sebastian Choren Date: Wed, 25 Oct 2023 18:04:21 -0300 Subject: [PATCH] feat: use semver for version check (#3295) --- cli/config/version.go | 41 ++++++++++++++++++++++++++++++++++++++--- go.mod | 1 + go.sum | 2 ++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/cli/config/version.go b/cli/config/version.go index b89a9dcd05..524ac1ccd9 100644 --- a/cli/config/version.go +++ b/cli/config/version.go @@ -2,8 +2,10 @@ package config import ( "context" + "errors" "fmt" + "github.com/Masterminds/semver/v3" "github.com/kubeshop/tracetest/cli/openapi" ) @@ -31,15 +33,48 @@ Server: Not Configured`, false Server: Failed to get the server version - %s`, err.Error()), false } + versionMatch := false version := meta.GetVersion() - isVersionMatch := version == Version - if isVersionMatch { + if !isSemver(version) || !isSemver(Version) { + // if either version is not semver, we can't compare them + // do a basic strict compare + + versionMatch = version == Version + } else { + + serverVersion, err := semver.NewVersion(version) + if err != nil { + return result + fmt.Sprintf(` +Server: Failed to parse the server version - %s`, err.Error()), false + } + + cliVersion, err := semver.NewVersion(Version) + if err != nil { + return result + fmt.Sprintf(` +Failed to parse the CLI version - %s`, err.Error()), false + } + + versionConstrait, err := semver.NewConstraint(fmt.Sprintf(">=%d.%d", cliVersion.Major(), cliVersion.Minor())) + if err != nil { + return result + fmt.Sprintf(` +Failed to parse the CLI version constraint - %s`, err.Error()), false + } + + versionMatch = versionConstrait.Check(serverVersion) + } + + if versionMatch { version += ` ✔️ Version match` } return result + fmt.Sprintf(` -Server: %s`, version), isVersionMatch +Server: %s`, version), versionMatch +} + +func isSemver(version string) bool { + _, err := semver.NewVersion(version) + return !errors.Is(err, semver.ErrInvalidSemVer) } func getVersionMetadata(ctx context.Context, client *openapi.APIClient) (*openapi.Version, error) { diff --git a/go.mod b/go.mod index 1d6483514f..60d9fcce17 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/Code-Hex/go-generics-cache v1.3.1 github.com/IBM/sarama v1.40.1 github.com/Jeffail/gabs/v2 v2.7.0 + github.com/Masterminds/semver/v3 v3.2.1 github.com/agnivade/levenshtein v1.0.1 github.com/alecthomas/participle/v2 v2.0.0-alpha8 github.com/alexeyco/simpletable v1.0.0 diff --git a/go.sum b/go.sum index 12609a7ea8..2153028c56 100644 --- a/go.sum +++ b/go.sum @@ -153,6 +153,8 @@ github.com/MarvinJWendt/testza v0.4.2/go.mod h1:mSdhXiKH8sg/gQehJ63bINcCKp7RtYew github.com/MarvinJWendt/testza v0.5.2 h1:53KDo64C1z/h/d/stCYCPY69bt/OSwjq5KpFNwi+zB4= github.com/MarvinJWendt/testza v0.5.2/go.mod h1:xu53QFE5sCdjtMCKk8YMQ2MnymimEctc4n3EjyIYvEY= github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= +github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0= +github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= github.com/Masterminds/sprig v2.16.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o= github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA= github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=