mirror of https://github.com/fmartingr/shiori.git
Merge pull request #259 from go-shiori/fix/exit-status
Exit with non-zero status when commands fail
This commit is contained in:
commit
ff5c464156
|
@ -3,6 +3,7 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -47,7 +48,7 @@ func checkHandler(cmd *cobra.Command, args []string) {
|
||||||
ids, err := parseStrIndices(args)
|
ids, err := parseStrIndices(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to parse args: %v\n", err)
|
cError.Printf("Failed to parse args: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch bookmarks from database
|
// Fetch bookmarks from database
|
||||||
|
@ -55,7 +56,7 @@ func checkHandler(cmd *cobra.Command, args []string) {
|
||||||
bookmarks, err := db.GetBookmarks(filterOptions)
|
bookmarks, err := db.GetBookmarks(filterOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to get bookmarks: %v\n", err)
|
cError.Printf("Failed to get bookmarks: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create HTTP client
|
// Create HTTP client
|
||||||
|
@ -127,15 +128,17 @@ func checkHandler(cmd *cobra.Command, args []string) {
|
||||||
// Print the unreachable bookmarks
|
// Print the unreachable bookmarks
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
|
||||||
|
var code int
|
||||||
if len(unreachableIDs) == 0 {
|
if len(unreachableIDs) == 0 {
|
||||||
cInfo.Println("All bookmarks is reachable.")
|
cInfo.Println("All bookmarks are reachable.")
|
||||||
} else {
|
} else {
|
||||||
sort.Ints(unreachableIDs)
|
sort.Ints(unreachableIDs)
|
||||||
|
code = 1
|
||||||
cError.Println("Encountered some unreachable bookmarks:")
|
cError.Println("Encountered some unreachable bookmarks:")
|
||||||
for _, id := range unreachableIDs {
|
for _, id := range unreachableIDs {
|
||||||
cError.Printf("%d ", id)
|
cError.Printf("%d ", id)
|
||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,14 +48,14 @@ func deleteHandler(cmd *cobra.Command, args []string) {
|
||||||
ids, err := parseStrIndices(args)
|
ids, err := parseStrIndices(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to parse args: %v\n", err)
|
cError.Printf("Failed to parse args: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete bookmarks from database
|
// Delete bookmarks from database
|
||||||
err = db.DeleteBookmarks(ids...)
|
err = db.DeleteBookmarks(ids...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to delete bookmarks: %v\n", err)
|
cError.Printf("Failed to delete bookmarks: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete thumbnail image and archives from local disk
|
// Delete thumbnail image and archives from local disk
|
||||||
|
|
|
@ -27,7 +27,7 @@ func exportHandler(cmd *cobra.Command, args []string) {
|
||||||
bookmarks, err := db.GetBookmarks(database.GetBookmarksOptions{})
|
bookmarks, err := db.GetBookmarks(database.GetBookmarksOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to get bookmarks: %v\n", err)
|
cError.Printf("Failed to get bookmarks: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(bookmarks) == 0 {
|
if len(bookmarks) == 0 {
|
||||||
|
@ -43,7 +43,7 @@ func exportHandler(cmd *cobra.Command, args []string) {
|
||||||
dstFile, err := os.Create(args[0])
|
dstFile, err := os.Create(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to create destination file: %v\n", err)
|
cError.Printf("Failed to create destination file: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer dstFile.Close()
|
defer dstFile.Close()
|
||||||
|
|
||||||
|
@ -85,7 +85,7 @@ func exportHandler(cmd *cobra.Command, args []string) {
|
||||||
err = dstFile.Sync()
|
err = dstFile.Sync()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to export the bookmarks: %v\n", err)
|
cError.Printf("Failed to export the bookmarks: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Export finished")
|
fmt.Println("Export finished")
|
||||||
|
|
|
@ -41,14 +41,14 @@ func importHandler(cmd *cobra.Command, args []string) {
|
||||||
bookID, err := db.CreateNewID("bookmark")
|
bookID, err := db.CreateNewID("bookmark")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to create ID: %v\n", err)
|
cError.Printf("Failed to create ID: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open bookmark's file
|
// Open bookmark's file
|
||||||
srcFile, err := os.Open(args[0])
|
srcFile, err := os.Open(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to open %s: %v\n", args[0], err)
|
cError.Printf("Failed to open %s: %v\n", args[0], err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer srcFile.Close()
|
defer srcFile.Close()
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ func importHandler(cmd *cobra.Command, args []string) {
|
||||||
doc, err := goquery.NewDocumentFromReader(srcFile)
|
doc, err := goquery.NewDocumentFromReader(srcFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to parse bookmark: %v\n", err)
|
cError.Printf("Failed to parse bookmark: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
doc.Find("dt>a").Each(func(_ int, a *goquery.Selection) {
|
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...)
|
bookmarks, err = db.SaveBookmarks(bookmarks...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to save bookmarks: %v\n", err)
|
cError.Printf("Failed to save bookmarks: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print imported bookmark
|
// Print imported bookmark
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
fp "path/filepath"
|
fp "path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -45,13 +46,13 @@ func openHandler(cmd *cobra.Command, args []string) {
|
||||||
ids, err := parseStrIndices(args)
|
ids, err := parseStrIndices(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Println(err)
|
cError.Println(err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If in archive mode, only one bookmark allowed
|
// If in archive mode, only one bookmark allowed
|
||||||
if len(ids) > 1 && archiveMode {
|
if len(ids) > 1 && archiveMode {
|
||||||
cError.Println("In archive mode, only one bookmark allowed")
|
cError.Println("In archive mode, only one bookmark allowed")
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no arguments (i.e all bookmarks will be opened),
|
// 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)
|
bookmarks, err := db.GetBookmarks(getOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to get bookmarks: %v\n", err)
|
cError.Printf("Failed to get bookmarks: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(bookmarks) == 0 {
|
if len(bookmarks) == 0 {
|
||||||
if len(ids) > 0 {
|
if len(ids) > 0 {
|
||||||
cError.Println("No matching index found")
|
cError.Println("No matching index found")
|
||||||
|
os.Exit(1)
|
||||||
} else {
|
} else {
|
||||||
cError.Println("No bookmarks saved yet")
|
cError.Println("No bookmarks saved yet")
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If not text cache mode nor archive mode, open bookmarks in browser
|
// If not text cache mode nor archive mode, open bookmarks in browser
|
||||||
if !textCacheMode && !archiveMode {
|
if !textCacheMode && !archiveMode {
|
||||||
|
var code int
|
||||||
for _, book := range bookmarks {
|
for _, book := range bookmarks {
|
||||||
err = openBrowser(book.URL)
|
err = openBrowser(book.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to open %s: %v\n", book.URL, err)
|
cError.Printf("Failed to open %s: %v\n", book.URL, err)
|
||||||
|
code = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show bookmarks content in terminal
|
// Show bookmarks content in terminal
|
||||||
if textCacheMode {
|
if textCacheMode {
|
||||||
termWidth := getTerminalWidth()
|
termWidth := getTerminalWidth()
|
||||||
|
|
||||||
|
var code int
|
||||||
for _, book := range bookmarks {
|
for _, book := range bookmarks {
|
||||||
cIndex.Printf("%d. ", book.ID)
|
cIndex.Printf("%d. ", book.ID)
|
||||||
cTitle.Println(book.Title)
|
cTitle.Println(book.Title)
|
||||||
|
@ -109,6 +115,7 @@ func openHandler(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
if book.Content == "" {
|
if book.Content == "" {
|
||||||
cError.Println("This bookmark doesn't have any cached content")
|
cError.Println("This bookmark doesn't have any cached content")
|
||||||
|
code = 1
|
||||||
} else {
|
} else {
|
||||||
book.Content = strings.Join(strings.Fields(book.Content), " ")
|
book.Content = strings.Join(strings.Fields(book.Content), " ")
|
||||||
fmt.Println(book.Content)
|
fmt.Println(book.Content)
|
||||||
|
@ -118,7 +125,7 @@ func openHandler(cmd *cobra.Command, args []string) {
|
||||||
cSymbol.Println(strings.Repeat("=", termWidth))
|
cSymbol.Println(strings.Repeat("=", termWidth))
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
return
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open archive
|
// Open archive
|
||||||
|
@ -128,7 +135,7 @@ func openHandler(cmd *cobra.Command, args []string) {
|
||||||
archive, err := warc.Open(archivePath)
|
archive, err := warc.Open(archivePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to open archive: %v\n", err)
|
cError.Printf("Failed to open archive: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer archive.Close()
|
defer archive.Close()
|
||||||
|
|
||||||
|
@ -162,7 +169,7 @@ func openHandler(cmd *cobra.Command, args []string) {
|
||||||
listener, err := net.Listen("tcp", listenerAddr)
|
listener, err := net.Listen("tcp", listenerAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to serve archive: %v\n", err)
|
cError.Printf("Failed to serve archive: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
portNumber := listener.Addr().(*net.TCPAddr).Port
|
portNumber := listener.Addr().(*net.TCPAddr).Port
|
||||||
|
@ -176,6 +183,7 @@ func openHandler(cmd *cobra.Command, args []string) {
|
||||||
err := openBrowser(localhostAddr)
|
err := openBrowser(localhostAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to open browser: %v\n", err)
|
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)
|
err = http.Serve(listener, router)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to serve archive: %v\n", err)
|
cError.Printf("Failed to serve archive: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ func pocketHandler(cmd *cobra.Command, args []string) {
|
||||||
srcFile, err := os.Open(args[0])
|
srcFile, err := os.Open(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Println(err)
|
cError.Println(err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer srcFile.Close()
|
defer srcFile.Close()
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ func pocketHandler(cmd *cobra.Command, args []string) {
|
||||||
doc, err := goquery.NewDocumentFromReader(srcFile)
|
doc, err := goquery.NewDocumentFromReader(srcFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Println(err)
|
cError.Println(err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
doc.Find("a").Each(func(_ int, a *goquery.Selection) {
|
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...)
|
bookmarks, err = db.SaveBookmarks(bookmarks...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to save bookmarks: %v\n", err)
|
cError.Printf("Failed to save bookmarks: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print imported bookmark
|
// Print imported bookmark
|
||||||
|
|
|
@ -3,6 +3,7 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/go-shiori/shiori/internal/database"
|
"github.com/go-shiori/shiori/internal/database"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -83,7 +84,7 @@ func printHandler(cmd *cobra.Command, args []string) {
|
||||||
bt, err := json.MarshalIndent(&bookmarks, "", " ")
|
bt, err := json.MarshalIndent(&bookmarks, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Println(err)
|
cError.Println(err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(bt))
|
fmt.Println(string(bt))
|
||||||
|
|
|
@ -2,6 +2,7 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -67,7 +68,7 @@ func updateHandler(cmd *cobra.Command, args []string) {
|
||||||
ids, err := parseStrIndices(args)
|
ids, err := parseStrIndices(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to parse args: %v\n", err)
|
cError.Printf("Failed to parse args: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up new parameter from flags
|
// 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
|
// Since user uses custom URL, make sure there is only one ID to update
|
||||||
if len(ids) != 1 {
|
if len(ids) != 1 {
|
||||||
cError.Println("Update only accepts one index while using --url flag")
|
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)
|
bookmarks, err := db.GetBookmarks(filterOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to get bookmarks: %v\n", err)
|
cError.Printf("Failed to get bookmarks: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(bookmarks) == 0 {
|
if len(bookmarks) == 0 {
|
||||||
cError.Println("No matching index found")
|
cError.Println("No matching index found")
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if user really want to batch update archive
|
// 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...)
|
bookmarks, err = db.SaveBookmarks(bookmarks...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cError.Printf("Failed to save bookmark: %v\n", err)
|
cError.Printf("Failed to save bookmark: %v\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print updated bookmarks
|
// Print updated bookmarks
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
printBookmarks(bookmarks...)
|
printBookmarks(bookmarks...)
|
||||||
|
|
||||||
|
var code int
|
||||||
if len(idWithProblems) > 0 {
|
if len(idWithProblems) > 0 {
|
||||||
|
code = 1
|
||||||
sort.Ints(idWithProblems)
|
sort.Ints(idWithProblems)
|
||||||
|
|
||||||
cError.Println("Encountered error while downloading some bookmark(s):")
|
cError.Println("Encountered error while downloading some bookmark(s):")
|
||||||
|
@ -298,4 +301,5 @@ func updateHandler(cmd *cobra.Command, args []string) {
|
||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue