diff --git a/cmd/add.go b/cmd/add.go index 91bf018..d7ed28e 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -22,7 +22,7 @@ var description string // addCmd represents the add command var addCmd = &cobra.Command{ - Use: "add", + Use: "add ", Short: "Add a bookmark", Long: `Adds a new bookmark to the bookmark manager. diff --git a/cmd/edit.go b/cmd/edit.go index 6024b1c..3a50aa0 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -5,7 +5,6 @@ package cmd import ( "fmt" - "log" "strings" "github.com/charmbracelet/huh" @@ -15,30 +14,41 @@ import ( // 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) { + Use: "edit [search query]", + Short: "Edit a bookmark", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { db, err := store.Open() if err != nil { - log.Panicln(err.Error()) + fmt.Println(err.Error()) return } defer db.Close() searchQuery := strings.Join(args, " ") + if searchQuery == "" { + err = huh.NewInput().Title("Search query").Value(&searchQuery).Run() + if err != nil { + fmt.Println(err.Error()) + return + } + if searchQuery == "" { + fmt.Println("no search query provided") + return + } + } bookmarks, err := store.SearchBookmarks(db, searchQuery) if err != nil { - log.Panicln("unable to search bookmarks", err.Error()) + fmt.Println("unable to search bookmarks", err.Error()) + return } if len(bookmarks) == 0 { fmt.Println("found no bookmarks") return } - if len(bookmarks) != 1 { + if len(bookmarks) != 1 { pickedIndex := 0 options := make([]huh.Option[int], len(bookmarks)) for i, bookmark := range bookmarks { @@ -49,7 +59,8 @@ var editCmd = &cobra.Command{ if err == huh.ErrUserAborted { return } - log.Fatalln(err.Error()) + fmt.Println(err.Error()) + return } bookmarks = []store.Bookmark{bookmarks[pickedIndex]} } @@ -66,12 +77,11 @@ var editCmd = &cobra.Command{ huh.NewText().Title("Description").Value(&bookmark.Description), )).Run() - bookmark.Tags = strings.Split(tags, ",") err = store.UpdateBookmark(db, original_bookmark, bookmark) if err != nil { - log.Fatalln(err.Error()) + fmt.Println(err.Error()) return } diff --git a/cmd/keys-delete.go b/cmd/keys-delete.go index 4adc9de..eed3433 100644 --- a/cmd/keys-delete.go +++ b/cmd/keys-delete.go @@ -12,13 +12,10 @@ import ( // keysDeleteCmd represents the delete command var keysDeleteCmd = &cobra.Command{ - Use: "delete", + Use: "delete ", Short: "Delete a key", - Long: `Pass in the key to delete from the allowed api keys for the local http server - -Example: -mark keys delete `, - Args: cobra.ExactArgs(1), + Long: `Pass in the key to delete from the allowed api keys for the local http server`, + Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { db, err := store.Open() if err != nil { diff --git a/cmd/keys.go b/cmd/keys.go index c7e8782..3061155 100644 --- a/cmd/keys.go +++ b/cmd/keys.go @@ -5,7 +5,6 @@ package cmd import ( "fmt" - "log" "github.com/lukasmwerner/mark/store" "github.com/spf13/cobra" @@ -14,23 +13,19 @@ import ( // keysCmd represents the keys command var keysCmd = &cobra.Command{ Use: "keys", - 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.`, + Short: "List all allowed api keys for the local http server", Run: func(cmd *cobra.Command, args []string) { db, err := store.Open() if err != nil { - log.Fatal(err) + fmt.Println(err.Error()) + return } defer db.Close() keys, err := store.GetKeys(db) if err != nil { - log.Fatal(err) + fmt.Println(err.Error()) + return } for _, key := range keys { fmt.Println(key) diff --git a/cmd/open.go b/cmd/open.go index f3653d0..fe40266 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -5,7 +5,6 @@ package cmd import ( "fmt" - "log" "strings" "github.com/charmbracelet/huh" @@ -23,7 +22,7 @@ var openCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { db, err := store.Open() if err != nil { - log.Panicln(err.Error()) + fmt.Println(err.Error()) return } defer db.Close() @@ -32,7 +31,8 @@ var openCmd = &cobra.Command{ bookmarks, err := store.SearchBookmarks(db, searchQuery) if err != nil { - log.Panicln("unable to search bookmarks", err.Error()) + fmt.Println("unable to search bookmarks", err.Error()) + return } if len(bookmarks) == 0 { fmt.Println("found no bookmarks") @@ -55,10 +55,15 @@ var openCmd = &cobra.Command{ if err == huh.ErrUserAborted { return } - log.Fatalln(err.Error()) + fmt.Println(err.Error()) + return } - browser.OpenURL(pickedLink) + err = browser.OpenURL(pickedLink) + if err != nil { + fmt.Println("unable to open url in browser", err.Error()) + return + } }, } diff --git a/cmd/root.go b/cmd/root.go index a3e157b..3b0a3da 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -231,8 +231,8 @@ var rootCmd = &cobra.Command{ Use: "mark", Short: "A simple bookmark manager from the commandline", Long: `Mark is a simple bookmark manager that allows you to save and recall bookmarks. -It also allows you to sync those changes across all your devices using a -file sync service. This is sort-of explained the following blog post: +It also allows you to sync those changes across all your devices using a +file sync service. This is sort-of explained the following blog post: https://lukaswerner.com/post/2024-08-13@Sqlite-Local-First`, // Uncomment the following line if your bare application // has an action associated with it: @@ -240,7 +240,7 @@ file sync service. This is sort-of explained the following blog post: db, err := store.Open() if err != nil { - log.Panicln(err) + fmt.Println("unable to open database", err.Error()) return } @@ -260,8 +260,8 @@ file sync service. This is sort-of explained the following blog post: prog := tea.NewProgram(m, tea.WithAltScreen()) if _, err := prog.Run(); err != nil { - fmt.Println("Error running program:", err) - os.Exit(1) + fmt.Println("Error running program:", err.Error()) + return } }, } diff --git a/cmd/server.go b/cmd/server.go index 7c75fa5..4b5fc08 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -23,7 +23,7 @@ var serverCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { db, err := store.Open() if err != nil { - log.Fatalln("error occured in opening db: ", err.Error()) + fmt.Println("error occured in opening db: ", err.Error()) return } @@ -63,7 +63,7 @@ var serverCmd = &cobra.Command{ return } w.WriteHeader(http.StatusCreated) - w.Write([]byte(fmt.Sprintf(`{"id": %d}`, id))) + w.Write(fmt.Appendf([]byte{}, `{"id": %d}`, id)) }))) http.Handle("PATCH /api/bookmarks", AuthRequired(db, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/cmd/show.go b/cmd/show.go index f70a41f..90550a5 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -7,7 +7,6 @@ import ( "encoding/csv" "encoding/json" "fmt" - "log" "os" "strings" @@ -27,7 +26,7 @@ var showCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { db, err := store.Open() if err != nil { - log.Panicln(err.Error()) + fmt.Println("unable to open database", err.Error()) return } defer db.Close() @@ -36,7 +35,8 @@ var showCmd = &cobra.Command{ bookmarks, err := store.SearchBookmarks(db, searchQuery) if err != nil { - log.Panicln("unable to search bookmarks", err.Error()) + fmt.Println("unable to search bookmarks", err.Error()) + return } if len(bookmarks) == 0 { fmt.Println("found no bookmarks") @@ -54,7 +54,8 @@ var showCmd = &cobra.Command{ if err == huh.ErrUserAborted { return } - log.Fatalln(err.Error()) + fmt.Println("unable to pick bookmark", err.Error()) + return } bookmarks = []store.Bookmark{bookmarks[pickedIndex]} }