@@ -241,18 +241,27 @@ func initJsonFile(path string) error {
241
241
return nil
242
242
}
243
243
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
+
244
251
// Check for the need of updates if AutoCheckUpdate is on, if its the first time
245
252
// that version is checked or if has more than 24h since the last version check,
246
253
// look into the repo if there's any more recent version
247
254
func CheckForUpdates () {
248
255
var Config internal.ConfigType
249
256
250
257
// 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.
251
261
data , err := os .ReadFile (variable .ConfigFile )
252
262
if err != nil {
253
263
log .Fatalf ("Config file doesn't exist: %v" , err )
254
264
}
255
-
256
265
err = toml .Unmarshal (data , & Config )
257
266
if err != nil {
258
267
log .Fatalf ("Error decoding config file ( your config file may be misconfigured ): %v" , err )
@@ -276,32 +285,29 @@ func CheckForUpdates() {
276
285
if parseErr == nil {
277
286
lastTime = parsedTime .UTC ()
278
287
} 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 )
282
290
}
283
291
}
284
292
285
293
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
+ }()
286
298
client := & http.Client {
287
299
Timeout : 5 * time .Second ,
288
300
}
289
301
resp , err := client .Get (variable .LatestVersionURL )
290
302
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 )
296
304
return
297
305
}
298
306
defer resp .Body .Close ()
299
307
300
308
body , err := io .ReadAll (resp .Body )
301
309
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 )
305
311
return
306
312
}
307
313
@@ -312,8 +318,7 @@ func CheckForUpdates() {
312
318
var release GitHubRelease
313
319
if err := json .Unmarshal (body , & release ); err != nil {
314
320
// 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 )
317
322
return
318
323
}
319
324
@@ -327,10 +332,6 @@ func CheckForUpdates() {
327
332
fmt .Printf (lipgloss .NewStyle ().Foreground (lipgloss .Color ("#FF69E1" )).Render ("┃ " )+ "Please update.\n ┏\n \n => %s\n \n " , variable .LatestVersionGithub )
328
333
fmt .Printf (" ┛\n " )
329
334
}
330
-
331
- // Always update the timestamp file after checking
332
- timeStr := currentTime .Format (time .RFC3339 )
333
- os .WriteFile (variable .LastCheckVersion , []byte (timeStr ), 0644 )
334
335
}
335
336
}
336
337
0 commit comments