@cryptotaxi247 / kubo / commits / 0a0c38fb9

using @jbenet's industrial lipstick to clean this up

Henry committed Oct 20, 2014 at 20:01 UTC 0a0c38fb91d25ac2747ef9c337c525257738dd07
2 files changed +77 -33
cmd/ipfs/ipfs.go
+2 -33
@@ -5,10 +5,8 @@ import (
5 "fmt"
6 "os"
7 "runtime/pprof"
8 - "time"
8
9 flag "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
11 - check "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/inconshreveable/go-update/check"
10 commander "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
11 ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
12
@@ -128,37 +126,8 @@ func localNode(confdir string, online bool) (*core.IpfsNode, error) {
126 return nil, err
127 }
128
131 - if cfg.Version.ShouldCheckForUpdate() {
132 - log.Info("checking for update")
133 - u, err := updates.CheckForUpdate()
134 - if err != nil {
135 - if err != check.NoUpdateAvailable {
136 - if cfg.Version.Check == config.CheckError {
137 - log.Error("Error while checking for update: %v\n", err)
138 - return nil, err
139 - }
140 - // when "warn" version.check mode we just show a warning message
141 - log.Warning(err.Error())
142 - } else { // err == check.NoUpdateAvailable
143 - log.Notice("No update available, checked on %s", time.Now())
144 - config.RecordUpdateCheck(cfg, filename)
145 - }
146 - } else { // update avail
147 - if cfg.Version.AutoUpdate != config.UpdateNever {
148 - if updates.ShouldAutoUpdate(cfg.Version.AutoUpdate, u.Version) {
149 - log.Notice("Applying update %s", u.Version)
150 -
151 - if err = updates.Apply(u); err != nil {
152 - log.Error(err.Error())
153 - return nil, err
154 - }
155 -
156 - // BUG(cryptix): no good way to restart yet. - tracking https://github.com/inconshreveable/go-update/issues/5
157 - fmt.Println("update %v applied. please restart.", u.Version)
158 - os.Exit(0)
159 - }
160 - }
161 - }
129 + if err := updates.CliCheckForUpdates(cfg, filename); err != nil {
130 + return nil, err
131 }
132
133 return core.NewIpfsNode(cfg, online)
updates/updates.go
+75
@@ -3,6 +3,7 @@ package updates
3 import (
4 "fmt"
5 "os"
6 + "time"
7
8 "github.com/jbenet/go-ipfs/config"
9 u "github.com/jbenet/go-ipfs/util"
@@ -122,3 +123,77 @@ func ShouldAutoUpdate(setting config.AutoUpdateSetting, newVer string) bool {
123
124 return false
125 }
126 +
127 +func CliCheckForUpdates(cfg *config.Config, confFile string) error {
128 +
129 + // if config says not to, don't check for updates
130 + if !cfg.Version.ShouldCheckForUpdate() {
131 + log.Info("update checking disabled.")
132 + return nil
133 + }
134 +
135 + log.Info("checking for update")
136 + u, err := CheckForUpdate()
137 + // if there is no update available, record it, and exit.
138 + if err == check.NoUpdateAvailable {
139 + log.Notice("No update available, checked on %s", time.Now())
140 + config.RecordUpdateCheck(cfg, confFile) // only record if we checked successfully.
141 + return nil
142 + }
143 +
144 + // if another, unexpected error occurred, note it.
145 + if err != nil {
146 + if cfg.Version.Check == config.CheckError {
147 + log.Error("Error while checking for update: %v\n", err)
148 + return nil
149 + }
150 + // when "warn" version.check mode we just show a warning message
151 + log.Warning(err.Error())
152 + return nil
153 + }
154 +
155 + // there is an update available
156 +
157 + // if we autoupdate
158 + if cfg.Version.AutoUpdate != config.UpdateNever {
159 + // and we should auto update
160 + if ShouldAutoUpdate(cfg.Version.AutoUpdate, u.Version) {
161 + log.Notice("Applying update %s", u.Version)
162 +
163 + if err = Apply(u); err != nil {
164 + log.Error(err.Error())
165 + return nil
166 + }
167 +
168 + // BUG(cryptix): no good way to restart yet. - tracking https://github.com/inconshreveable/go-update/issues/5
169 + fmt.Println("update %v applied. please restart.", u.Version)
170 + os.Exit(0)
171 + }
172 + }
173 +
174 + // autoupdate did not exit, so regular notices.
175 + switch cfg.Version.Check {
176 + case config.CheckError:
177 + return fmt.Errorf(errShouldUpdate, Version, u.Version)
178 + case config.CheckWarn:
179 + // print the warning
180 + fmt.Printf("New version available: %s", u.Version)
181 + default: // ignore
182 + }
183 + return nil
184 +}
185 +
186 +var errShouldUpdate = `
187 +Your go-ipfs version is: %s
188 +There is a new version available: %s
189 +Since this is alpha software, it is strongly recommended you update.
190 +
191 +To update, run:
192 +
193 + ipfs update apply
194 +
195 +To disable this notice, run:
196 +
197 + ipfs config Version.Check warn
198 +
199 +`