From 82b4fbee64fd83c823b7b0f12cfc5e8d6c7883a4 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Fri, 8 Mar 2024 12:19:05 -0500 Subject: [PATCH] Properly close files to fix various issues on Windows (fixes GPG keygen, note temp files not being deleted) --- src/cli/edit.go | 11 ++++++++--- src/offline/init.go | 3 +++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/cli/edit.go b/src/cli/edit.go index 0b770e4..8711694 100644 --- a/src/cli/edit.go +++ b/src/cli/edit.go @@ -102,6 +102,9 @@ func editNote(baseNote []string) ([]string, bool) { } } + // close tempFile + tempFile.Close() + // edit the tempFile (note) with the user's text editor cmd := exec.Command(editor, tempFile.Name()) cmd.Stdout = os.Stdout @@ -114,20 +117,22 @@ func editNote(baseNote []string) ([]string, bool) { } // open the tempFile for reading - file, err := os.Open(tempFile.Name()) + tempFile, err = os.Open(tempFile.Name()) if err != nil { fmt.Println(offline.AnsiError + "Failed to open temporary file (\"" + tempFile.Name() + "\") " + err.Error() + offline.AnsiReset) os.Exit(1) } - defer file.Close() // read the edited note from the tempFile var note []string - scanner := bufio.NewScanner(file) + scanner := bufio.NewScanner(tempFile) for scanner.Scan() { note = append(note, scanner.Text()) } + // close the tempFile + tempFile.Close() + // return the edited note if it is different from baseNote if !reflect.DeepEqual(offline.RemoveTrailingEmptyStrings(note), baseNote) { return note, true diff --git a/src/offline/init.go b/src/offline/init.go index f75b3c8..1a9b5f7 100644 --- a/src/offline/init.go +++ b/src/offline/init.go @@ -55,6 +55,9 @@ func GpgKeyGen() string { unixTime := strconv.FormatInt(time.Now().Unix(), 10) gpgGenTempFile.WriteString(strings.Join([]string{"Key-Type: eddsa", "Key-Curve: ed25519", "Key-Usage: sign", "Subkey-Type: ecdh", "Subkey-Curve: cv25519", "Subkey-Usage: encrypt", "Name-Real: libmutton-" + unixTime, "Name-Comment: gpg-libmutton", "Name-Email: github.com/rwinkhart/libmutton", "Expire-Date: 0"}, "\n")) + // close gpg-gen file + gpgGenTempFile.Close() + // generate GPG key based on gpg-gen file cmd := exec.Command("gpg", "-q", "--batch", "--generate-key", gpgGenTempFile.Name()) cmd.Stdout = os.Stdout