Use go-version to compare version

This commit is contained in:
sophia 2021-11-08 13:57:36 -06:00 committed by Paul Hinze
parent 01b63b559a
commit e22df5af5c
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
3 changed files with 17 additions and 6 deletions

1
go.mod
View File

@ -33,6 +33,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-plugin v1.3.0
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl/v2 v2.7.1-0.20201023000745-3de61ecba298
github.com/hashicorp/nomad/api v0.0.0-20200814140818-42de70466a9d

2
go.sum
View File

@ -200,6 +200,8 @@ github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR3
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
github.com/hashicorp/go-version v1.3.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/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"reflect"
"github.com/hashicorp/go-version"
"github.com/mitchellh/mapstructure"
)
@ -81,23 +82,30 @@ func LoadBoxMetadata(data []byte) (*BoxMetadata, error) {
return &result, mapstructure.Decode(metadata, &result)
}
func (b *BoxMetadata) Version(version string, providerOpts *BoxVersionProvider) (v *BoxVersion, err error) {
func (b *BoxMetadata) Version(ver string, providerOpts *BoxVersionProvider) (v *BoxVersion, err error) {
matchesProvider := false
for _, ver := range b.Versions {
if ver.Version == version {
inputVersion, err := version.NewVersion(ver)
if err != nil {
return nil, err
}
for _, boxVer := range b.Versions {
boxVersion, err := version.NewVersion(boxVer.Version)
if err != nil {
return nil, err
}
if boxVersion.Equal(inputVersion) {
// Check for the provider in the version
if providerOpts == nil {
matchesProvider = true
} else {
for _, p := range ver.Providers {
for _, p := range boxVer.Providers {
if p.Matches(providerOpts) {
matchesProvider = true
}
}
}
if matchesProvider {
return ver, nil
return boxVer, nil
}
}
}