From 5b8460bf539e8f4990189da7accd669e55648154 Mon Sep 17 00:00:00 2001 From: Lukas Werner Date: Thu, 29 Aug 2024 13:54:41 -0700 Subject: [PATCH] feat: add edit command! --- cmd/edit.go | 92 +++++++++++++++++++++++++++++++++++++++++++++++ store/database.go | 20 +++++++++-- 2 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 cmd/edit.go diff --git a/cmd/edit.go b/cmd/edit.go new file mode 100644 index 0000000..3bf5d80 --- /dev/null +++ b/cmd/edit.go @@ -0,0 +1,92 @@ +/* +Copyright © 2024 Lukas Werner +*/ +package cmd + +import ( + "fmt" + "log" + "strings" + + "github.com/charmbracelet/huh" + "github.com/lukasmwerner/mark/store" + "github.com/spf13/cobra" +) + +// editCmd represents the edit command +var editCmd = &cobra.Command{ + Use: "edit", + Short: "A brief description of your command", + Long: ``, + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + db, err := store.Open() + if err != nil { + log.Panicln(err.Error()) + return + } + defer db.Close() + + searchQuery := strings.Join(args, " ") + + bookmarks, err := store.SearchBookmarks(db, searchQuery) + if err != nil { + log.Panicln("unable to search bookmarks", err.Error()) + } + if len(bookmarks) == 0 { + fmt.Println("found no bookmarks") + return + } + + if len(bookmarks) != 1 { + pickedIndex := 0 + options := make([]huh.Option[int], len(bookmarks)) + for i, bookmark := range bookmarks { + options[i] = huh.NewOption(bookmark.Title, i) + } + err = huh.NewSelect[int]().Title("Pick your link").Options(options...).Value(&pickedIndex).Run() + if err != nil { + if err == huh.ErrUserAborted { + return + } + log.Fatalln(err.Error()) + } + bookmarks = []store.Bookmark{bookmarks[pickedIndex]} + } + + bookmark := bookmarks[0] + original_bookmark := bookmark + + tags := strings.Join(bookmark.Tags, ",") + + huh.NewForm(huh.NewGroup( + huh.NewInput().Title("Title").Value(&bookmark.Title), + huh.NewInput().Title("Tags").Value(&tags), + huh.NewInput().Title("URL").Value(&bookmark.Url), + huh.NewText().Title("Description").Value(&bookmark.Description), + )).Run() + + + + err = store.UpdateBookmark(db, original_bookmark, bookmark) + if err != nil { + log.Fatalln(err.Error()) + return + } + + }, +} + +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/store/database.go b/store/database.go index 490167d..95db0b5 100644 --- a/store/database.go +++ b/store/database.go @@ -111,7 +111,6 @@ func Open() (*DB, error) { return nil, errors.Join(errors.New("unable to setup crdts"), err) } - err = syncronizeFromHostsToDB(db, hostname, changesPath) if err != nil { return nil, errors.Join(errors.New("unable to sync fs -> db"), err) @@ -165,7 +164,6 @@ func InsertBookmark(db *DB, bookmark Bookmark) (BookmarkId, error) { return BookmarkId(id), err } - func SearchBookmarks(db *DB, query string) ([]Bookmark, error) { bookmarks := []Bookmark{} rows, err := db.Query(`SELECT url, title, description, tags FROM Bookmarks_fts WHERE Bookmarks_fts MATCH ?;`, query) @@ -187,3 +185,21 @@ func SearchBookmarks(db *DB, query string) ([]Bookmark, error) { return bookmarks, nil } + +func UpdateBookmark(db *DB, original Bookmark, updated Bookmark) error { + _, err := db.Exec(`UPDATE Bookmarks SET + url = ?, + title = ?, + description = ?, + tags = ? + WHERE + url = ?;`, + updated.Url, + updated.Title, + updated.Description, + strings.Join(updated.Tags, ", "), + original.Url, + ) + + return err +} -- 2.51.2