@@ -106,6 +106,8 @@ func Run(content embed.FS) {
106
106
107
107
firstUse := checkFirstUse ()
108
108
109
+ go CheckForUpdates ()
110
+
109
111
p := tea .NewProgram (internal .InitialModel (path , firstUse , hasTrash ), tea .WithAltScreen (), tea .WithMouseCellMotion ())
110
112
if _ , err := p .Run (); err != nil {
111
113
log .Fatalf ("Alas, there's been an error: %v" , err )
@@ -115,7 +117,6 @@ func Run(content embed.FS) {
115
117
fmt .Println (variable .LastDir )
116
118
}
117
119
118
- CheckForUpdates ()
119
120
return nil
120
121
},
121
122
}
@@ -240,18 +241,29 @@ func initJsonFile(path string) error {
240
241
return nil
241
242
}
242
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
+
243
251
// Check for the need of updates if AutoCheckUpdate is on, if its the first time
244
252
// that version is checked or if has more than 24h since the last version check,
245
253
// look into the repo if there's any more recent version
254
+ // Todo : This is too big of a function. Refactor it to displayUpdateNotification, fetchLatestVersion,
255
+ // shouldCheckForUpdates, chucks
246
256
func CheckForUpdates () {
247
257
var Config internal.ConfigType
248
258
249
259
// Get AutoCheck flag from configuration files
260
+
261
+ // Todo : We are reading the config file here, and also in the loadConfigFile functions
262
+ // This needs to be fixed.
250
263
data , err := os .ReadFile (variable .ConfigFile )
251
264
if err != nil {
252
265
log .Fatalf ("Config file doesn't exist: %v" , err )
253
266
}
254
-
255
267
err = toml .Unmarshal (data , & Config )
256
268
if err != nil {
257
269
log .Fatalf ("Error decoding config file ( your config file may be misconfigured ): %v" , err )
@@ -261,16 +273,30 @@ func CheckForUpdates() {
261
273
return
262
274
}
263
275
276
+ // Get current time in UTC
277
+ currentTime := time .Now ().UTC ()
278
+
264
279
// Check last time the version was checked
265
- lastTime , err := readLastTimeCheckVersionFromFile (variable .LastCheckVersion )
266
- if err != nil && ! os .IsNotExist (err ) {
267
- fmt .Println ("Error reading from file:" , err )
268
- return
280
+ content , err := os .ReadFile (variable .LastCheckVersion )
281
+
282
+ // Default to zero time if file doesn't exist, is empty, or has errors
283
+ lastTime := time.Time {}
284
+
285
+ if err == nil && len (content ) > 0 {
286
+ parsedTime , parseErr := time .Parse (time .RFC3339 , string (content ))
287
+ if parseErr == nil {
288
+ lastTime = parsedTime .UTC ()
289
+ } else {
290
+ // Let the time stay as zero initialized value
291
+ slog .Error ("Error parsing time from LastCheckVersion file. Setting last time to zero" , "error" , parseErr )
292
+ }
269
293
}
270
294
271
- currentTime := time .Now ().UTC ()
272
-
273
295
if lastTime .IsZero () || currentTime .Sub (lastTime ) >= 24 * time .Hour {
296
+ // We would make sure to update the file in all return paths
297
+ defer func () {
298
+ writeLastCheckTime (currentTime )
299
+ }()
274
300
client := & http.Client {
275
301
Timeout : 5 * time .Second ,
276
302
}
@@ -283,6 +309,7 @@ func CheckForUpdates() {
283
309
284
310
body , err := io .ReadAll (resp .Body )
285
311
if err != nil {
312
+ slog .Error ("Error reading response body" , "error" , err )
286
313
return
287
314
}
288
315
@@ -292,6 +319,8 @@ func CheckForUpdates() {
292
319
293
320
var release GitHubRelease
294
321
if err := json .Unmarshal (body , & release ); err != nil {
322
+ // Update the timestamp file even if JSON parsing fails
323
+ slog .Error ("Error parsing JSON from Github" , "error" , err )
295
324
return
296
325
}
297
326
@@ -305,13 +334,6 @@ func CheckForUpdates() {
305
334
fmt .Printf (lipgloss .NewStyle ().Foreground (lipgloss .Color ("#FF69E1" )).Render ("┃ " )+ "Please update.\n ┏\n \n => %s\n \n " , variable .LatestVersionGithub )
306
335
fmt .Printf (" ┛\n " )
307
336
}
308
-
309
- timeStr := currentTime .Format (time .RFC3339 )
310
- err = writeToFile (variable .LastCheckVersion , timeStr )
311
- if err != nil {
312
- log .Println ("Error writing to file:" , err )
313
- return
314
- }
315
337
}
316
338
}
317
339
@@ -323,37 +345,3 @@ func versionToNumber(version string) int {
323
345
num , _ := strconv .Atoi (version )
324
346
return num
325
347
}
326
-
327
- // Check the last time the version file was checked
328
- func readLastTimeCheckVersionFromFile (filename string ) (time.Time , error ) {
329
- content , err := os .ReadFile (filename )
330
- if err != nil {
331
- return time.Time {}, err
332
- }
333
- if len (content ) == 0 {
334
- return time.Time {}, nil
335
- }
336
- lastTime , err := time .Parse (time .RFC3339 , string (content ))
337
- if err != nil {
338
- return time.Time {}, err
339
- }
340
-
341
- // Ensure the time is in UTC
342
- return lastTime .UTC (), nil
343
- }
344
-
345
- // Write content to filename
346
- func writeToFile (filename , content string ) error {
347
- file , err := os .OpenFile (filename , os .O_WRONLY | os .O_CREATE , 0644 )
348
- if err != nil {
349
- return err
350
- }
351
- defer file .Close ()
352
-
353
- _ , err = file .WriteString (content )
354
- if err != nil {
355
- return err
356
- }
357
-
358
- return nil
359
- }
0 commit comments