Skip to content

Commit

Permalink
feat: use semver for version check (#3295)
Browse files Browse the repository at this point in the history
  • Loading branch information
schoren authored Oct 25, 2023
1 parent 440c721 commit 271be34
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
41 changes: 38 additions & 3 deletions cli/config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package config

import (
"context"
"errors"
"fmt"

"github.com/Masterminds/semver/v3"
"github.com/kubeshop/tracetest/cli/openapi"
)

Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 271be34

Please sign in to comment.