From 78837baad5414d01dd01083d62b52b2cd157feaf Mon Sep 17 00:00:00 2001 From: Andrew-71 Date: Sat, 4 May 2024 21:36:43 +0300 Subject: [PATCH] Improve file closing --- README.md | 3 --- TODO.md | 6 +----- config.go | 5 ++++- export.go | 13 +++++++------ 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index dbb8277..cafd1ed 100644 --- a/README.md +++ b/README.md @@ -8,15 +8,12 @@ As a result, I can't guarantee that it's either secure or stable. ## Features: * Each day, you get a text file. You have until 23:59 of that very day to finalise it. * You can save named notes to document milestones, big events, or just a nice game you played this month -* There is also a readme.txt file (just like this one, except you get to write it!)* * You can easily export entire `data` dir in a `.zip` archive for backups * Everything is plain(text) and simple. No databases, encryption, OAuth, or anything fancy. Even the password is plainte- *wait is this a feature?* * Docker support (in fact, that's probably the best way to run this) * Optional Telegram notifications for failed login attempts -*only available through API, subject to change and removal - ## Technical details You can read a relevant entry in my blog [here](https://a71.su/notes/hibiscus/). It provides some useful information and context for why this app exists in the first place. diff --git a/TODO.md b/TODO.md index 3c185fe..7d462c5 100644 --- a/TODO.md +++ b/TODO.md @@ -1,12 +1,8 @@ # TODO List of things to add to this project -* Fix the weird issue with compose and mounts (absolute path something) when updating!!! * CI/CD pipeline * Better docs in case others want to use ths for some reason -* Github/Codeberg/whatever mirror for when `faye` (my server) is offline -* API revamp +* GitHub/Codeberg/whatever mirror for when `faye` (my server) is offline * Check export function for improvements -* More slog.Debug -* Consider less clunky auth method * *Go* dependency-less? <-- this is a terrible idea \ No newline at end of file diff --git a/config.go b/config.go index 015c077..3579aa9 100644 --- a/config.go +++ b/config.go @@ -64,7 +64,6 @@ func (c *Config) Reload() error { if err != nil { return err } - defer file.Close() options := map[string]string{} scanner := bufio.NewScanner(file) @@ -78,6 +77,10 @@ func (c *Config) Reload() error { if err := scanner.Err(); err != nil { return err } + err = file.Close() + if err != nil { + return err + } timezone := "Local" // Timezone is handled separately because reflection refStruct := reflect.ValueOf(*c) diff --git a/export.go b/export.go index 57989df..162a55c 100644 --- a/export.go +++ b/export.go @@ -18,11 +18,8 @@ func Export(filename string) error { slog.Error("error creating export archive", "error", err) return err } - defer file.Close() w := zip.NewWriter(file) - defer w.Close() - walker := func(path string, info os.FileInfo, err error) error { if path == filename || filepath.Ext(path) == ".zip" { //Ignore export file itself and .zip archives return nil @@ -38,7 +35,6 @@ func Export(filename string) error { if err != nil { return err } - defer file.Close() f, err := w.Create(path) if err != nil { @@ -50,7 +46,7 @@ func Export(filename string) error { return err } - return nil + return file.Close() } err = filepath.Walk("data/", walker) if err != nil { @@ -58,13 +54,18 @@ func Export(filename string) error { return err } - return nil + err = file.Close() + if err != nil { + return err + } + return w.Close() } // GetExport returns a .zip archive with contents of the data folder func GetExport(w http.ResponseWriter, r *http.Request) { err := Export(ExportPath) if err != nil { + slog.Error("error getting export archive", "error", err) http.Error(w, "could not export", http.StatusInternalServerError) return }