Merge pull request #1621 from Zillode/fix-no-upgrade

Fix compilation of -noupgrade builds
This commit is contained in:
Jakob Borg 2015-04-10 08:30:50 +02:00
commit 7df9c1b6e4
5 changed files with 28 additions and 14 deletions

View File

@ -681,7 +681,7 @@ func restGetSystemUpgrade(w http.ResponseWriter, r *http.Request) {
http.Error(w, upgrade.ErrUpgradeUnsupported.Error(), 500) http.Error(w, upgrade.ErrUpgradeUnsupported.Error(), 500)
return return
} }
rel, err := upgrade.LatestGithubRelease(Version) rel, err := upgrade.LatestRelease(Version)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
@ -723,7 +723,7 @@ func restGetLang(w http.ResponseWriter, r *http.Request) {
} }
func restPostSystemUpgrade(w http.ResponseWriter, r *http.Request) { func restPostSystemUpgrade(w http.ResponseWriter, r *http.Request) {
rel, err := upgrade.LatestGithubRelease(Version) rel, err := upgrade.LatestRelease(Version)
if err != nil { if err != nil {
l.Warnln("getting latest release:", err) l.Warnln("getting latest release:", err)
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)

View File

@ -325,7 +325,7 @@ func main() {
} }
if doUpgrade || doUpgradeCheck { if doUpgrade || doUpgradeCheck {
rel, err := upgrade.LatestGithubRelease(Version) rel, err := upgrade.LatestRelease(Version)
if err != nil { if err != nil {
l.Fatalln("Upgrade:", err) // exits 1 l.Fatalln("Upgrade:", err) // exits 1
} }
@ -965,7 +965,7 @@ func autoUpgrade() {
case <-timer.C: case <-timer.C:
} }
rel, err := upgrade.LatestGithubRelease(Version) rel, err := upgrade.LatestRelease(Version)
if err == upgrade.ErrUpgradeUnsupported { if err == upgrade.ErrUpgradeUnsupported {
events.Default.Unsubscribe(sub) events.Default.Unsubscribe(sub)
return return

View File

@ -27,21 +27,21 @@ import (
"strings" "strings"
) )
// Returns the latest release, including prereleases or not depending on the argument // Returns the latest releases, including prereleases or not depending on the argument
func LatestGithubRelease(version string) (Release, error) { func LatestGithubReleases(version string) ([]Release, error) {
resp, err := http.Get("https://api.github.com/repos/syncthing/syncthing/releases?per_page=10") resp, err := http.Get("https://api.github.com/repos/syncthing/syncthing/releases?per_page=30")
if err != nil { if err != nil {
return Release{}, err return nil, err
} }
if resp.StatusCode > 299 { if resp.StatusCode > 299 {
return Release{}, fmt.Errorf("API call returned HTTP error: %s", resp.Status) return nil, fmt.Errorf("API call returned HTTP error: %s", resp.Status)
} }
var rels []Release var rels []Release
json.NewDecoder(resp.Body).Decode(&rels) json.NewDecoder(resp.Body).Decode(&rels)
resp.Body.Close() resp.Body.Close()
return LatestRelease(version, rels) return rels, nil
} }
type SortByRelease []Release type SortByRelease []Release
@ -56,7 +56,12 @@ func (s SortByRelease) Less(i, j int) bool {
return CompareVersions(s[i].Tag, s[j].Tag) > 0 return CompareVersions(s[i].Tag, s[j].Tag) > 0
} }
func LatestRelease(version string, rels []Release) (Release, error) { func LatestRelease(version string) (Release, error) {
rels, _ := LatestGithubReleases(version)
return SelectLatestRelease(version, rels)
}
func SelectLatestRelease(version string, rels []Release) (Release, error) {
if len(rels) == 0 { if len(rels) == 0 {
return Release{}, ErrVersionUnknown return Release{}, ErrVersionUnknown
} }

View File

@ -4,6 +4,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file, // License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/. // You can obtain one at http://mozilla.org/MPL/2.0/.
// +build !noupgrade
package upgrade package upgrade
import ( import (
@ -65,7 +67,7 @@ var upgrades = map[string]string{
"v0.11.0-beta0+40-g53cb66e-dirty": "v0.11.0-beta0", "v0.11.0-beta0+40-g53cb66e-dirty": "v0.11.0-beta0",
} }
func TestRelease(t *testing.T) { func TestGithubRelease(t *testing.T) {
fd, err := os.Open("testdata/github-releases.json") fd, err := os.Open("testdata/github-releases.json")
if err != nil { if err != nil {
t.Errorf("Missing github-release test data") t.Errorf("Missing github-release test data")
@ -76,7 +78,7 @@ func TestRelease(t *testing.T) {
json.NewDecoder(fd).Decode(&rels) json.NewDecoder(fd).Decode(&rels)
for old, target := range upgrades { for old, target := range upgrades {
upgrade, err := LatestRelease(old, rels) upgrade, err := SelectLatestRelease(old, rels)
if err != nil { if err != nil {
t.Error("Error retrieving latest version", err) t.Error("Error retrieving latest version", err)
} }
@ -85,3 +87,10 @@ func TestRelease(t *testing.T) {
} }
} }
} }
func TestErrorRelease(t *testing.T) {
_, err := SelectLatestRelease("v0.11.0-beta", nil)
if err == nil {
t.Error("Should return an error when no release were available")
}
}

View File

@ -16,6 +16,6 @@ func upgradeToURL(binary, url string) error {
return ErrUpgradeUnsupported return ErrUpgradeUnsupported
} }
func LatestRelease(prerelease bool) (Release, error) { func LatestRelease(version string) (Release, error) {
return Release{}, ErrUpgradeUnsupported return Release{}, ErrUpgradeUnsupported
} }