updates: dont count dev versions as older
- if equinox says there is a new update, but the version number IS NOT larger, we interpret that as no update (you may have gotten a newer version by building it yourself). - Also export ErrNoUpdateAvailable so clients don't also need to import the equinox library to check the error. cc @cryptix comments?
Juan Batiz-Benet committed
Oct 26, 2014 at 01:38 UTC
ef6d7350191ac785b5ef3774ce556e8f574cdff1
1 file changed
+33
-8
updates/updates.go
+33
-8
@@ -81,6 +81,9 @@ var log = u.Logger("updates")
81
82
var currentVersion *semver.Version
83
84
+// ErrNoUpdateAvailable returned when a check fails to find a newer update.
85
+var ErrNoUpdateAvailable = check.NoUpdateAvailable
86
+
87
func init() {
88
var err error
89
currentVersion, err = parseVersion()
@@ -96,6 +99,9 @@ func parseVersion() (*semver.Version, error) {
99
}
100
101
// CheckForUpdate checks the equinox.io api if there is an update available
102
+// NOTE: if equinox says there is a new update, but the version number IS NOT
103
+// larger, we interpret that as no update (you may have gotten a newer version
104
+// by building it yourself).
105
func CheckForUpdate() (*check.Result, error) {
106
param := check.Params{
107
AppVersion: Version,
@@ -108,7 +114,19 @@ func CheckForUpdate() (*check.Result, error) {
114
return nil, fmt.Errorf("Failed to parse public key: %v", err)
115
}
116
111
- return param.CheckForUpdate(updateEndpointURL, up)
117
+ res, err := param.CheckForUpdate(updateEndpointURL, up)
118
+ if err != nil {
119
+ return res, err
120
+ }
121
+
122
+ newer, err := versionIsNewer(res.Version)
123
+ if err != nil {
124
+ return nil, err
125
+ }
126
+ if !newer {
127
+ return nil, ErrNoUpdateAvailable
128
+ }
129
+ return res, err
130
}
131
132
// Apply cheks if the running process is able to update itself
@@ -177,6 +195,7 @@ func ShouldAutoUpdate(setting config.AutoUpdateSetting, newVer string) bool {
195
return false
196
}
197
198
+// CliCheckForUpdates is the automatic update check from the commandline.
199
func CliCheckForUpdates(cfg *config.Config, confFile string) error {
200
201
// if config says not to, don't check for updates
@@ -188,7 +207,7 @@ func CliCheckForUpdates(cfg *config.Config, confFile string) error {
207
log.Info("checking for update")
208
u, err := CheckForUpdate()
209
// if there is no update available, record it, and exit.
191
- if err == check.NoUpdateAvailable {
210
+ if err == ErrNoUpdateAvailable {
211
log.Noticef("No update available, checked on %s", time.Now())
212
config.RecordUpdateCheck(cfg, confFile) // only record if we checked successfully.
213
return nil
@@ -196,12 +215,7 @@ func CliCheckForUpdates(cfg *config.Config, confFile string) error {
215
216
// if another, unexpected error occurred, note it.
217
if err != nil {
199
- if cfg.Version.Check == config.CheckError {
200
- log.Errorf("Error while checking for update: %v", err)
201
- return nil
202
- }
203
- // when "warn" version.check mode we just show a warning message
204
- log.Warning(err.Error())
218
+ log.Errorf("Error while checking for update: %v", err)
219
return nil
220
}
221
@@ -236,6 +250,17 @@ func CliCheckForUpdates(cfg *config.Config, confFile string) error {
250
return nil
251
}
252
253
+func versionIsNewer(version string) (bool, error) {
254
+ nv, err := semver.NewVersion(version)
255
+ if err != nil {
256
+ return false, fmt.Errorf("could not parse version string: %s", err)
257
+ }
258
+
259
+ cv := currentVersion
260
+ newer := !nv.LessThan(*cv) && nv.String() != cv.String()
261
+ return newer, nil
262
+}
263
+
264
var errShouldUpdate = `
265
Your go-ipfs version is: %s
266
There is a new version available: %s