@cryptotaxi247 / kubo / commits / 2806260f5

use inconshreveable's go-update to check for updates

Henry committed Oct 18, 2014 at 14:33 UTC 2806260f5c54b5c61730a592ac58d6d977c5850f
2 files changed +23 -66
cmd/ipfs/ipfs.go
+10 -10
@@ -7,6 +7,7 @@ import (
7 "runtime/pprof"
8
9 flag "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
10 + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/inconshreveable/go-update/check"
11 commander "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
12 ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
13
@@ -126,18 +127,17 @@ func localNode(confdir string, online bool) (*core.IpfsNode, error) {
127 }
128
129 if cfg.Version.ShouldCheckForUpdate() {
129 - obsolete := updates.CheckForUpdates()
130 - if obsolete != nil {
131 - if cfg.Version.Check == config.CheckError {
132 - return nil, obsolete
133 - }
130 + _, err := updates.CheckForUpdate()
131 + if err != nil {
132 + if err != check.NoUpdateAvailable {
133 + log.Error("Error while checking for update: %v\n", err)
134 + return nil, err
135
135 - // when "warn" version.check mode we just show warning message
136 - log.Warning(fmt.Sprintf("%v", obsolete))
137 - } else {
138 - // update most recent check timestamp in config
139 - config.RecordUpdateCheck(cfg, filename)
136 + }
137 + log.Notice("No update available")
138 }
139 +
140 + config.RecordUpdateCheck(cfg, filename)
141 }
142
143 return core.NewIpfsNode(cfg, online)
updates/updates.go
+13 -56
@@ -1,32 +1,18 @@
1 package updates
2
3 import (
4 - "encoding/json"
5 - "fmt"
6 - "net/http"
4 "os"
5
6 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
7 + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/inconshreveable/go-update"
8 + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/inconshreveable/go-update/check"
9 u "github.com/jbenet/go-ipfs/util"
10 )
11
12 const (
14 - Version = "0.1.0" // actual current application's version literal
15 - EndpointURLLatestReleases = "https://api.github.com/repos/jbenet/go-ipfs/tags"
16 - VersionErrorShort = `Warning: You are running version %s of go-ipfs. The latest version is %s.`
17 - VersionErrorLong = `
18 - Warning: You are running version %s of go-ipfs. The latest version is %s.
19 - Since this is alpha software, it is strongly recommended you update.
20 -
21 - You can update go-ipfs by running
22 -
23 - ipfs version update
24 -
25 - You can silence this message by running
26 -
27 - ipfs config update.check ignore
28 -
29 - `
13 + Version = "0.1.0" // actual current application's version literal
14 + UpdateEndpointURL = "https://api.equinox.io/1/Updates"
15 + UpdateAppID = "ap_ywkPmAR40q4EfdikN9Jh2hgIHi"
16 )
17
18 var log = u.Logger("updates")
@@ -45,42 +31,13 @@ func init() {
31 func parseVersion() (*semver.Version, error) {
32 return semver.NewVersion(Version)
33 }
48 -func CheckForUpdates() error {
49 - resp, err := http.Get(EndpointURLLatestReleases)
50 - if err != nil {
51 - // can't reach the endpoint, coud be firewall, or no internet connection or something else
52 - log.Error("update check: error connecting to API endpoint for newer versions: %v", err)
53 - return nil
54 - }
55 - var body interface{}
56 - _ = json.NewDecoder(resp.Body).Decode(&body)
57 - releases, ok := body.([]interface{})
58 - if !ok {
59 - // the response body does not seem to meet specified Github API format
60 - // https://developer.github.com/v3/repos/#list-tags
61 - log.Error("update check: API endpoint for newer versions does not seem to be in Github API specified format")
62 - return nil
63 - }
64 - for _, r := range releases {
65 - release, ok := r.(map[string]interface{})
66 - if !ok {
67 - continue
68 - }
69 - tagName, ok := release["name"].(string)
70 - if !ok {
71 - continue
72 - }
73 - if len(tagName) > 0 && tagName[0] == 'v' {
74 - // both 'v0.1.0' and '0.1.0' semver tagname conventions can be encountered
75 - tagName = tagName[1:]
76 - }
77 - releaseVersion, err := semver.NewVersion(tagName)
78 - if err != nil {
79 - continue
80 - }
81 - if currentVersion.LessThan(*releaseVersion) {
82 - return fmt.Errorf(VersionErrorLong, Version, tagName)
83 - }
34 +
35 +func CheckForUpdate() (*check.Result, error) {
36 + param := check.Params{
37 + AppVersion: Version,
38 + AppId: UpdateAppID,
39 + Channel: "stable",
40 }
85 - return nil
41 +
42 + return param.CheckForUpdate(UpdateEndpointURL, update.New())
43 }