Skip to content

Commit 10b259a

Browse files
committed
minor updates to the update checker
1 parent f92b3f4 commit 10b259a

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

lib/app/app.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,11 @@ func (a *Application) createNewConfigFile() {
307307
}
308308

309309
func (a *Application) checkForApplicationUpdates() {
310-
// if a.Gatekeeper.CanAccessUrl(updater.GetUpdateCheckUrlFormat()) {
311-
updateAvailable := updater.IsLatestApplicationReleaseNewerThanCurrent(version.APP_VERSION, "permafrost-dev/stackup")
310+
updateAvailable, release := updater.IsLatestApplicationReleaseNewerThanCurrent(a.Workflow.Cache, version.APP_VERSION, "permafrost-dev/stackup")
312311

313312
if updateAvailable {
314-
support.WarningMessage("A new version of StackUp is available. Please update to the latest version.")
313+
support.WarningMessage(fmt.Sprintf("A new version of StackUp is available, released %s.", release.TimeSinceRelease))
315314
}
316-
// }
317315
}
318316

319317
func (a *Application) handleFlagOptions() {

lib/updater/updater.go

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,57 @@ import (
77
"regexp"
88
"strconv"
99
"strings"
10+
"time"
11+
12+
"github.com/golang-module/carbon/v2"
13+
"github.com/stackup-app/stackup/lib/cache"
1014
)
1115

1216
type Release struct {
13-
Name string `json:"name"`
14-
TagName string `json:"tag_name"`
15-
Prerelease bool `json:"prerelease"`
17+
Name string `json:"name"`
18+
TagName string `json:"tag_name"`
19+
Prerelease bool `json:"prerelease"`
20+
PublishedAt string `json:"published_at"`
21+
22+
DaysSinceRelease int
23+
HoursSinceRelease int
24+
TimeSinceRelease string
1625
}
1726

1827
func GetUpdateCheckUrlFormat() string {
1928
return "https://api.github.com/repos/%s/%s/releases/latest"
2029
}
2130

2231
// Example: IsLatestApplicationReleaseNewerThanCurrent("v0.0.1", "permafrost-dev/stackup")
23-
func IsLatestApplicationReleaseNewerThanCurrent(currentVersion string, githubRepository string) bool {
32+
func IsLatestApplicationReleaseNewerThanCurrent(c *cache.Cache, currentVersion string, githubRepository string) (bool, *Release) {
33+
if c.Has("latest-release:"+githubRepository) && !c.IsExpired("latest-release:"+githubRepository) {
34+
var release = Release{}
35+
releaseJson, found := c.Get("latest-release:" + githubRepository)
36+
if found {
37+
json.Unmarshal([]byte(releaseJson.Value), &release)
38+
latest := extractSemver(release.TagName)
39+
current := extractSemver(currentVersion)
40+
41+
release.TimeSinceRelease = carbon.Parse(release.PublishedAt).DiffForHumans()
42+
return isGreaterSemver(latest, current), &release
43+
}
44+
}
45+
2446
latestRelease := getLatestApplicationRelease(githubRepository)
2547

48+
latestRelease.TimeSinceRelease = carbon.Parse(latestRelease.PublishedAt).DiffForHumans()
49+
50+
//cache the response
51+
latestReleaseJson, _ := json.Marshal(latestRelease)
52+
expires := carbon.Now().AddMinutes(60)
53+
updatedAt := carbon.Now()
54+
entry := cache.CreateCacheEntry("latest-release"+githubRepository, string(latestReleaseJson), &expires, "", "", &updatedAt)
55+
c.Set("latest-release:"+githubRepository, entry, 60)
56+
2657
latest := extractSemver(latestRelease.TagName)
2758
current := extractSemver(currentVersion)
2859

29-
return isGreaterSemver(latest, current)
60+
return isGreaterSemver(latest, current), latestRelease
3061
}
3162

3263
func getLatestApplicationRelease(repository string) *Release {
@@ -37,12 +68,22 @@ func getLatestApplicationRelease(repository string) *Release {
3768
release, err := getLatestReleaseForRepository(owner, repoName)
3869
if err != nil {
3970
return &Release{
40-
Name: "unknown",
41-
TagName: "v0.0.0",
42-
Prerelease: false,
71+
Name: "unknown",
72+
TagName: "v0.0.0",
73+
Prerelease: false,
74+
DaysSinceRelease: 0,
4375
}
4476
}
4577

78+
release.DaysSinceRelease, _ = daysSinceDate(release.PublishedAt)
79+
release.HoursSinceRelease, _ = hoursSinceDate(release.PublishedAt)
80+
81+
if release.DaysSinceRelease == 0 {
82+
release.TimeSinceRelease = fmt.Sprintf("%d hours ago", release.HoursSinceRelease)
83+
} else {
84+
release.TimeSinceRelease = fmt.Sprintf("%d days ago", release.DaysSinceRelease)
85+
}
86+
4687
return release
4788
}
4889

@@ -94,3 +135,29 @@ func extractSemver(version string) string {
94135

95136
return version
96137
}
138+
139+
func daysSinceDate(dateString string) (int, error) {
140+
layout := "2006-01-02T15:04:05Z"
141+
parsedDate, err := time.Parse(layout, dateString)
142+
if err != nil {
143+
return 0, err
144+
}
145+
146+
currentTime := time.Now()
147+
difference := currentTime.Sub(parsedDate)
148+
days := int(difference.Hours() / 24)
149+
return days, nil
150+
}
151+
152+
func hoursSinceDate(dateString string) (int, error) {
153+
layout := "2006-01-02T15:04:05Z"
154+
parsedDate, err := time.Parse(layout, dateString)
155+
if err != nil {
156+
return 0, err
157+
}
158+
159+
currentTime := time.Now()
160+
difference := currentTime.Sub(parsedDate)
161+
hours := int(difference.Hours())
162+
return hours, nil
163+
}

0 commit comments

Comments
 (0)