Properly close files to fix various issues on Windows (fixes GPG keygen, note temp files not being deleted)

This commit is contained in:
2024-03-08 12:19:05 -05:00
parent f8bf12677f
commit 4394020dd2
+8 -3
View File
@@ -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