// Handshake1Compatible checks whether two versions are compatible // returns nil if they are fine func Handshake1Compatible(handshakeA, handshakeB *pb.Handshake1) error { a, err := semver.NewVersion(*handshakeA.ProtocolVersion) if err != nil { return err } b, err := semver.NewVersion(*handshakeB.ProtocolVersion) if err != nil { return err } if a.Major != b.Major { return ErrVersionMismatch } return nil }
func init() { var err error IpfsVersion, err = semver.NewVersion("0.0.1") if err != nil { panic(fmt.Errorf("invalid protocol version: %v", err)) } }
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 }
// 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.Errorf("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) }