From 3760466b31483d2474da4cfce5a53329b8b4f5fe Mon Sep 17 00:00:00 2001 From: isthisnagee Date: Sat, 1 Jan 2022 08:56:30 -0800 Subject: [PATCH] Add ability to edit --- cmd/edit.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ cmd/editNote.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/note.go | 16 +--------- cmd/show.go | 1 + cmd/util.go | 35 ++++++++++++++++++---- model/entry.go | 18 ++++++++++++ model/note.go | 33 +++++++++++++++++++++ 7 files changed, 236 insertions(+), 20 deletions(-) create mode 100644 cmd/edit.go create mode 100644 cmd/editNote.go diff --git a/cmd/edit.go b/cmd/edit.go new file mode 100644 index 0000000..e7510c8 --- /dev/null +++ b/cmd/edit.go @@ -0,0 +1,77 @@ +/* +Copyright © 2021 NAME HERE + +*/ +package cmd + +import ( + "io/ioutil" + "log" + "os" + "strconv" + + "github.com/spf13/cobra" +) + +// editCmd represents the edit command +var editCmd = &cobra.Command{ + Use: "edit", + Short: "A brief description of your command", + Args: cobra.ExactArgs(1), + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + 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) { + id, _ := strconv.Atoi(args[0]) + entry, _ := App.GetDiaryEntry(int64(id)) + temp_file, err := ioutil.TempFile(os.TempDir(), "diary_note_title.*.md") + if err != nil { + log.Fatal(err) + } + + // Preload the data in the file + _, err = temp_file.Write([]byte(entry.Title)) + 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.EditDiaryEntry(entry.Id, string(new_content)) + + }, +} + +func init() { + rootCmd.AddCommand(editCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // editCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // editCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/editNote.go b/cmd/editNote.go new file mode 100644 index 0000000..297001b --- /dev/null +++ b/cmd/editNote.go @@ -0,0 +1,76 @@ +/* +Copyright © 2021 NAME HERE + +*/ +package cmd + +import ( + "io/ioutil" + "log" + "os" + "strconv" + + "github.com/spf13/cobra" +) + +// editNoteCmd represents the editNote command +var editNoteCmd = &cobra.Command{ + Use: "note", + Args: cobra.ExactArgs(1), + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + PreRunE: func(cmd *cobra.Command, args []string) error { + id, err := strconv.Atoi(args[0]) + if err != nil { + return err + } + _, err = App.GetDiaryEntryNote(int64(id)) + if err != nil { + return err + } + return nil + }, + Run: func(cmd *cobra.Command, args []string) { + id, _ := strconv.Atoi(args[0]) + note, _ := App.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.EditDiaryEntryNote(note.Id, string(new_content)) + }, +} + +func init() { + editCmd.AddCommand(editNoteCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // editNoteCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // editNoteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/note.go b/cmd/note.go index 8dd10cc..244f955 100644 --- a/cmd/note.go +++ b/cmd/note.go @@ -8,7 +8,6 @@ import ( "io/ioutil" "log" "os" - "os/exec" "strconv" "github.com/spf13/cobra" @@ -49,20 +48,7 @@ var noteCmd = &cobra.Command{ } defer os.Remove(temp_file.Name()) - var editor = os.Getenv("EDITOR") - if editor == "" { - editor = "nano" - } - editor_cmd := exec.Command(editor, temp_file.Name()) - 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() + err = OpenEditor(temp_file.Name()) if err != nil { log.Fatal(err) } diff --git a/cmd/show.go b/cmd/show.go index 718f142..096d2d7 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -15,6 +15,7 @@ import ( // showCmd represents the show command var showCmd = &cobra.Command{ Use: "show", + Args: cobra.ExactArgs(1), Short: "A brief description of your command", Long: `A longer description that spans multiple lines and likely contains examples and usage of using your command. For example: diff --git a/cmd/util.go b/cmd/util.go index 587b0eb..8a4b847 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -8,6 +8,7 @@ import ( "isthisnagee.com/tools/diary/model" "log" "os" + "os/exec" "path" "strconv" "time" @@ -62,17 +63,41 @@ func PrintEntries(entries []*model.DiaryEntry) { } } -func PrintNote(note *model.DiaryEntryNote) { +func PrintNote(note *model.DiaryEntryNote, add_tail bool) { created_time := time.Unix(note.CreatedAt, 0).Format(time_fmt_str) - fmt.Println("---note created_at ", created_time) + fmt.Println("---note ( id:", note.Id, ")", created_time) fmt.Println() body := markdown.Render(note.Body, 80, 3) fmt.Println(string(body)) - fmt.Println("---") + if add_tail { + fmt.Println("---") + } } func PrintNotes(notes []*model.DiaryEntryNote) { - for _, entry := range notes { - PrintNote(entry) + for idx, entry := range notes { + var is_last = idx == len(notes)-1 + PrintNote(entry, is_last) + } +} + +func OpenEditor(fpath string) error { + var editor = os.Getenv("EDITOR") + if editor == "" { + editor = "nano" + } + editor_cmd := exec.Command(editor, fpath) + editor_cmd.Stdin = os.Stdin + editor_cmd.Stdout = os.Stdout + editor_cmd.Stderr = os.Stderr + + err := editor_cmd.Start() + if err != nil { + return err + } + err = editor_cmd.Wait() + if err != nil { + return err } + return nil } diff --git a/model/entry.go b/model/entry.go index 3f9ea42..82ec45f 100644 --- a/model/entry.go +++ b/model/entry.go @@ -6,6 +6,7 @@ import ( "database/sql" "isthisnagee.com/tools/diary/db" "log" + "strings" ) type DiaryEntry struct { @@ -51,6 +52,23 @@ func (app *App) NewDiaryEntry(title string) *DiaryEntry { return &diary_entry } +func (app *App) EditDiaryEntry(id int64, title string) (*DiaryEntryNote, error) { + clean_title := strings.TrimSpace(title) + result, err := app.Db.Exec("UPDATE diary_log set title=? where id=?", clean_title, id) + if err != nil { + return nil, err + } + + num_rows_affected, err := result.RowsAffected() + if err != nil { + return nil, err + } + if num_rows_affected == 0 { + return nil, &NotFoundError{given_id: id} + } + return app.GetDiaryEntryNote(id) +} + func (app *App) GetDiaryEntry(id int64) (*DiaryEntry, error) { var diary_entry DiaryEntry diff --git a/model/note.go b/model/note.go index 48d18b2..4a2ee21 100644 --- a/model/note.go +++ b/model/note.go @@ -1,5 +1,7 @@ package model +import "database/sql" + type DiaryEntryNote struct { Id int64 `json:"id"` Body string `json:"body"` @@ -37,6 +39,37 @@ func (app *App) NewDiaryEntryNote(entry_id int64, body string) *DiaryEntryNote { return &diary_entry_note } +func (app *App) GetDiaryEntryNote(id int64) (*DiaryEntryNote, error) { + var note DiaryEntryNote + + if err := app.Db.QueryRow( + "SELECT id, body, created_at, version, log_id FROM diary_log_note WHERE id=? ", + id, + ).Scan(¬e.Id, ¬e.Body, ¬e.CreatedAt, ¬e.Version, ¬e.LogId); err != nil { + if err == sql.ErrNoRows { + return nil, &NotFoundError{given_id: id} + } + return nil, err + } + return ¬e, nil +} + +func (app *App) EditDiaryEntryNote(id int64, body string) (*DiaryEntryNote, error) { + result, err := app.Db.Exec("UPDATE diary_log_note set body=? where id=?", body, id) + if err != nil { + return nil, err + } + + num_rows_affected, err := result.RowsAffected() + if err != nil { + return nil, err + } + if num_rows_affected == 0 { + return nil, &NotFoundError{given_id: id} + } + return app.GetDiaryEntryNote(id) +} + func (app *App) GetDiaryEntryNotes(entry_id int64) []*DiaryEntryNote { // not sure if it should be descending. rows, err := app.Db.Query(