Implement logic for open cmd

This commit is contained in:
Radhi Fadlillah 2019-05-22 16:47:20 +07:00
parent b2a3d444d3
commit ed2b6b0740
4 changed files with 111 additions and 2 deletions

1
go.mod
View File

@ -13,5 +13,6 @@ require (
github.com/mattn/go-sqlite3 v1.10.0
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.4
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
google.golang.org/appengine v1.6.0 // indirect
)

1
go.sum
View File

@ -67,6 +67,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190520210107-018c4d40a106 h1:EZofHp/BzEf3j39/+7CX1JvH0WaPG+ikBrqAdAPf+GM=

View File

@ -1,6 +1,10 @@
package cmd
import (
"fmt"
"strings"
"github.com/go-shiori/shiori/internal/database"
"github.com/spf13/cobra"
)
@ -12,11 +16,89 @@ func openCmd() *cobra.Command {
"Accepts space-separated list of indices (e.g. 5 6 23 4 110 45), " +
"hyphenated range (e.g. 100-200) or both (e.g. 1-3 7 9). " +
"If no arguments, ALL bookmarks will be opened.",
Run: openHandler,
}
cmd.Flags().BoolP("yes", "y", false, "Skip confirmation prompt and open ALL bookmarks")
cmd.Flags().BoolP("cache", "c", false, "Open the bookmark's cache in text-only mode")
cmd.Flags().Bool("trim-space", false, "Trim all spaces and newlines from the bookmark's cache")
cmd.Flags().BoolP("text-cache", "t", false, "Open the bookmark's text cache in terminal")
return cmd
}
func openHandler(cmd *cobra.Command, args []string) {
// Parse flags
skipConfirm, _ := cmd.Flags().GetBool("yes")
textCacheMode, _ := cmd.Flags().GetBool("text-cache")
// If no arguments (i.e all bookmarks will be opened),
// confirm to user
if len(args) == 0 && !skipConfirm {
confirmOpen := ""
fmt.Print("Open ALL bookmarks? (y/N): ")
fmt.Scanln(&confirmOpen)
if confirmOpen != "y" {
return
}
}
// Convert args to ids
ids, err := parseStrIndices(args)
if err != nil {
cError.Println(err)
return
}
// Read bookmarks from database
getOptions := database.GetBookmarksOptions{
IDs: ids,
WithContent: true,
}
bookmarks, err := DB.GetBookmarks(getOptions)
if err != nil {
cError.Printf("Failed to get bookmarks: %v\n", err)
return
}
if len(bookmarks) == 0 {
switch {
case len(ids) > 0:
cError.Println("No matching index found")
default:
cError.Println("No bookmarks saved yet")
}
return
}
// If not text cache mode, open bookmarks in browser
if !textCacheMode {
for _, book := range bookmarks {
err = openBrowser(book.URL)
if err != nil {
cError.Printf("Failed to open %s: %v\n", book.URL, err)
}
}
return
}
// Show bookmarks content in terminal
termWidth := getTerminalWidth()
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")
} else {
book.Content = strings.Join(strings.Fields(book.Content), " ")
fmt.Println(book.Content)
}
fmt.Println()
cSymbol.Println(strings.Repeat("=", termWidth))
fmt.Println()
}
}

View File

@ -8,13 +8,16 @@ import (
"net/http"
nurl "net/url"
"os"
"os/exec"
fp "path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/fatih/color"
"github.com/go-shiori/shiori/internal/model"
"golang.org/x/crypto/ssh/terminal"
)
var (
@ -169,3 +172,25 @@ func parseStrIndices(indices []string) ([]int, error) {
return listIndex, nil
}
// openBrowser tries to open the URL in a browser,
// and returns any error if it happened.
func openBrowser(url string) error {
var args []string
switch runtime.GOOS {
case "darwin":
args = []string{"open"}
case "windows":
args = []string{"cmd", "/c", "start"}
default:
args = []string{"xdg-open"}
}
cmd := exec.Command(args[0], append(args[1:], url)...)
return cmd.Run()
}
func getTerminalWidth() int {
width, _, _ := terminal.GetSize(int(os.Stdin.Fd()))
return width
}