CheckUpdate checks whether there is a version update available. If there is no update, or no new version is available, this function returns nil. The function uses cache.
(ctx context.Context, opts ...CheckOption)
| 210 | // If there is no update, or no new version is available, this function returns nil. |
| 211 | // The function uses cache. |
| 212 | func CheckUpdate(ctx context.Context, opts ...CheckOption) (*Update, error) { |
| 213 | current, err := semver.Parse(strings.TrimPrefix(TTN, "v")) |
| 214 | if err != nil { |
| 215 | return nil, err |
| 216 | } |
| 217 | if c, err := cachedCheck(); err == nil { |
| 218 | if c.Timestamp.Add(versionCheckCacheTTL).After(time.Now()) && c.Result.Current.Compare(current) == 0 { |
| 219 | return c.Result, nil |
| 220 | } |
| 221 | } |
| 222 | o := &checkOptions{ |
| 223 | client: http.DefaultClient, |
| 224 | current: current, |
| 225 | sourceURL: TTSVersionCheckURL, |
| 226 | docsURL: TTSUpgradingDocsURL, |
| 227 | } |
| 228 | for _, opt := range opts { |
| 229 | opt.apply(o) |
| 230 | } |
| 231 | latest, err := latest(ctx, o.client, o.sourceURL) |
| 232 | if err != nil { |
| 233 | return nil, err |
| 234 | } |
| 235 | var minor, patch bool |
| 236 | switch { |
| 237 | case o.current.Minor < latest.Minor: |
| 238 | minor = true |
| 239 | case o.current.Minor > latest.Minor: |
| 240 | return nil, nil |
| 241 | case o.current.Patch < latest.Patch: |
| 242 | patch = true |
| 243 | default: |
| 244 | return nil, nil |
| 245 | } |
| 246 | update := &Update{ |
| 247 | Current: o.current, |
| 248 | Latest: latest, |
| 249 | DocsURL: o.docsURL, |
| 250 | Minor: minor, |
| 251 | Patch: patch, |
| 252 | } |
| 253 | if err = saveCheck(update); err != nil { |
| 254 | log.FromContext(ctx).WithError(err).Warn("Failed to write version check cache") |
| 255 | } |
| 256 | return update, nil |
| 257 | } |
| 258 | |
| 259 | // LogUpdate logs an Warn level message when there is a newer minor, and a Log level message when there is a newer patch. |
| 260 | // When there is no new minor or patch version, this function does nothing. |