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.
94 lines
2.0 KiB
94 lines
2.0 KiB
/*
|
|
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
|
|
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"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.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)
|
|
}
|
|
|
|
file_path := path.Join(os.TempDir(), "diary_note.md")
|
|
file, err := os.Create(file_path)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
file.Close()
|
|
|
|
var editor = os.Getenv("EDITOR")
|
|
if editor == "" {
|
|
editor = "nano"
|
|
}
|
|
editor_cmd := exec.Command(editor, file_path)
|
|
editor_cmd.Stdin = os.Stdin
|
|
editor_cmd.Stdout = os.Stdout
|
|
editor_cmd.Stderr = os.Stderr
|
|
|
|
err = editor_cmd.Start()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
err = editor_cmd.Wait()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
content, err := ioutil.ReadFile(file_path)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
os.Remove(file_path)
|
|
App.NewDiaryEntryNote(int64(entry_id), string(content))
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
addCmd.AddCommand(noteCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// noteCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// noteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
}
|