From 540ea1485474f46581f26600d9308cdaedc0965c Mon Sep 17 00:00:00 2001 From: isthisnagee Date: Tue, 28 Dec 2021 20:17:01 -0800 Subject: [PATCH] Use a temp file to create the note --- cmd/note.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/cmd/note.go b/cmd/note.go index 7780dec..8dd10cc 100644 --- a/cmd/note.go +++ b/cmd/note.go @@ -9,7 +9,6 @@ import ( "log" "os" "os/exec" - "path" "strconv" "github.com/spf13/cobra" @@ -44,18 +43,17 @@ var noteCmd = &cobra.Command{ log.Fatal(err) } - file_path := path.Join(os.TempDir(), "diary_note.md") - file, err := os.Create(file_path) + temp_file, err := ioutil.TempFile(os.TempDir(), "diary_note.*.md") if err != nil { log.Fatal(err) } - file.Close() + defer os.Remove(temp_file.Name()) var editor = os.Getenv("EDITOR") if editor == "" { editor = "nano" } - editor_cmd := exec.Command(editor, file_path) + editor_cmd := exec.Command(editor, temp_file.Name()) editor_cmd.Stdin = os.Stdin editor_cmd.Stdout = os.Stdout editor_cmd.Stderr = os.Stderr @@ -69,11 +67,10 @@ var noteCmd = &cobra.Command{ log.Fatal(err) } - content, err := ioutil.ReadFile(file_path) + content, err := ioutil.ReadFile(temp_file.Name()) if err != nil { log.Fatal(err) } - os.Remove(file_path) App.NewDiaryEntryNote(int64(entry_id), string(content)) }, }