Use a temp file to create the note

pull/8/head
isthisnagee 3 years ago
parent 8bc8b84583
commit 540ea14854

@ -9,7 +9,6 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"path"
"strconv" "strconv"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -44,18 +43,17 @@ var noteCmd = &cobra.Command{
log.Fatal(err) log.Fatal(err)
} }
file_path := path.Join(os.TempDir(), "diary_note.md") temp_file, err := ioutil.TempFile(os.TempDir(), "diary_note.*.md")
file, err := os.Create(file_path)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
file.Close() defer os.Remove(temp_file.Name())
var editor = os.Getenv("EDITOR") var editor = os.Getenv("EDITOR")
if editor == "" { if editor == "" {
editor = "nano" editor = "nano"
} }
editor_cmd := exec.Command(editor, file_path) editor_cmd := exec.Command(editor, temp_file.Name())
editor_cmd.Stdin = os.Stdin editor_cmd.Stdin = os.Stdin
editor_cmd.Stdout = os.Stdout editor_cmd.Stdout = os.Stdout
editor_cmd.Stderr = os.Stderr editor_cmd.Stderr = os.Stderr
@ -69,11 +67,10 @@ var noteCmd = &cobra.Command{
log.Fatal(err) log.Fatal(err)
} }
content, err := ioutil.ReadFile(file_path) content, err := ioutil.ReadFile(temp_file.Name())
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
os.Remove(file_path)
App.NewDiaryEntryNote(int64(entry_id), string(content)) App.NewDiaryEntryNote(int64(entry_id), string(content))
}, },
} }

Loading…
Cancel
Save