Fix version comparison in upgrade
This commit is contained in:
parent
1cd7cc6869
commit
4031f5e24b
@ -1,11 +1,10 @@
|
|||||||
// +build !windows
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -14,8 +13,10 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"bytes"
|
||||||
"bitbucket.org/kardianos/osext"
|
"bitbucket.org/kardianos/osext"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,6 +34,10 @@ type githubAsset struct {
|
|||||||
var GoArchExtra string // "", "v5", "v6", "v7"
|
var GoArchExtra string // "", "v5", "v6", "v7"
|
||||||
|
|
||||||
func upgrade() error {
|
func upgrade() error {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
return errors.New("Upgrade currently unsupported on Windows")
|
||||||
|
}
|
||||||
|
|
||||||
path, err := osext.Executable()
|
path, err := osext.Executable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -52,14 +57,15 @@ func upgrade() error {
|
|||||||
}
|
}
|
||||||
rel := rels[0]
|
rel := rels[0]
|
||||||
|
|
||||||
if rel.Tag > Version {
|
switch compareVersions(rel.Tag, Version) {
|
||||||
l.Infof("Attempting upgrade to %s...", rel.Tag)
|
case -1:
|
||||||
} else if rel.Tag == Version {
|
|
||||||
l.Okf("Already running the latest version, %s. Not upgrading.", Version)
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
l.Okf("Current version %s is newer than latest release %s. Not upgrading.", Version, rel.Tag)
|
l.Okf("Current version %s is newer than latest release %s. Not upgrading.", Version, rel.Tag)
|
||||||
return nil
|
return nil
|
||||||
|
case 0:
|
||||||
|
l.Okf("Already running the latest version, %s. Not upgrading.", Version)
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
l.Infof("Attempting upgrade to %s...", rel.Tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedRelease := fmt.Sprintf("syncthing-%s-%s%s-%s.", runtime.GOOS, runtime.GOARCH, GoArchExtra, rel.Tag)
|
expectedRelease := fmt.Sprintf("syncthing-%s-%s%s-%s.", runtime.GOOS, runtime.GOARCH, GoArchExtra, rel.Tag)
|
||||||
@ -147,3 +153,18 @@ func readTarGZ(url string, dir string) (string, error) {
|
|||||||
|
|
||||||
return "", fmt.Errorf("No upgrade found")
|
return "", fmt.Errorf("No upgrade found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func compareVersions(a, b string) int {
|
||||||
|
return bytes.Compare(versionParts(a), versionParts(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
func versionParts(v string) []byte {
|
||||||
|
parts := strings.Split(v, "-")
|
||||||
|
fields := strings.Split(parts[0], ".")
|
||||||
|
res := make([]byte, len(fields))
|
||||||
|
for i, s := range fields {
|
||||||
|
v, _ := strconv.Atoi(s)
|
||||||
|
res[i] = byte(v)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
27
cmd/syncthing/upgrade_test.go
Normal file
27
cmd/syncthing/upgrade_test.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
var testcases = []struct {
|
||||||
|
a, b string
|
||||||
|
r int
|
||||||
|
}{
|
||||||
|
{"0.1.2", "0.1.2", 0},
|
||||||
|
{"0.1.3", "0.1.2", 1},
|
||||||
|
{"0.1.1", "0.1.2", -1},
|
||||||
|
{"0.3.0", "0.1.2", 1},
|
||||||
|
{"0.0.9", "0.1.2", -1},
|
||||||
|
{"1.1.2", "0.1.2", 1},
|
||||||
|
{"0.1.2", "1.1.2", -1},
|
||||||
|
{"0.1.10", "0.1.9", 1},
|
||||||
|
{"0.10.0", "0.2.0", 1},
|
||||||
|
{"30.10.0", "4.9.0", 1},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCompareVersions(t *testing.T) {
|
||||||
|
for _, tc := range testcases {
|
||||||
|
if r := compareVersions(tc.a, tc.b); r != tc.r {
|
||||||
|
t.Errorf("compareVersions(%q, %q): %d != %d", tc.a, tc.b, r, tc.r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,9 +0,0 @@
|
|||||||
// +build windows
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
func upgrade() error {
|
|
||||||
return errors.New("Upgrade currently unsupported on Windows")
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user