From e22df5af5c9688fb643a3730b11b58428f6d23e4 Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 8 Nov 2021 13:57:36 -0600 Subject: [PATCH] Use go-version to compare version --- go.mod | 1 + go.sum | 2 ++ internal/core/box_metadata.go | 20 ++++++++++++++------ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 4082b2e74..81e0f93e2 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 88a4fc513..ad2c4bf6e 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/core/box_metadata.go b/internal/core/box_metadata.go index e2c7d1f05..a715769a4 100644 --- a/internal/core/box_metadata.go +++ b/internal/core/box_metadata.go @@ -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 } } }