@cryptotaxi247 / kubo / commits / 7652f86c6

fixed a parsing bug on Version.CheckPeriod

And refactored a few names/comments

Juan Batiz-Benet committed Oct 9, 2014 at 20:31 UTC 7652f86c6f33fb3d57f07f1f5acfed44ab8453a0
3 files changed +58 -21
cmd/ipfs/ipfs.go
+5 -3
@@ -120,16 +120,18 @@ func localNode(confdir string, online bool) (*core.IpfsNode, error) {
120 return nil, err
121 }
122
123 - if cfg.Version.EligibleForUpdateCheck() {
123 + if cfg.Version.ShouldCheckForUpdate() {
124 obsolete := updates.CheckForUpdates()
125 if obsolete != nil {
126 if cfg.Version.Check == config.CheckError {
127 return nil, obsolete
128 }
129 - log.Warning(fmt.Sprintf("%v", obsolete)) // when "warn" version.check mode we just show warning message
129 +
130 + // when "warn" version.check mode we just show warning message
131 + log.Warning(fmt.Sprintf("%v", obsolete))
132 } else {
133 // update most recent check timestamp in config
132 - cfg.RecordCurrentUpdateCheck(filename)
134 + config.RecordUpdateCheck(cfg, filename)
135 }
136 }
137
config/config.go
+2
@@ -11,6 +11,8 @@ import (
11 u "github.com/jbenet/go-ipfs/util"
12 )
13
14 +var log = u.Logger("config")
15 +
16 // Identity tracks the configuration of the local node's identity.
17 type Identity struct {
18 PeerID string
config/version.go
+51 -18
@@ -1,38 +1,71 @@
1 package config
2
3 -import "time"
3 +import (
4 + "strconv"
5 + "time"
6 +)
7
8 // Version regulates checking if the most recent version is run
9 type Version struct {
7 - Check string // "ignore" for do not check, "warn" and "error" for reacting when obsolete
8 - Current string // ipfs version for which config was generated
9 - UpdateCheckedTime time.Time // timestamp for the last time API endpoint was checked for updates
10 - UpdateCheckPeriod time.Duration // time duration over which the update check will not be performed
10 + // Current is the ipfs version for which config was generated
11 + Current string
12 +
13 + // Check signals how to react on updates:
14 + // - "ignore" for not checking
15 + // - "warn" for issuing a warning and proceeding
16 + // - "error" for exiting with an error
17 + Check string
18 +
19 + // CheckDate is a timestamp for the last time API endpoint was checked for updates
20 + CheckDate time.Time
21 +
22 + // CheckPeriod is the time duration over which the update check will not be performed
23 + // (Note: cannot use time.Duration because marshalling with json breaks it)
24 + CheckPeriod string
25 }
26
27 // supported Version.Check values
28 const (
15 - CheckError = "error" // value for Version.Check to raise error and exit if version is obsolete
16 - CheckWarn = "warn" // value for Version.Check to show warning message if version is obsolete
17 - CheckIgnore = "ignore" // value for Version.Check to not perform update check
29 + // CheckError value for Version.Check to raise error and exit if version is obsolete
30 + CheckError = "error"
31 +
32 + // CheckWarn value for Version.Check to show warning message if version is obsolete
33 + CheckWarn = "warn"
34 +
35 + // CheckIgnore value for Version.Check to not perform update check
36 + CheckIgnore = "ignore"
37 )
38
20 -var defaultUpdateCheckPeriod = time.Hour * 48
39 +// defaultCheckPeriod governs h
40 +var defaultCheckPeriod = time.Hour * 48
41
22 -// EligibleForUpdateCheck returns if update check API endpoint is needed for this specific runtime
23 -func (v *Version) EligibleForUpdateCheck() bool {
24 - if v.Check == CheckIgnore || v.UpdateCheckedTime.Add(v.UpdateCheckPeriod).After(time.Now()) {
42 +func (v *Version) checkPeriodDuration() time.Duration {
43 + d, err := strconv.Atoi(v.CheckPeriod)
44 + if err != nil {
45 + log.Error("config.Version.CheckPeriod parse error. Using default.")
46 + return defaultCheckPeriod
47 + }
48 + return time.Duration(d)
49 +}
50 +
51 +// ShouldCheckForUpdate returns if update check API endpoint is needed for this specific runtime
52 +func (v *Version) ShouldCheckForUpdate() bool {
53 +
54 + period := v.checkPeriodDuration()
55 + if v.Check == CheckIgnore || v.CheckDate.Add(period).After(time.Now()) {
56 return false
57 }
58 return true
59 }
60
30 -// RecordCurrentUpdateCheck is called to record that update check was performed and showed that the running version is the most recent one
31 -func (cfg *Config) RecordCurrentUpdateCheck(filename string) {
32 - cfg.Version.UpdateCheckedTime = time.Now()
33 - if cfg.Version.UpdateCheckPeriod == time.Duration(0) {
34 - // UpdateCheckPeriod was not initialized for some reason (e.g. config file used is broken)
35 - cfg.Version.UpdateCheckPeriod = defaultUpdateCheckPeriod
61 +// RecordUpdateCheck is called to record that an update check was performed,
62 +// showing that the running version is the most recent one.
63 +func RecordUpdateCheck(cfg *Config, filename string) {
64 + cfg.Version.CheckDate = time.Now()
65 +
66 + if cfg.Version.CheckPeriod == "" {
67 + // CheckPeriod was not initialized for some reason (e.g. config file broken)
68 + cfg.Version.CheckPeriod = strconv.Itoa(int(defaultCheckPeriod))
69 }
70
71 WriteConfigFile(filename, cfg)