From 46190590cbfe4e7ede36e1434b3741bcad990f56 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 4 May 2017 14:37:16 -0400 Subject: [PATCH] change []*Version to []Version Versions are used as values, so don't keep them as pointers here --- plugin/discovery/get.go | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/plugin/discovery/get.go b/plugin/discovery/get.go index e9bc927a8..c53dd0ff5 100644 --- a/plugin/discovery/get.go +++ b/plugin/discovery/get.go @@ -69,8 +69,8 @@ func GetProvider(dst, provider string, req Constraints) error { return err } - version := newestVersion(versions, req) - if version == nil { + version, err := newestVersion(versions, req) + if err != nil { return fmt.Errorf("no version of %q available that fulfills constraints %s", provider, req) } @@ -80,29 +80,37 @@ func GetProvider(dst, provider string, req Constraints) error { return getter.Get(dst, url) } +var errVersionNotFound = errors.New("version not found") + // take the list of available versions for a plugin, and the required // Constraints, and return the latest available version that satisfies the // constraints. -func newestVersion(available []*Version, required Constraints) *Version { - var latest *Version +func newestVersion(available []Version, required Constraints) (Version, error) { + var latest Version + found := false + for _, v := range available { - if required.Has(*v) { - if latest == nil { + if required.Has(v) { + if !found { latest = v + found = true continue } - if v.NewerThan(*latest) { + if v.NewerThan(latest) { latest = v } } } - return latest + if !found { + return latest, errVersionNotFound + } + return latest, nil } // list the version available for the named plugin -func listProviderVersions(name string) ([]*Version, error) { +func listProviderVersions(name string) ([]Version, error) { versions, err := listPluginVersions(providersURL.versionsURL(name)) if err != nil { return nil, fmt.Errorf("failed to fetch versions for provider %q: %s", name, err) @@ -110,7 +118,7 @@ func listProviderVersions(name string) ([]*Version, error) { return versions, nil } -func listProvisionerVersions(name string) ([]*Version, error) { +func listProvisionerVersions(name string) ([]Version, error) { versions, err := listPluginVersions(provisionersURL.versionsURL(name)) if err != nil { return nil, fmt.Errorf("failed to fetch versions for provisioner %q: %s", name, err) @@ -123,7 +131,7 @@ func listProvisionerVersions(name string) ([]*Version, error) { // TODO: This doesn't yet take into account plugin protocol version. // That may have to be checked via an http header via a separate request // to each plugin file. -func listPluginVersions(url string) ([]*Version, error) { +func listPluginVersions(url string) ([]Version, error) { resp, err := http.Get(url) if err != nil { return nil, err @@ -159,7 +167,7 @@ func listPluginVersions(url string) ([]*Version, error) { } f(body) - var versions []*Version + var versions []Version for _, name := range names { parts := strings.SplitN(name, "_", 2) @@ -171,7 +179,7 @@ func listPluginVersions(url string) ([]*Version, error) { continue } - versions = append(versions, &v) + versions = append(versions, v) } }