From a0ce72516748aa379550b7cb81f55979b6eb3366 Mon Sep 17 00:00:00 2001 From: Dean Jackson Date: Fri, 13 Dec 2019 08:55:55 +0100 Subject: [PATCH] Exit with non-zero status when add fails --- internal/cmd/check.go | 11 +++++++---- internal/cmd/delete.go | 4 ++-- internal/cmd/export.go | 6 +++--- internal/cmd/import.go | 8 ++++---- internal/cmd/open.go | 23 ++++++++++++++++------- internal/cmd/pocket.go | 6 +++--- internal/cmd/print.go | 3 ++- internal/cmd/update.go | 14 +++++++++----- 8 files changed, 46 insertions(+), 29 deletions(-) diff --git a/internal/cmd/check.go b/internal/cmd/check.go index 660de6229..7a99f846c 100644 --- a/internal/cmd/check.go +++ b/internal/cmd/check.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "net/http" + "os" "sort" "sync" "time" @@ -47,7 +48,7 @@ func checkHandler(cmd *cobra.Command, args []string) { ids, err := parseStrIndices(args) if err != nil { cError.Printf("Failed to parse args: %v\n", err) - return + os.Exit(1) } // Fetch bookmarks from database @@ -55,7 +56,7 @@ func checkHandler(cmd *cobra.Command, args []string) { bookmarks, err := db.GetBookmarks(filterOptions) if err != nil { cError.Printf("Failed to get bookmarks: %v\n", err) - return + os.Exit(1) } // Create HTTP client @@ -127,15 +128,17 @@ func checkHandler(cmd *cobra.Command, args []string) { // Print the unreachable bookmarks fmt.Println() + var code int if len(unreachableIDs) == 0 { - cInfo.Println("All bookmarks is reachable.") + cInfo.Println("All bookmarks are reachable.") } else { sort.Ints(unreachableIDs) - + code = 1 cError.Println("Encountered some unreachable bookmarks:") for _, id := range unreachableIDs { cError.Printf("%d ", id) } fmt.Println() } + os.Exit(code) } diff --git a/internal/cmd/delete.go b/internal/cmd/delete.go index 082669184..93a6b52a4 100644 --- a/internal/cmd/delete.go +++ b/internal/cmd/delete.go @@ -48,14 +48,14 @@ func deleteHandler(cmd *cobra.Command, args []string) { ids, err := parseStrIndices(args) if err != nil { cError.Printf("Failed to parse args: %v\n", err) - return + os.Exit(1) } // Delete bookmarks from database err = db.DeleteBookmarks(ids...) if err != nil { cError.Printf("Failed to delete bookmarks: %v\n", err) - return + os.Exit(1) } // Delete thumbnail image and archives from local disk diff --git a/internal/cmd/export.go b/internal/cmd/export.go index cef4c7440..6484a3e0e 100644 --- a/internal/cmd/export.go +++ b/internal/cmd/export.go @@ -27,7 +27,7 @@ func exportHandler(cmd *cobra.Command, args []string) { bookmarks, err := db.GetBookmarks(database.GetBookmarksOptions{}) if err != nil { cError.Printf("Failed to get bookmarks: %v\n", err) - return + os.Exit(1) } if len(bookmarks) == 0 { @@ -43,7 +43,7 @@ func exportHandler(cmd *cobra.Command, args []string) { dstFile, err := os.Create(args[0]) if err != nil { cError.Printf("Failed to create destination file: %v\n", err) - return + os.Exit(1) } defer dstFile.Close() @@ -85,7 +85,7 @@ func exportHandler(cmd *cobra.Command, args []string) { err = dstFile.Sync() if err != nil { cError.Printf("Failed to export the bookmarks: %v\n", err) - return + os.Exit(1) } fmt.Println("Export finished") diff --git a/internal/cmd/import.go b/internal/cmd/import.go index 4243c9dcb..d917b9ee7 100644 --- a/internal/cmd/import.go +++ b/internal/cmd/import.go @@ -41,14 +41,14 @@ func importHandler(cmd *cobra.Command, args []string) { bookID, err := db.CreateNewID("bookmark") if err != nil { cError.Printf("Failed to create ID: %v\n", err) - return + os.Exit(1) } // Open bookmark's file srcFile, err := os.Open(args[0]) if err != nil { cError.Printf("Failed to open %s: %v\n", args[0], err) - return + os.Exit(1) } defer srcFile.Close() @@ -59,7 +59,7 @@ func importHandler(cmd *cobra.Command, args []string) { doc, err := goquery.NewDocumentFromReader(srcFile) if err != nil { cError.Printf("Failed to parse bookmark: %v\n", err) - return + os.Exit(1) } doc.Find("dt>a").Each(func(_ int, a *goquery.Selection) { @@ -130,7 +130,7 @@ func importHandler(cmd *cobra.Command, args []string) { bookmarks, err = db.SaveBookmarks(bookmarks...) if err != nil { cError.Printf("Failed to save bookmarks: %v\n", err) - return + os.Exit(1) } // Print imported bookmark diff --git a/internal/cmd/open.go b/internal/cmd/open.go index bd9ff528f..ce545e1a8 100644 --- a/internal/cmd/open.go +++ b/internal/cmd/open.go @@ -4,6 +4,7 @@ import ( "fmt" "net" "net/http" + "os" fp "path/filepath" "strconv" "strings" @@ -45,13 +46,13 @@ func openHandler(cmd *cobra.Command, args []string) { ids, err := parseStrIndices(args) if err != nil { cError.Println(err) - return + os.Exit(1) } // If in archive mode, only one bookmark allowed if len(ids) > 1 && archiveMode { cError.Println("In archive mode, only one bookmark allowed") - return + os.Exit(1) } // If no arguments (i.e all bookmarks will be opened), @@ -75,33 +76,38 @@ func openHandler(cmd *cobra.Command, args []string) { bookmarks, err := db.GetBookmarks(getOptions) if err != nil { cError.Printf("Failed to get bookmarks: %v\n", err) - return + os.Exit(1) } if len(bookmarks) == 0 { if len(ids) > 0 { cError.Println("No matching index found") + os.Exit(1) } else { cError.Println("No bookmarks saved yet") + os.Exit(1) } return } // If not text cache mode nor archive mode, open bookmarks in browser if !textCacheMode && !archiveMode { + var code int for _, book := range bookmarks { err = openBrowser(book.URL) if err != nil { cError.Printf("Failed to open %s: %v\n", book.URL, err) + code = 1 } } - return + os.Exit(code) } // Show bookmarks content in terminal if textCacheMode { termWidth := getTerminalWidth() + var code int for _, book := range bookmarks { cIndex.Printf("%d. ", book.ID) cTitle.Println(book.Title) @@ -109,6 +115,7 @@ func openHandler(cmd *cobra.Command, args []string) { if book.Content == "" { cError.Println("This bookmark doesn't have any cached content") + code = 1 } else { book.Content = strings.Join(strings.Fields(book.Content), " ") fmt.Println(book.Content) @@ -118,7 +125,7 @@ func openHandler(cmd *cobra.Command, args []string) { cSymbol.Println(strings.Repeat("=", termWidth)) fmt.Println() } - return + os.Exit(code) } // Open archive @@ -128,7 +135,7 @@ func openHandler(cmd *cobra.Command, args []string) { archive, err := warc.Open(archivePath) if err != nil { cError.Printf("Failed to open archive: %v\n", err) - return + os.Exit(1) } defer archive.Close() @@ -162,7 +169,7 @@ func openHandler(cmd *cobra.Command, args []string) { listener, err := net.Listen("tcp", listenerAddr) if err != nil { cError.Printf("Failed to serve archive: %v\n", err) - return + os.Exit(1) } portNumber := listener.Addr().(*net.TCPAddr).Port @@ -176,6 +183,7 @@ func openHandler(cmd *cobra.Command, args []string) { err := openBrowser(localhostAddr) if err != nil { cError.Printf("Failed to open browser: %v\n", err) + os.Exit(1) } }() @@ -183,5 +191,6 @@ func openHandler(cmd *cobra.Command, args []string) { err = http.Serve(listener, router) if err != nil { cError.Printf("Failed to serve archive: %v\n", err) + os.Exit(1) } } diff --git a/internal/cmd/pocket.go b/internal/cmd/pocket.go index dafa94701..f7d2ab553 100644 --- a/internal/cmd/pocket.go +++ b/internal/cmd/pocket.go @@ -36,7 +36,7 @@ func pocketHandler(cmd *cobra.Command, args []string) { srcFile, err := os.Open(args[0]) if err != nil { cError.Println(err) - return + os.Exit(1) } defer srcFile.Close() @@ -47,7 +47,7 @@ func pocketHandler(cmd *cobra.Command, args []string) { doc, err := goquery.NewDocumentFromReader(srcFile) if err != nil { cError.Println(err) - return + os.Exit(1) } doc.Find("a").Each(func(_ int, a *goquery.Selection) { @@ -109,7 +109,7 @@ func pocketHandler(cmd *cobra.Command, args []string) { bookmarks, err = db.SaveBookmarks(bookmarks...) if err != nil { cError.Printf("Failed to save bookmarks: %v\n", err) - return + os.Exit(1) } // Print imported bookmark diff --git a/internal/cmd/print.go b/internal/cmd/print.go index e1c8ac255..03eb6896b 100644 --- a/internal/cmd/print.go +++ b/internal/cmd/print.go @@ -3,6 +3,7 @@ package cmd import ( "encoding/json" "fmt" + "os" "github.com/go-shiori/shiori/internal/database" "github.com/spf13/cobra" @@ -83,7 +84,7 @@ func printHandler(cmd *cobra.Command, args []string) { bt, err := json.MarshalIndent(&bookmarks, "", " ") if err != nil { cError.Println(err) - return + os.Exit(1) } fmt.Println(string(bt)) diff --git a/internal/cmd/update.go b/internal/cmd/update.go index 896fa6699..da3e5f4fe 100644 --- a/internal/cmd/update.go +++ b/internal/cmd/update.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "os" "sort" "strings" "sync" @@ -67,7 +68,7 @@ func updateHandler(cmd *cobra.Command, args []string) { ids, err := parseStrIndices(args) if err != nil { cError.Printf("Failed to parse args: %v\n", err) - return + os.Exit(1) } // Clean up new parameter from flags @@ -84,7 +85,7 @@ func updateHandler(cmd *cobra.Command, args []string) { // Since user uses custom URL, make sure there is only one ID to update if len(ids) != 1 { cError.Println("Update only accepts one index while using --url flag") - return + os.Exit(1) } } @@ -96,12 +97,12 @@ func updateHandler(cmd *cobra.Command, args []string) { bookmarks, err := db.GetBookmarks(filterOptions) if err != nil { cError.Printf("Failed to get bookmarks: %v\n", err) - return + os.Exit(1) } if len(bookmarks) == 0 { cError.Println("No matching index found") - return + os.Exit(1) } // Check if user really want to batch update archive @@ -282,14 +283,16 @@ func updateHandler(cmd *cobra.Command, args []string) { bookmarks, err = db.SaveBookmarks(bookmarks...) if err != nil { cError.Printf("Failed to save bookmark: %v\n", err) - return + os.Exit(1) } // Print updated bookmarks fmt.Println() printBookmarks(bookmarks...) + var code int if len(idWithProblems) > 0 { + code = 1 sort.Ints(idWithProblems) cError.Println("Encountered error while downloading some bookmark(s):") @@ -298,4 +301,5 @@ func updateHandler(cmd *cobra.Command, args []string) { } fmt.Println() } + os.Exit(code) }