Skip to content

Commit 083f9c7

Browse files
authored
Merge branch 'yorukot:main' into compress_all_files_selected
2 parents 5d1ff49 + 54aee46 commit 083f9c7

File tree

1 file changed

+70
-62
lines changed

1 file changed

+70
-62
lines changed

src/cmd/main.go

Lines changed: 70 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -249,84 +249,92 @@ func writeLastCheckTime(t time.Time) {
249249
// Check for the need of updates if AutoCheckUpdate is on, if its the first time
250250
// that version is checked or if has more than 24h since the last version check,
251251
// look into the repo if there's any more recent version
252-
// Todo : This is too big of a function. Refactor it to displayUpdateNotification, fetchLatestVersion,
253-
// shouldCheckForUpdates, chucks
254252
func CheckForUpdates() {
255253
if !common.Config.AutoCheckUpdate {
256254
return
257255
}
258256

259-
// Get current time in UTC
260257
currentTime := time.Now().UTC()
258+
lastCheckTime := readLastCheckTime()
261259

262-
// Check last time the version was checked
263-
content, err := os.ReadFile(variable.LastCheckVersion)
260+
if !shouldCheckForUpdate(currentTime, lastCheckTime) {
261+
return
262+
}
264263

265-
// Default to zero time if file doesn't exist, is empty, or has errors
266-
lastTime := time.Time{}
264+
defer writeLastCheckTime(currentTime)
265+
checkAndNotifyUpdate()
266+
}
267267

268-
if err == nil && len(content) > 0 {
269-
parsedTime, parseErr := time.Parse(time.RFC3339, string(content))
270-
if parseErr == nil {
271-
lastTime = parsedTime.UTC()
272-
} else {
273-
// Let the time stay as zero initialized value
274-
slog.Error("Error parsing time from LastCheckVersion file. Setting last time to zero", "error", parseErr)
275-
}
268+
// Default to zero time if file doesn't exist, is empty, or has errors
269+
func readLastCheckTime() time.Time {
270+
content, err := os.ReadFile(variable.LastCheckVersion)
271+
if err != nil || len(content) == 0 {
272+
return time.Time{}
276273
}
277274

278-
if lastTime.IsZero() || currentTime.Sub(lastTime) >= 24*time.Hour {
279-
// We would make sure to update the file in all return paths
280-
defer func() {
281-
writeLastCheckTime(currentTime)
282-
}()
283-
// Create a context with a 5-second timeout
284-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
285-
defer cancel() // Cancel the context when done
286-
client := &http.Client{}
287-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, variable.LatestVersionURL, nil)
288-
if err != nil {
289-
slog.Error("Error forming updates request:", "error", err)
290-
return
291-
}
275+
parsedTime, parseErr := time.Parse(time.RFC3339, string(content))
276+
if parseErr != nil {
277+
slog.Error("Failed to parse LastCheckVersion timestamp", "error", parseErr)
278+
return time.Time{}
279+
}
292280

293-
resp, err := client.Do(req)
294-
if err != nil {
295-
if ctx.Err() == context.DeadlineExceeded {
296-
slog.Error("Update check request timed out")
297-
return
298-
}
299-
slog.Error("Error checking for updates:", "error", err)
300-
return
301-
}
302-
defer resp.Body.Close()
281+
return parsedTime.UTC()
282+
}
303283

304-
body, err := io.ReadAll(resp.Body)
305-
if err != nil {
306-
slog.Error("Error reading response body", "error", err)
307-
return
308-
}
284+
func shouldCheckForUpdate(now, last time.Time) bool {
285+
return last.IsZero() || now.Sub(last) >= 24*time.Hour
286+
}
309287

310-
type GitHubRelease struct {
311-
TagName string `json:"tag_name"`
312-
}
288+
func checkAndNotifyUpdate() {
289+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
290+
defer cancel()
313291

314-
var release GitHubRelease
315-
if err := json.Unmarshal(body, &release); err != nil {
316-
// Update the timestamp file even if JSON parsing fails
317-
slog.Error("Error parsing JSON from Github", "error", err)
318-
return
319-
}
292+
resp, err := fetchLatestRelease(ctx)
293+
if err != nil {
294+
slog.Error("Failed to fetch update", "error", err)
295+
return
296+
}
297+
defer resp.Body.Close()
298+
299+
body, err := io.ReadAll(resp.Body)
300+
if err != nil {
301+
slog.Error("Failed to read update response", "error", err)
302+
return
303+
}
320304

321-
// Check if the local version is outdated
322-
if semver.Compare(release.TagName, variable.CurrentVersion) > 0 {
323-
fmt.Println(lipgloss.NewStyle().Foreground(lipgloss.Color("#FF69E1")).Render("┃ ") +
324-
lipgloss.NewStyle().Foreground(lipgloss.Color("#FFBA52")).Bold(true).Render("A new version ") +
325-
lipgloss.NewStyle().Foreground(lipgloss.Color("#00FFF2")).Bold(true).Italic(true).Render(release.TagName) +
326-
lipgloss.NewStyle().Foreground(lipgloss.Color("#FFBA52")).Bold(true).Render(" is available."))
305+
type GitHubRelease struct {
306+
TagName string `json:"tag_name"`
307+
}
327308

328-
fmt.Printf(lipgloss.NewStyle().Foreground(lipgloss.Color("#FF69E1")).Render("┃ ")+"Please update.\n\n\n => %s\n\n", variable.LatestVersionGithub)
329-
fmt.Printf(" ┛\n")
330-
}
309+
var release GitHubRelease
310+
if err := json.Unmarshal(body, &release); err != nil {
311+
slog.Error("Failed to parse GitHub JSON", "error", err)
312+
return
331313
}
314+
315+
if semver.Compare(release.TagName, variable.CurrentVersion) > 0 {
316+
notifyUpdateAvailable(release.TagName)
317+
}
318+
}
319+
320+
func fetchLatestRelease(ctx context.Context) (*http.Response, error) {
321+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, variable.LatestVersionURL, nil)
322+
if err != nil {
323+
return nil, err
324+
}
325+
return (&http.Client{}).Do(req)
326+
}
327+
328+
func notifyUpdateAvailable(latest string) {
329+
fmt.Println(
330+
lipgloss.NewStyle().Foreground(lipgloss.Color("#FF69E1")).Render("┃ ") +
331+
lipgloss.NewStyle().Foreground(lipgloss.Color("#FFBA52")).Bold(true).Render("A new version ") +
332+
lipgloss.NewStyle().Foreground(lipgloss.Color("#00FFF2")).Bold(true).Italic(true).Render(latest) +
333+
lipgloss.NewStyle().Foreground(lipgloss.Color("#FFBA52")).Bold(true).Render(" is available."),
334+
)
335+
fmt.Printf(
336+
lipgloss.NewStyle().Foreground(lipgloss.Color("#FF69E1")).Render("┃ ")+"Please update.\n\n\n => %s\n\n",
337+
variable.LatestVersionGithub,
338+
)
339+
fmt.Println(" ┛")
332340
}

0 commit comments

Comments
 (0)