M cmd/add.go => cmd/add.go +4 -4
@@ 19,7 19,6 @@ import (
"errors"
"fmt"
"net/url"
- "os"
"strings"
"github.com/spf13/cobra"
@@ 45,7 44,7 @@ bookmarks.`,
return nil
},
- Run: func(cmd *cobra.Command, args []string) {
+ RunE: func(cmd *cobra.Command, args []string) error {
b := bookmark.Bookmark{
URL: args[0],
Tags: tags,
@@ 53,11 52,12 @@ bookmarks.`,
}
if err := bookmark.Add(config.BookmarkPath, b); err != nil {
- fmt.Println(err)
- os.Exit(1)
+ return err
}
fmt.Printf("Added new bookmark %s\n", b.URL)
+
+ return nil
},
}
M cmd/doc.go => cmd/doc.go +4 -4
@@ 16,8 16,6 @@
package cmd
import (
- "log"
-
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
@@ 26,14 24,16 @@ var docCmd = &cobra.Command{
Use: "doc",
Short: "Generate man pages",
Hidden: true,
- Run: func(cmd *cobra.Command, args []string) {
+ RunE: func(cmd *cobra.Command, args []string) error {
header := &doc.GenManHeader{
Title: "BMK",
Section: "1",
}
if err := doc.GenManTree(rootCmd, header, "doc"); err != nil {
- log.Fatal(err)
+ return err
}
+
+ return nil
},
}
M cmd/list.go => cmd/list.go +4 -5
@@ 16,8 16,6 @@
package cmd
import (
- "fmt"
- "os"
"strings"
"git.sr.ht/~gjabell/bmk/bookmark"
@@ 30,15 28,16 @@ var listCmd = &cobra.Command{
Short: "List bookmarks",
Long: `List bookmarks in the manager, optionally specifying a list of tags or a
description. Bookmarks will be filtered by the tags and/or description.`,
- Run: func(cmd *cobra.Command, args []string) {
+ RunE: func(cmd *cobra.Command, args []string) error {
filter := bookmark.Filter{Tags: tags, Indices: []int{}, Desc: strings.Join(args[0:], " ")}
bookmarks, err := bookmark.List(config.BookmarkPath, filter)
if err != nil {
- fmt.Println(err)
- os.Exit(1)
+ return err
}
bookmark.Print(bookmarks)
+
+ return nil
},
}
M cmd/open.go => cmd/open.go +4 -4
@@ 18,7 18,6 @@ package cmd
import (
"errors"
"fmt"
- "os"
"os/exec"
"strconv"
@@ 57,12 56,11 @@ file.`,
return nil
},
- Run: func(cmd *cobra.Command, args []string) {
+ RunE: func(cmd *cobra.Command, args []string) error {
filter := bookmark.Filter{Tags: tags, Indices: indices, Desc: ""}
bookmarks, err := bookmark.List(config.BookmarkPath, filter)
if err != nil {
- fmt.Println(err)
- os.Exit(1)
+ return err
}
browserCmd := "xdg-open"
@@ 76,6 74,8 @@ file.`,
fmt.Println(err)
}
}
+
+ return nil
},
}
M cmd/remove.go => cmd/remove.go +5 -5
@@ 35,7 35,7 @@ var removeCmd = &cobra.Command{
Long: `Remove a bookmark from the manager. Takes a list of numbers referencing the
index values from the list command.`,
Args: cobra.MinimumNArgs(1),
- Run: func(cmd *cobra.Command, args []string) {
+ RunE: func(cmd *cobra.Command, args []string) error {
var indices []int
reader := bufio.NewReader(os.Stdin)
for _, i := range args {
@@ 54,8 54,7 @@ index values from the list command.`,
confirm, err := reader.ReadString('\n')
if err != nil {
- fmt.Println(err)
- os.Exit(1)
+ return err
}
if strings.ToLower(string(confirm[0])) == "y" {
@@ 67,13 66,14 @@ index values from the list command.`,
removed, err := bookmark.Remove(config.BookmarkPath, indices)
if err != nil {
- fmt.Println(err)
- os.Exit(1)
+ return err
}
for _, r := range removed {
fmt.Printf("Removed %s\n", r.URL)
}
+
+ return nil
},
}
M cmd/root.go => cmd/root.go +2 -2
@@ 36,8 36,8 @@ var rootCmd = &cobra.Command{
Long: `bmk is a command-line bookmark manager, designed to store and retrieve bookmarks
without relying on a web-browser.
`,
- Run: func(cmd *cobra.Command, args []string) {
- listCmd.Run(cmd, args)
+ RunE: func(cmd *cobra.Command, args []string) error {
+ return listCmd.RunE(cmd, args)
},
}