Skip to content

Commit

Permalink
Fix for improper warning semver comparisson #27
Browse files Browse the repository at this point in the history
  • Loading branch information
akranga committed Feb 5, 2023
1 parent 88782b9 commit 55c50b1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
48 changes: 33 additions & 15 deletions cmd/hub/lifecycle/requirement.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/epam/hubctl/cmd/hub/manifest"
"github.com/epam/hubctl/cmd/hub/parameters"
"github.com/epam/hubctl/cmd/hub/util"
version "github.com/hashicorp/go-version"
)

const (
Expand Down Expand Up @@ -171,17 +172,25 @@ var bins = map[string][]string{
}

type BinVersion struct {
minVersion string
minVersion *version.Version
versionRegexp *regexp.Regexp
}

var binVersion = map[string]BinVersion{
"gcloud": {"246.0.0", regexp.MustCompile(`Google Cloud SDK ([\d.]+)`)},
"gsutil": {"4.38", regexp.MustCompile(`version: ([\d.]+)`)},
"vault": {"1.3.2", regexp.MustCompile(`Vault v([\d.]+)`)},
"kubectl": {"1.18.15", regexp.MustCompile(`GitVersion:"v([\d.]+)`)},
"helm": {"3.5.1", regexp.MustCompile(`(?:SemVer|Version):"v([\d.]+)`)},
"terraform": {"0.14.0", regexp.MustCompile(`Terraform v([\d.]+)`)},
func semver(s string) *version.Version {
v, err := version.NewVersion(s)
if err != nil {
log.Printf("Invalid version: %s", s)
}
return v
}

var binVersion = map[string]*BinVersion{
"gcloud": {semver("246.0.0"), regexp.MustCompile(`Google Cloud SDK ([\d.]+)`)},
"gsutil": {semver("4.38"), regexp.MustCompile(`version: ([\d.]+)`)},
"vault": {semver("1.3.2"), regexp.MustCompile(`Vault v([\d.]+)`)},
"kubectl": {semver("1.18.15"), regexp.MustCompile(`GitVersion:"v([\d.]+)`)},
"helm": {semver("3.5.1"), regexp.MustCompile(`(?:SemVer|Version):"v([\d.]+)`)},
"terraform": {semver("0.14.0"), regexp.MustCompile(`Terraform v([\d.]+)`)},
}

func checkStackRequires(requires []string, optional, requiresOfOptionalComponents map[string][]string) map[string][]string {
Expand Down Expand Up @@ -242,7 +251,7 @@ func checkRequire(require string) (bool, error) {
}
verReq, exist := binVersion[bin[0]]
if exist {
err := checkRequiresBinVersion(verReq.minVersion, verReq.versionRegexp, out)
err := checkRequiresBinVersion(verReq, out)
if err != nil {
util.WarnOnce("`%s` version requirement cannot be satisfied: %s: %v; update `%[1]s` binary?",
bin[0], require, err)
Expand Down Expand Up @@ -278,18 +287,27 @@ func checkRequiresBin(bin ...string) ([]byte, error) {
return out, err
}

func checkRequiresBinVersion(minVer string, verRegexp *regexp.Regexp, out []byte) error {
// validates binary version against minimum required version
//
// reqVer: required version and regexp to extract version string
// currVer: raw output from binary
//
// returns error version is not valid
func checkRequiresBinVersion(reqVer *BinVersion, out []byte) error {
if len(out) == 0 {
return errors.New("no output")
}
match := verRegexp.FindSubmatch(out)

match := reqVer.versionRegexp.FindSubmatch(out)
if len(match) != 2 {
return errors.New("no version string found")
}
ver := string(match[1])
// TODO here 1.6 > 1.10 which is not the case
if ver < minVer {
return fmt.Errorf("`%s` version detected; should have at least version `%s`", ver, minVer)
currVer, err := version.NewVersion(string(match[1]))
if err != nil {
return err
}
if currVer.LessThan(reqVer.minVersion) {
return fmt.Errorf("`%s` version detected; should have at least version `%s`", currVer, reqVer.minVersion.String())
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ require (
github.com/google/go-cmp v0.5.8 // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/googleapis/go-type-adapters v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.13 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down

0 comments on commit 55c50b1

Please sign in to comment.