Skip to content

Commit

Permalink
fix: golangci-lint errors (go-shiori#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmartingr authored Feb 13, 2022
1 parent 2ca628b commit a76b121
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 83 deletions.
4 changes: 3 additions & 1 deletion internal/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func exportHandler(cmd *cobra.Command, args []string) {

// Make sure destination directory exist
dstDir := fp.Dir(args[0])
os.MkdirAll(dstDir, os.ModePerm)
if err := os.MkdirAll(dstDir, os.ModePerm); err != nil {
cError.Printf("Error crating destination directory: %s", err)
}

// Create destination file
dstFile, err := os.Create(args[0])
Expand Down
24 changes: 11 additions & 13 deletions internal/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@ import (

"github.com/fatih/color"
"github.com/go-shiori/shiori/internal/model"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)

var (
cIndex = color.New(color.FgHiCyan)
cSymbol = color.New(color.FgHiMagenta)
cTitle = color.New(color.FgHiGreen).Add(color.Bold)
cReadTime = color.New(color.FgHiMagenta)
cURL = color.New(color.FgHiYellow)
cExcerpt = color.New(color.FgHiWhite)
cTag = color.New(color.FgHiBlue)

cInfo = color.New(color.FgHiCyan)
cError = color.New(color.FgHiRed)
cWarning = color.New(color.FgHiYellow)
cIndex = color.New(color.FgHiCyan)
cSymbol = color.New(color.FgHiMagenta)
cTitle = color.New(color.FgHiGreen).Add(color.Bold)
cURL = color.New(color.FgHiYellow)
cExcerpt = color.New(color.FgHiWhite)
cTag = color.New(color.FgHiBlue)

cInfo = color.New(color.FgHiCyan)
cError = color.New(color.FgHiRed)

errInvalidIndex = errors.New("Index is not valid")
)
Expand Down Expand Up @@ -130,7 +128,7 @@ func openBrowser(url string) error {
}

func getTerminalWidth() int {
width, _, _ := terminal.GetSize(int(os.Stdin.Fd()))
width, _, _ := term.GetSize(int(os.Stdin.Fd()))
return width
}

Expand Down
2 changes: 1 addition & 1 deletion internal/core/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func ProcessBookmark(req ProcessRequest) (model.Bookmark, bool, error) {

nurl, err := url.Parse(book.URL)
if err != nil {
fmt.Errorf("Failed to parse url: %v", err)
return book, true, fmt.Errorf("Failed to parse url: %v", err)
}

article, err := readability.FromReader(readabilityInput, nurl)
Expand Down
32 changes: 24 additions & 8 deletions internal/database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"database/sql"
"fmt"
"log"
"strings"
"time"

Expand Down Expand Up @@ -33,7 +34,9 @@ func OpenMySQLDatabase(connString string) (mysqlDB *MySQLDatabase, err error) {
defer func() {
if r := recover(); r != nil {
panicErr, _ := r.(error)
tx.Rollback()
if err := tx.Rollback(); err != nil {
log.Printf("error during rollback: %s", err)
}

mysqlDB = nil
err = panicErr
Expand Down Expand Up @@ -102,7 +105,9 @@ func (db *MySQLDatabase) SaveBookmarks(bookmarks ...model.Bookmark) (result []mo
defer func() {
if r := recover(); r != nil {
panicErr, _ := r.(error)
tx.Rollback()
if err := tx.Rollback(); err != nil {
log.Printf("error during rollback: %s", err)
}

result = []model.Bookmark{}
err = panicErr
Expand Down Expand Up @@ -192,7 +197,9 @@ func (db *MySQLDatabase) SaveBookmarks(bookmarks ...model.Bookmark) (result []mo
tag.ID = int(tagID64)
}

stmtInsertBookTag.Exec(tag.ID, book.ID)
if _, err := stmtInsertBookTag.Exec(tag.ID, book.ID); err != nil {
log.Printf("error during insert: %s", err)
}
}

newTags = append(newTags, tag)
Expand Down Expand Up @@ -458,7 +465,9 @@ func (db *MySQLDatabase) DeleteBookmarks(ids ...int) (err error) {
defer func() {
if r := recover(); r != nil {
panicErr, _ := r.(error)
tx.Rollback()
if err := tx.Rollback(); err != nil {
log.Printf("error during rollback: %s", err)
}

err = panicErr
}
Expand Down Expand Up @@ -507,7 +516,9 @@ func (db *MySQLDatabase) GetBookmark(id int, url string) (model.Bookmark, bool)
}

book := model.Bookmark{}
db.Get(&book, query, args...)
if err := db.Get(&book, query, args...); err != nil {
log.Printf("error during db.get: %s", err)
}

return book, book.ID != 0
}
Expand Down Expand Up @@ -562,9 +573,12 @@ func (db *MySQLDatabase) GetAccounts(opts GetAccountsOptions) ([]model.Account,
// Returns the account and boolean whether it's exist or not.
func (db *MySQLDatabase) GetAccount(username string) (model.Account, bool) {
account := model.Account{}
db.Get(&account, `SELECT
if err := db.Get(&account, `SELECT
id, username, password, owner FROM account WHERE username = ?`,
username)
username,
); err != nil {
log.Printf("error during db.get: %s", err)
}

return account, account.ID != 0
}
Expand All @@ -581,7 +595,9 @@ func (db *MySQLDatabase) DeleteAccounts(usernames ...string) (err error) {
defer func() {
if r := recover(); r != nil {
panicErr, _ := r.(error)
tx.Rollback()
if err := tx.Rollback(); err != nil {
log.Printf("error during rollback: %s", err)
}

err = panicErr
}
Expand Down
57 changes: 36 additions & 21 deletions internal/database/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"database/sql"
"fmt"
"log"
"strings"
"time"

Expand Down Expand Up @@ -32,8 +33,9 @@ func OpenPGDatabase(connString string) (pgDB *PGDatabase, err error) {
defer func() {
if r := recover(); r != nil {
panicErr, _ := r.(error)
tx.Rollback()

if err := tx.Rollback(); err != nil {
log.Printf("error during rollback: %s", err)
}
pgDB = nil
err = panicErr
}
Expand Down Expand Up @@ -98,7 +100,9 @@ func (db *PGDatabase) SaveBookmarks(bookmarks ...model.Bookmark) (result []model
defer func() {
if r := recover(); r != nil {
panicErr, _ := r.(error)
tx.Rollback()
if err := tx.Rollback(); err != nil {
log.Printf("error during rollback: %s", err)
}

result = []model.Bookmark{}
err = panicErr
Expand Down Expand Up @@ -188,7 +192,9 @@ func (db *PGDatabase) SaveBookmarks(bookmarks ...model.Bookmark) (result []model
tag.ID = int(tagID64)
}

stmtInsertBookTag.Exec(tag.ID, book.ID)
if _, err := stmtInsertBookTag.Exec(tag.ID, book.ID); err != nil {
log.Printf("error during insert: %s", err)
}
}

newTags = append(newTags, tag)
Expand Down Expand Up @@ -237,7 +243,7 @@ func (db *PGDatabase) GetBookmarks(opts GetBookmarksOptions) ([]model.Bookmark,
// Add where clause for search keyword
if opts.Keyword != "" {
query += ` AND (
url LIKE :lkw OR
url LIKE :lkw OR
MATCH(title, excerpt, content) AGAINST (:kw IN BOOLEAN MODE)
)`

Expand Down Expand Up @@ -315,7 +321,8 @@ func (db *PGDatabase) GetBookmarks(opts GetBookmarksOptions) ([]model.Bookmark,
}

// Expand query, because some of the args might be an array
query, args, err := sqlx.Named(query, arg)
var err error
query, args, _ := sqlx.Named(query, arg)
query, args, err = sqlx.In(query, args...)
if err != nil {
return nil, fmt.Errorf("failed to expand query: %v", err)
Expand All @@ -330,10 +337,10 @@ func (db *PGDatabase) GetBookmarks(opts GetBookmarksOptions) ([]model.Bookmark,
}

// Fetch tags for each bookmarks
stmtGetTags, err := db.Preparex(`SELECT t.id, t.name
FROM bookmark_tag bt
stmtGetTags, err := db.Preparex(`SELECT t.id, t.name
FROM bookmark_tag bt
LEFT JOIN tag t ON bt.tag_id = t.id
WHERE bt.bookmark_id = $1
WHERE bt.bookmark_id = $1
ORDER BY t.name`)
if err != nil {
return nil, fmt.Errorf("failed to prepare tag query: %v", err)
Expand Down Expand Up @@ -369,7 +376,7 @@ func (db *PGDatabase) GetBookmarksCount(opts GetBookmarksOptions) (int, error) {
// Add where clause for search keyword
if opts.Keyword != "" {
query += ` AND (
url LIKE :lurl OR
url LIKE :lurl OR
MATCH(title, excerpt, content) AGAINST (:kw IN BOOLEAN MODE)
)`

Expand Down Expand Up @@ -431,7 +438,8 @@ func (db *PGDatabase) GetBookmarksCount(opts GetBookmarksOptions) (int, error) {
}

// Expand query, because some of the args might be an array
query, args, err := sqlx.Named(query, arg)
var err error
query, args, _ := sqlx.Named(query, arg)
query, args, err = sqlx.In(query, args...)
if err != nil {
return 0, fmt.Errorf("failed to expand query: %v", err)
Expand Down Expand Up @@ -460,8 +468,9 @@ func (db *PGDatabase) DeleteBookmarks(ids ...int) (err error) {
defer func() {
if r := recover(); r != nil {
panicErr, _ := r.(error)
tx.Rollback()

if err := tx.Rollback(); err != nil {
log.Printf("error during rollback: %s", err)
}
err = panicErr
}
}()
Expand Down Expand Up @@ -499,7 +508,7 @@ func (db *PGDatabase) DeleteBookmarks(ids ...int) (err error) {
func (db *PGDatabase) GetBookmark(id int, url string) (model.Bookmark, bool) {
args := []interface{}{id}
query := `SELECT
id, url, title, excerpt, author, public,
id, url, title, excerpt, author, public,
content, html, modified, content <> '' has_content
FROM bookmark WHERE id = $1`

Expand All @@ -509,7 +518,9 @@ func (db *PGDatabase) GetBookmark(id int, url string) (model.Bookmark, bool) {
}

book := model.Bookmark{}
db.Get(&book, query, args...)
if err := db.Get(&book, query, args...); err != nil {
log.Printf("error during db.get: %s", err)
}

return book, book.ID != 0
}
Expand Down Expand Up @@ -564,9 +575,12 @@ func (db *PGDatabase) GetAccounts(opts GetAccountsOptions) ([]model.Account, err
// Returns the account and boolean whether it's exist or not.
func (db *PGDatabase) GetAccount(username string) (model.Account, bool) {
account := model.Account{}
db.Get(&account, `SELECT
if err := db.Get(&account, `SELECT
id, username, password, owner FROM account WHERE username = $1`,
username)
username,
); err != nil {
log.Printf("error during db.get: %s", err)
}

return account, account.ID != 0
}
Expand All @@ -583,8 +597,9 @@ func (db *PGDatabase) DeleteAccounts(usernames ...string) (err error) {
defer func() {
if r := recover(); r != nil {
panicErr, _ := r.(error)
tx.Rollback()

if err := tx.Rollback(); err != nil {
log.Printf("error during rollback: %s", err)
}
err = panicErr
}
}()
Expand All @@ -605,8 +620,8 @@ func (db *PGDatabase) DeleteAccounts(usernames ...string) (err error) {
// GetTags fetch list of tags and their frequency.
func (db *PGDatabase) GetTags() ([]model.Tag, error) {
tags := []model.Tag{}
query := `SELECT bt.tag_id id, t.name, COUNT(bt.tag_id) n_bookmarks
FROM bookmark_tag bt
query := `SELECT bt.tag_id id, t.name, COUNT(bt.tag_id) n_bookmarks
FROM bookmark_tag bt
LEFT JOIN tag t ON bt.tag_id = t.id
GROUP BY bt.tag_id, t.name ORDER BY t.name`

Expand Down
Loading

0 comments on commit a76b121

Please sign in to comment.