Skip to content

Commit 223dba9

Browse files
committed
fix: Remove code duplication, and fix review comments
1 parent 381a658 commit 223dba9

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/cmd/main.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,27 @@ func initJsonFile(path string) error {
241241
return nil
242242
}
243243

244+
func writeLastCheckTime(t time.Time) {
245+
err := os.WriteFile(variable.LastCheckVersion, []byte(t.Format(time.RFC3339)), 0644)
246+
if err != nil {
247+
slog.Error("Error writing LastCheckVersion file", "error", err)
248+
}
249+
}
250+
244251
// Check for the need of updates if AutoCheckUpdate is on, if its the first time
245252
// that version is checked or if has more than 24h since the last version check,
246253
// look into the repo if there's any more recent version
247254
func CheckForUpdates() {
248255
var Config internal.ConfigType
249256

250257
// Get AutoCheck flag from configuration files
258+
259+
// Todo : We are reading the config file here, and also in the loadConfigFile functions
260+
// This needs to be fixed.
251261
data, err := os.ReadFile(variable.ConfigFile)
252262
if err != nil {
253263
log.Fatalf("Config file doesn't exist: %v", err)
254264
}
255-
256265
err = toml.Unmarshal(data, &Config)
257266
if err != nil {
258267
log.Fatalf("Error decoding config file ( your config file may be misconfigured ): %v", err)
@@ -276,32 +285,29 @@ func CheckForUpdates() {
276285
if parseErr == nil {
277286
lastTime = parsedTime.UTC()
278287
} else {
279-
// If we can't parse the time, overwrite with current time
280-
timeStr := currentTime.Format(time.RFC3339)
281-
os.WriteFile(variable.LastCheckVersion, []byte(timeStr), 0644)
288+
// Let the time stay as zero initialized value
289+
slog.Error("Error parsing time from LastCheckVersion file. Setting last time to zero", "error", parseErr)
282290
}
283291
}
284292

285293
if lastTime.IsZero() || currentTime.Sub(lastTime) >= 24*time.Hour {
294+
// We would make sure to update the file in all return paths
295+
defer func() {
296+
writeLastCheckTime(currentTime)
297+
}()
286298
client := &http.Client{
287299
Timeout: 5 * time.Second,
288300
}
289301
resp, err := client.Get(variable.LatestVersionURL)
290302
if err != nil {
291-
292-
slog.Error("Error checking for updates:", "error", err)
293-
// Update the timestamp file even if the update check fails
294-
timeStr := currentTime.Format(time.RFC3339)
295-
os.WriteFile(variable.LastCheckVersion, []byte(timeStr), 0644)
303+
slog.Error("Error checking for updates:", "error", err)
296304
return
297305
}
298306
defer resp.Body.Close()
299307

300308
body, err := io.ReadAll(resp.Body)
301309
if err != nil {
302-
// Update the timestamp file even if reading the response fails
303-
timeStr := currentTime.Format(time.RFC3339)
304-
os.WriteFile(variable.LastCheckVersion, []byte(timeStr), 0644)
310+
slog.Error("Error reading response body", "error", err)
305311
return
306312
}
307313

@@ -312,8 +318,7 @@ func CheckForUpdates() {
312318
var release GitHubRelease
313319
if err := json.Unmarshal(body, &release); err != nil {
314320
// Update the timestamp file even if JSON parsing fails
315-
timeStr := currentTime.Format(time.RFC3339)
316-
os.WriteFile(variable.LastCheckVersion, []byte(timeStr), 0644)
321+
slog.Error("Error parsing JSON from Github", "error", err)
317322
return
318323
}
319324

@@ -327,10 +332,6 @@ func CheckForUpdates() {
327332
fmt.Printf(lipgloss.NewStyle().Foreground(lipgloss.Color("#FF69E1")).Render("┃ ")+"Please update.\n\n\n => %s\n\n", variable.LatestVersionGithub)
328333
fmt.Printf(" ┛\n")
329334
}
330-
331-
// Always update the timestamp file after checking
332-
timeStr := currentTime.Format(time.RFC3339)
333-
os.WriteFile(variable.LastCheckVersion, []byte(timeStr), 0644)
334335
}
335336
}
336337

0 commit comments

Comments
 (0)