func versionIsNewer(version string) (bool, error) { nv, err := semver.NewVersion(version) if err != nil { return false, fmt.Errorf("could not parse version string: %s", err) } cv := currentVersion newer := !nv.LessThan(*cv) && nv.String() != cv.String() return newer, nil }
// protocolVersionsAreCompatible checks that the two implementations // can talk to each other. It will use semver, but for now while // we're in tight development, we will return false for minor version // changes too. func protocolVersionsAreCompatible(v1, v2 string) bool { if strings.HasPrefix(v1, "ipfs/") { v1 = v1[5:] } if strings.HasPrefix(v2, "ipfs/") { v2 = v2[5:] } v1s, err := semver.NewVersion(v1) if err != nil { return false } v2s, err := semver.NewVersion(v2) if err != nil { return false } return v1s.Major == v2s.Major && v1s.Minor == v2s.Minor }
// ShouldAutoUpdate decides wether a new version should be applied // checks against config setting and new version string. returns false in case of error func ShouldAutoUpdate(setting config.AutoUpdateSetting, newVer string) bool { if setting == config.AutoUpdateNever { return false } nv, err := semver.NewVersion(newVer) if err != nil { log.Infof("could not parse version string: %s", err) return false } n := nv.Slice() c := currentVersion.Slice() switch setting { case config.AutoUpdatePatch: if n[0] < c[0] { return false } if n[1] < c[1] { return false } return n[2] > c[2] case config.AutoUpdateMinor: if n[0] != c[0] { return false } return n[1] > c[1] || (n[1] == c[1] && n[2] > c[2]) case config.AutoUpdateMajor: for i := 0; i < 3; i++ { if n[i] < c[i] { return false } } return true } return false }
func TestShouldAutoUpdate(t *testing.T) { tests := []struct { setting config.AutoUpdateSetting currV, newV string should bool }{ {config.AutoUpdateNever, "0.0.1", "1.0.0", false}, {config.AutoUpdateNever, "0.0.1", "0.1.0", false}, {config.AutoUpdateNever, "0.0.1", "0.0.1", false}, {config.AutoUpdateNever, "0.0.1", "0.0.2", false}, {config.AutoUpdatePatch, "0.0.1", "1.0.0", false}, {config.AutoUpdatePatch, "0.0.1", "0.1.0", false}, {config.AutoUpdatePatch, "0.0.1", "0.0.1", false}, {config.AutoUpdatePatch, "0.0.2", "0.0.1", false}, {config.AutoUpdatePatch, "0.0.1", "0.0.2", true}, {config.AutoUpdateMinor, "0.1.1", "1.0.0", false}, {config.AutoUpdateMinor, "0.1.1", "0.2.0", true}, {config.AutoUpdateMinor, "0.1.1", "0.1.2", true}, {config.AutoUpdateMinor, "0.2.1", "0.1.9", false}, {config.AutoUpdateMinor, "0.1.2", "0.1.1", false}, {config.AutoUpdateMajor, "1.0.0", "2.0.0", true}, {config.AutoUpdateMajor, "1.0.0", "1.1.0", true}, {config.AutoUpdateMajor, "1.0.0", "1.0.1", true}, {config.AutoUpdateMajor, "2.0.0", "1.0.0", false}, // don't downgrade {config.AutoUpdateMajor, "2.5.0", "2.4.0", false}, {config.AutoUpdateMajor, "2.0.2", "2.0.1", false}, } for i, tc := range tests { var err error currentVersion, err = semver.NewVersion(tc.currV) if err != nil { t.Fatalf("Could not parse test version: %v", err) } if tc.should != ShouldAutoUpdate(tc.setting, tc.newV) { t.Fatalf("#%d failed for %+v", i, tc) } } }
func parseVersion() (*semver.Version, error) { return semver.NewVersion(Version) }