You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
diary/cmd/note.go

67 lines
1.3 KiB

/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
*/
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)
}