@cryptotaxi247 / kubo / commits / 98ba0be06

Move versioning/updates stuff to a separate package

Peter Borzov committed Oct 7, 2014 at 10:03 UTC 98ba0be06025743d471cae5f6af757ef97bc6705
4 files changed +86 -77
cmd/ipfs/init.go
+2 -1
@@ -11,6 +11,7 @@ import (
11 config "github.com/jbenet/go-ipfs/config"
12 ci "github.com/jbenet/go-ipfs/crypto"
13 spipe "github.com/jbenet/go-ipfs/crypto/spipe"
14 + updates "github.com/jbenet/go-ipfs/updates"
15 u "github.com/jbenet/go-ipfs/util"
16 )
17
@@ -138,7 +139,7 @@ func initCmd(c *commander.Command, inp []string) error {
139 // tracking ipfs version used to generate the init folder and adding update checker default setting.
140 cfg.Version = config.Version{
141 Check: "error",
141 - Current: currentVersion.String(),
142 + Current: updates.Version,
143 }
144
145 err = config.WriteConfigFile(filename, cfg)
cmd/ipfs/ipfs.go
+2 -1
@@ -13,6 +13,7 @@ import (
13 config "github.com/jbenet/go-ipfs/config"
14 core "github.com/jbenet/go-ipfs/core"
15 daemon "github.com/jbenet/go-ipfs/daemon"
16 + updates "github.com/jbenet/go-ipfs/updates"
17 u "github.com/jbenet/go-ipfs/util"
18 )
19
@@ -120,7 +121,7 @@ func localNode(confdir string, online bool) (*core.IpfsNode, error) {
121 }
122
123 if cfg.Version.Check != config.CheckIgnore {
123 - obsolete := checkForUpdates()
124 + obsolete := updates.CheckForUpdates()
125 if obsolete != nil {
126 if cfg.Version.Check == config.CheckError {
127 return nil, obsolete
cmd/ipfs/version.go
+2 -75
@@ -1,36 +1,11 @@
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"
4 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
5 + updates "github.com/jbenet/go-ipfs/updates"
6 u "github.com/jbenet/go-ipfs/util"
7 )
8
14 -// The IPFS version.
15 -const (
16 - Version = "0.1.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 -
9 var cmdIpfsVersion = &commander.Command{
10 UsageLine: "version",
11 Short: "Show ipfs version information.",
@@ -41,15 +16,7 @@ var cmdIpfsVersion = &commander.Command{
16 Run: versionCmd,
17 }
18
44 -var currentVersion *semver.Version
45 -
19 func init() {
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 - }
20 cmdIpfsVersion.Flag.Bool("number", false, "show only the number")
21 }
22
@@ -58,46 +25,6 @@ func versionCmd(c *commander.Command, _ []string) error {
25 if !number {
26 u.POut("ipfs version ")
27 }
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 - }
28 + u.POut("%s\n", updates.Version)
29 return nil
30 }
updates/updates.go new
+80
@@ -0,0 +1,80 @@
1 +package updates
2 +
3 +import (
4 + "encoding/json"
5 + "fmt"
6 + "net/http"
7 + "os"
8 +
9 + "github.com/coreos/go-semver/semver"
10 +)
11 +
12 +const (
13 + Version = "0.1.0" // actual current application's version literal
14 + EndpointURLLatestReleases = "https://api.github.com/repos/jbenet/go-ipfs/tags"
15 + VersionErrorShort = `Warning: You are running version %s of go-ipfs. The latest version is %s.`
16 + VersionErrorLong = `
17 + Warning: You are running version %s of go-ipfs. The latest version is %s.
18 + Since this is alpha software, it is strongly recommended you update.
19 +
20 + You can update go-ipfs by running
21 +
22 + ipfs version update
23 +
24 + You can silence this message by running
25 +
26 + ipfs config update.check ignore
27 +
28 + `
29 +)
30 +
31 +var currentVersion *semver.Version
32 +
33 +func init() {
34 + var err error
35 + currentVersion, err = semver.NewVersion(Version)
36 + if err != nil {
37 + fmt.Printf("The const Version literal in version.go needs to be in semver format: %s \n", Version)
38 + os.Exit(1)
39 + }
40 +}
41 +
42 +func CheckForUpdates() error {
43 + resp, err := http.Get(EndpointURLLatestReleases)
44 + if err != nil {
45 + // can't reach the endpoint, coud be firewall, or no internet connection or something else
46 + // will just silently move on
47 + return nil
48 + }
49 + var body interface{}
50 + _ = json.NewDecoder(resp.Body).Decode(&body)
51 + releases, ok := body.([]interface{})
52 + if !ok {
53 + // the response body does not seem to meet specified Github API format
54 + // https://developer.github.com/v3/repos/#list-tags
55 + // will just silently move on
56 + return nil
57 + }
58 + for _, r := range releases {
59 + release, ok := r.(map[string]interface{})
60 + if !ok {
61 + continue
62 + }
63 + tagName, ok := release["name"].(string)
64 + if !ok {
65 + continue
66 + }
67 + if len(tagName) > 0 && tagName[0] == 'v' {
68 + // both 'v0.1.0' and '0.1.0' semver tagname conventions can be encountered
69 + tagName = tagName[1:]
70 + }
71 + releaseVersion, err := semver.NewVersion(tagName)
72 + if err != nil {
73 + continue
74 + }
75 + if currentVersion.LessThan(*releaseVersion) {
76 + return fmt.Errorf(VersionErrorLong, Version, tagName)
77 + }
78 + }
79 + return nil
80 +}