CheckForUpdate checks whether an update exists for the GitHub CLI based on recency of last check within past 24 hours.
(ctx context.Context, client *http.Client, stateFilePath, repo, currentVersion string)
| 89 | |
| 90 | // CheckForUpdate checks whether an update exists for the GitHub CLI based on recency of last check within past 24 hours. |
| 91 | func CheckForUpdate(ctx context.Context, client *http.Client, stateFilePath, repo, currentVersion string) (*ReleaseInfo, error) { |
| 92 | stateEntry, _ := getStateEntry(stateFilePath) |
| 93 | if stateEntry != nil && time.Since(stateEntry.CheckedForUpdateAt).Hours() < 24 { |
| 94 | return nil, nil |
| 95 | } |
| 96 | |
| 97 | releaseInfo, err := getLatestReleaseInfo(ctx, client, repo) |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | |
| 102 | err = setStateEntry(stateFilePath, time.Now(), *releaseInfo) |
| 103 | if err != nil { |
| 104 | return nil, err |
| 105 | } |
| 106 | |
| 107 | if versionGreaterThan(releaseInfo.Version, currentVersion) { |
| 108 | return releaseInfo, nil |
| 109 | } |
| 110 | |
| 111 | return nil, nil |
| 112 | } |
| 113 | |
| 114 | func getLatestReleaseInfo(ctx context.Context, client *http.Client, repo string) (*ReleaseInfo, error) { |
| 115 | req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", repo), nil) |