Skip to content

Commit

Permalink
Exit with non-zero status when add fails
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Aug 6, 2020
1 parent 7124dc2 commit a0ce725
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 29 deletions.
11 changes: 7 additions & 4 deletions internal/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"net/http"
"os"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -47,15 +48,15 @@ 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
filterOptions := database.GetBookmarksOptions{IDs: ids}
bookmarks, err := db.GetBookmarks(filterOptions)
if err != nil {
cError.Printf("Failed to get bookmarks: %v\n", err)
return
os.Exit(1)
}

// Create HTTP client
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions internal/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()

Expand Down Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
23 changes: 16 additions & 7 deletions internal/cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"net/http"
"os"
fp "path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -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),
Expand All @@ -75,40 +76,46 @@ 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)
fmt.Println()

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)
Expand All @@ -118,7 +125,7 @@ func openHandler(cmd *cobra.Command, args []string) {
cSymbol.Println(strings.Repeat("=", termWidth))
fmt.Println()
}
return
os.Exit(code)
}

// Open archive
Expand All @@ -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()

Expand Down Expand Up @@ -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
Expand All @@ -176,12 +183,14 @@ 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)
}
}()

// Serve archive
err = http.Serve(listener, router)
if err != nil {
cError.Printf("Failed to serve archive: %v\n", err)
os.Exit(1)
}
}
6 changes: 3 additions & 3 deletions internal/cmd/pocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"encoding/json"
"fmt"
"os"

"github.com/go-shiori/shiori/internal/database"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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))
Expand Down
14 changes: 9 additions & 5 deletions internal/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"os"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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):")
Expand All @@ -298,4 +301,5 @@ func updateHandler(cmd *cobra.Command, args []string) {
}
fmt.Println()
}
os.Exit(code)
}

0 comments on commit a0ce725

Please sign in to comment.