/* Copyright © 2021 NAME HERE */ package cmd import ( "io/ioutil" "log" "os" "strconv" "github.com/spf13/cobra" ) // noteCmd represents the note command var noteCmd = &cobra.Command{ Use: "note", Args: cobra.ExactArgs(1), Short: "Add a note to the specified diary entry", Long: `Will open your $EDITOR (or nano, if no editor is defined) To define a default editor, set the EDITOR environment variable. Notes can be written in markdown. For example: export EDITOR="vim" `, PreRunE: func(cmd *cobra.Command, args []string) error { id, err := strconv.Atoi(args[0]) if err != nil { return err } _, err = App.Db.GetDiaryEntry(int64(id)) if err != nil { return err } return nil }, Run: func(cmd *cobra.Command, args []string) { entry_id, err := strconv.Atoi(args[0]) if err != nil { log.Fatal(err) } temp_file, err := ioutil.TempFile(os.TempDir(), "diary_note.*.md") if err != nil { log.Fatal(err) } defer os.Remove(temp_file.Name()) err = OpenEditor(temp_file.Name()) if err != nil { log.Fatal(err) } content, err := ioutil.ReadFile(temp_file.Name()) if err != nil { log.Fatal(err) } App.Db.NewDiaryEntryNote(int64(entry_id), string(content)) }, } func init() { addCmd.AddCommand(noteCmd) }