Skip to content

Commit 34fd3e6

Browse files
authored
Merge pull request #665 from yorukot/last-check-version-error
Fix Partial Overwrite Issue by Ensuring Full File Rewrite
2 parents 2db1ee7 + b26ebd2 commit 34fd3e6

File tree

1 file changed

+37
-49
lines changed

1 file changed

+37
-49
lines changed

src/cmd/main.go

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ func Run(content embed.FS) {
106106

107107
firstUse := checkFirstUse()
108108

109+
go CheckForUpdates()
110+
109111
p := tea.NewProgram(internal.InitialModel(path, firstUse, hasTrash), tea.WithAltScreen(), tea.WithMouseCellMotion())
110112
if _, err := p.Run(); err != nil {
111113
log.Fatalf("Alas, there's been an error: %v", err)
@@ -115,7 +117,6 @@ func Run(content embed.FS) {
115117
fmt.Println(variable.LastDir)
116118
}
117119

118-
CheckForUpdates()
119120
return nil
120121
},
121122
}
@@ -240,18 +241,29 @@ func initJsonFile(path string) error {
240241
return nil
241242
}
242243

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+
243251
// Check for the need of updates if AutoCheckUpdate is on, if its the first time
244252
// that version is checked or if has more than 24h since the last version check,
245253
// 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
246256
func CheckForUpdates() {
247257
var Config internal.ConfigType
248258

249259
// 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.
250263
data, err := os.ReadFile(variable.ConfigFile)
251264
if err != nil {
252265
log.Fatalf("Config file doesn't exist: %v", err)
253266
}
254-
255267
err = toml.Unmarshal(data, &Config)
256268
if err != nil {
257269
log.Fatalf("Error decoding config file ( your config file may be misconfigured ): %v", err)
@@ -261,16 +273,30 @@ func CheckForUpdates() {
261273
return
262274
}
263275

276+
// Get current time in UTC
277+
currentTime := time.Now().UTC()
278+
264279
// 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+
}
269293
}
270294

271-
currentTime := time.Now().UTC()
272-
273295
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+
}()
274300
client := &http.Client{
275301
Timeout: 5 * time.Second,
276302
}
@@ -283,6 +309,7 @@ func CheckForUpdates() {
283309

284310
body, err := io.ReadAll(resp.Body)
285311
if err != nil {
312+
slog.Error("Error reading response body", "error", err)
286313
return
287314
}
288315

@@ -292,6 +319,8 @@ func CheckForUpdates() {
292319

293320
var release GitHubRelease
294321
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)
295324
return
296325
}
297326

@@ -305,13 +334,6 @@ func CheckForUpdates() {
305334
fmt.Printf(lipgloss.NewStyle().Foreground(lipgloss.Color("#FF69E1")).Render("┃ ")+"Please update.\n\n\n => %s\n\n", variable.LatestVersionGithub)
306335
fmt.Printf(" ┛\n")
307336
}
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-
}
315337
}
316338
}
317339

@@ -323,37 +345,3 @@ func versionToNumber(version string) int {
323345
num, _ := strconv.Atoi(version)
324346
return num
325347
}
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

Comments
 (0)