/* Copyright © 2021 NAME HERE */ package cmd import ( "io/ioutil" "log" "os" "strconv" "strings" "github.com/spf13/cobra" ) // editNoteCmd represents the editNote command var editNoteCmd = &cobra.Command{ Use: "note", Short: "Edit the specified note", Args: cobra.ExactArgs(1), Long: ` Opens a file with the contents of the note. Edit this file to edit the note. Note that trailing spaces will be trimmed. `, PreRunE: func(cmd *cobra.Command, args []string) error { id, err := strconv.Atoi(args[0]) if err != nil { return err } _, err = App.Db.GetDiaryEntryNote(int64(id)) if err != nil { return err } return nil }, Run: func(cmd *cobra.Command, args []string) { id, _ := strconv.Atoi(args[0]) note, _ := App.Db.GetDiaryEntryNote(int64(id)) temp_file, err := ioutil.TempFile(os.TempDir(), "diary_note.*.md") if err != nil { log.Fatal(err) } // Preload the data in the file _, err = temp_file.Write([]byte(note.Body)) if err != nil { log.Fatal(err) } err = OpenEditor(temp_file.Name()) if err != nil { log.Fatal(err) } new_content, err := ioutil.ReadFile(temp_file.Name()) if err != nil { log.Fatal(err) } App.Db.EditDiaryEntryNote(note.Id, strings.TrimSpace(string(new_content))) }, } func init() { editCmd.AddCommand(editNoteCmd) }