Check for updates:if github releases with higher tag semvers exist
Peter Borzov committed
Oct 6, 2014 at 17:10 UTC
f6650b02890e82d36164cbde550de0bc5f8056c6
1 file changed
+69
-2
cmd/ipfs/version.go
+69
-2
@@ -1,13 +1,35 @@
1
package main
2
3
import (
4
+ "encoding/json"
5
+ "fmt"
6
+ "net/http"
7
+ "os"
8
+
9
semver "github.com/coreos/go-semver/semver"
10
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
11
u "github.com/jbenet/go-ipfs/util"
12
)
13
14
// The IPFS version.
10
-const Version = "0.1.0"
15
+const (
16
+ Version = "0.2.0"
17
+ EndpointURLLatestReleases = "https://api.github.com/repos/jbenet/go-ipfs/tags"
18
+ VersionErrorShort = `Warning: You are running version %s of go-ipfs. The latest version is %s.`
19
+ VersionErrorLong = `
20
+ Warning: You are running version %s of go-ipfs. The latest version is %s.
21
+ Since this is alpha software, it is strongly recommended you update.
22
+
23
+ You can update go-ipfs by running
24
+
25
+ ipfs version update
26
+
27
+ You can silence this message by running
28
+
29
+ ipfs config update.check ignore
30
+
31
+ `
32
+)
33
34
var cmdIpfsVersion = &commander.Command{
35
UsageLine: "version",
@@ -22,7 +44,12 @@ var cmdIpfsVersion = &commander.Command{
44
var currentVersion *semver.Version
45
46
func init() {
25
- currentVersion, _ = semver.NewVersion(Version)
47
+ var err error
48
+ currentVersion, err = semver.NewVersion(Version)
49
+ if err != nil {
50
+ fmt.Printf("The const Version literal in version.go needs to be in semver format: %s \n", Version)
51
+ os.Exit(1)
52
+ }
53
cmdIpfsVersion.Flag.Bool("number", false, "show only the number")
54
}
55
@@ -34,3 +61,43 @@ func versionCmd(c *commander.Command, _ []string) error {
61
u.POut("%s\n", Version)
62
return nil
63
}
64
+
65
+func checkForUpdates() error {
66
+ resp, err := http.Get(EndpointURLLatestReleases)
67
+ if err != nil {
68
+ // can't reach the endpoint, coud be firewall, or no internet connection or something else
69
+ // will just silently move on
70
+ return nil
71
+ }
72
+ var body interface{}
73
+ _ = json.NewDecoder(resp.Body).Decode(&body)
74
+ releases, ok := body.([]interface{})
75
+ if !ok {
76
+ // the response body does not seem to meet specified Github API format
77
+ // https://developer.github.com/v3/repos/#list-tags
78
+ // will just silently move on
79
+ return nil
80
+ }
81
+ for _, r := range releases {
82
+ release, ok := r.(map[string]interface{})
83
+ if !ok {
84
+ continue
85
+ }
86
+ tagName, ok := release["name"].(string)
87
+ if !ok {
88
+ continue
89
+ }
90
+ if len(tagName) > 0 && tagName[0] == 'v' {
91
+ // both 'v0.1.0' and '0.1.0' semver tagname conventions can be encountered
92
+ tagName = tagName[1:]
93
+ }
94
+ releaseVersion, err := semver.NewVersion(tagName)
95
+ if err != nil {
96
+ continue
97
+ }
98
+ if currentVersion.LessThan(*releaseVersion) {
99
+ return fmt.Errorf(VersionErrorLong, Version, tagName)
100
+ }
101
+ }
102
+ return nil
103
+}