github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/repo/config/version.go (about) 1 package config 2 3 import ( 4 "errors" 5 "strconv" 6 "strings" 7 "time" 8 ) 9 10 // CurrentVersionNumber is the current application's version literal 11 const CurrentVersionNumber = "0.3.8-dev" 12 13 // Version regulates checking if the most recent version is run 14 type Version struct { 15 // Current is the ipfs version for which config was generated 16 Current string 17 18 // Check signals how to react on updates: 19 // - "ignore" for not checking 20 // - "warn" for issuing a warning and proceeding 21 // - "error" for exiting with an error 22 Check string 23 24 // CheckDate is a timestamp for the last time API endpoint was checked for updates 25 CheckDate time.Time 26 27 // CheckPeriod is the time duration over which the update check will not be performed 28 // (Note: cannot use time.Duration because marshalling with json breaks it) 29 CheckPeriod string 30 31 // AutoUpdate is optional 32 AutoUpdate AutoUpdateSetting 33 } 34 35 // supported Version.Check values 36 const ( 37 // CheckError value for Version.Check to raise error and exit if version is obsolete 38 CheckError = "error" 39 40 // CheckWarn value for Version.Check to show warning message if version is obsolete 41 CheckWarn = "warn" 42 43 // CheckIgnore value for Version.Check to not perform update check 44 CheckIgnore = "ignore" 45 ) 46 47 // AutoUpdateSetting implements json.Unmarshaler to check values in config 48 type AutoUpdateSetting int 49 50 // AutoUpdateSetting values 51 const ( 52 AutoUpdateNever AutoUpdateSetting = iota // do not auto-update 53 AutoUpdatePatch // only on new patch versions 54 AutoUpdateMinor // on new minor or patch versions (Default) 55 AutoUpdateMajor // on all, even Major, version changes 56 ) 57 58 // ErrUnknownAutoUpdateSetting is returned when an unknown value is read from the config 59 var ErrUnknownAutoUpdateSetting = errors.New("unknown value for AutoUpdate") 60 61 // defaultCheckPeriod governs h 62 var defaultCheckPeriod = time.Hour * 48 63 64 // UnmarshalJSON checks the input against known strings 65 func (s *AutoUpdateSetting) UnmarshalJSON(in []byte) error { 66 67 switch strings.ToLower(string(in)) { 68 case `"never"`: 69 *s = AutoUpdateNever 70 case `"major"`: 71 *s = AutoUpdateMajor 72 case `"minor"`: 73 *s = AutoUpdateMinor 74 case `"patch"`: 75 *s = AutoUpdatePatch 76 default: 77 *s = AutoUpdateMinor 78 return ErrUnknownAutoUpdateSetting 79 } 80 return nil 81 } 82 83 // MarshalJSON converts the value back to JSON string 84 func (s AutoUpdateSetting) MarshalJSON() ([]byte, error) { 85 return []byte(`"` + s.String() + `"`), nil 86 } 87 88 // String converts valye to human readable string 89 func (s AutoUpdateSetting) String() string { 90 switch s { 91 case AutoUpdateNever: 92 return "never" 93 case AutoUpdateMajor: 94 return "major" 95 case AutoUpdateMinor: 96 return "minor" 97 case AutoUpdatePatch: 98 return "patch" 99 default: 100 return ErrUnknownAutoUpdateSetting.Error() 101 } 102 } 103 104 func (v *Version) checkPeriodDuration() time.Duration { 105 d, err := strconv.Atoi(v.CheckPeriod) 106 if err != nil { 107 log.Warning("config.Version.CheckPeriod parse error. Using default.") 108 return defaultCheckPeriod 109 } 110 return time.Duration(d) 111 } 112 113 // ShouldCheckForUpdate returns if update check API endpoint is needed for this specific runtime 114 func (v *Version) ShouldCheckForUpdate() bool { 115 116 period := v.checkPeriodDuration() 117 if v.Check == CheckIgnore || v.CheckDate.Add(period).After(time.Now()) { 118 return false 119 } 120 return true 121 } 122 123 // VersionDefaultValue returns the default version config value (for init). 124 func VersionDefaultValue() Version { 125 return Version{ 126 Current: CurrentVersionNumber, 127 Check: "error", 128 CheckPeriod: strconv.Itoa(int(defaultCheckPeriod)), 129 AutoUpdate: AutoUpdateMinor, 130 } 131 }