Add ability to edit

pull/8/head
isthisnagee 3 years ago
parent a88b0f1444
commit 3760466b31

@ -0,0 +1,77 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
*/
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")
}

@ -0,0 +1,76 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
*/
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")
}

@ -8,7 +8,6 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"os/exec"
"strconv" "strconv"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -49,20 +48,7 @@ var noteCmd = &cobra.Command{
} }
defer os.Remove(temp_file.Name()) defer os.Remove(temp_file.Name())
var editor = os.Getenv("EDITOR") err = OpenEditor(temp_file.Name())
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()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

@ -15,6 +15,7 @@ import (
// showCmd represents the show command // showCmd represents the show command
var showCmd = &cobra.Command{ var showCmd = &cobra.Command{
Use: "show", Use: "show",
Args: cobra.ExactArgs(1),
Short: "A brief description of your command", Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example: and usage of using your command. For example:

@ -8,6 +8,7 @@ import (
"isthisnagee.com/tools/diary/model" "isthisnagee.com/tools/diary/model"
"log" "log"
"os" "os"
"os/exec"
"path" "path"
"strconv" "strconv"
"time" "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) 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() fmt.Println()
body := markdown.Render(note.Body, 80, 3) body := markdown.Render(note.Body, 80, 3)
fmt.Println(string(body)) fmt.Println(string(body))
if add_tail {
fmt.Println("---") fmt.Println("---")
} }
}
func PrintNotes(notes []*model.DiaryEntryNote) { func PrintNotes(notes []*model.DiaryEntryNote) {
for _, entry := range notes { for idx, entry := range notes {
PrintNote(entry) 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
} }

@ -6,6 +6,7 @@ import (
"database/sql" "database/sql"
"isthisnagee.com/tools/diary/db" "isthisnagee.com/tools/diary/db"
"log" "log"
"strings"
) )
type DiaryEntry struct { type DiaryEntry struct {
@ -51,6 +52,23 @@ func (app *App) NewDiaryEntry(title string) *DiaryEntry {
return &diary_entry 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) { func (app *App) GetDiaryEntry(id int64) (*DiaryEntry, error) {
var diary_entry DiaryEntry var diary_entry DiaryEntry

@ -1,5 +1,7 @@
package model package model
import "database/sql"
type DiaryEntryNote struct { type DiaryEntryNote struct {
Id int64 `json:"id"` Id int64 `json:"id"`
Body string `json:"body"` Body string `json:"body"`
@ -37,6 +39,37 @@ func (app *App) NewDiaryEntryNote(entry_id int64, body string) *DiaryEntryNote {
return &diary_entry_note 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(&note.Id, &note.Body, &note.CreatedAt, &note.Version, &note.LogId); err != nil {
if err == sql.ErrNoRows {
return nil, &NotFoundError{given_id: id}
}
return nil, err
}
return &note, 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 { func (app *App) GetDiaryEntryNotes(entry_id int64) []*DiaryEntryNote {
// not sure if it should be descending. // not sure if it should be descending.
rows, err := app.Db.Query( rows, err := app.Db.Query(

Loading…
Cancel
Save