From 168b623985c6e5bb1e7f39cafcf27a8ef0cb16ef Mon Sep 17 00:00:00 2001 From: Sascha Date: Thu, 1 Mar 2018 10:51:30 +0100 Subject: [PATCH 01/14] feat(database): make path to database configurable --- cmd/root.go | 16 ++++++++++++++-- database/sqlite.go | 4 ++-- main.go | 14 +------------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 6030f835..9e004d6d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,14 +2,15 @@ package cmd import ( "fmt" - "github.com/RadhiFadlillah/shiori/database" "github.com/spf13/cobra" "os" + db "github.com/RadhiFadlillah/shiori/database" + _ "github.com/mattn/go-sqlite3" ) var ( // DB is database that used by this cli - DB database.Database + DB db.Database rootCmd = &cobra.Command{ Use: "shiori", @@ -25,3 +26,14 @@ func Execute() { os.Exit(1) } } + +func init () { + databasePath := "shiori.db" + if value, found := os.LookupEnv("ENV_SHIORI_DB"); found { + databasePath = value + } + sqliteDB, err := db.OpenSQLiteDatabase(databasePath) + checkError(err) + + DB = sqliteDB +} \ No newline at end of file diff --git a/database/sqlite.go b/database/sqlite.go index 21baeda8..77511bd5 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -18,10 +18,10 @@ type SQLiteDatabase struct { } // OpenSQLiteDatabase creates and open connection to new SQLite3 database. -func OpenSQLiteDatabase() (*SQLiteDatabase, error) { +func OpenSQLiteDatabase(databasePath string) (*SQLiteDatabase, error) { // Open database and start transaction var err error - db := sqlx.MustConnect("sqlite3", "shiori.db") + db := sqlx.MustConnect("sqlite3", databasePath) tx := db.MustBegin() // Make sure to rollback if panic ever happened diff --git a/main.go b/main.go index c44a6aad..54181ae3 100644 --- a/main.go +++ b/main.go @@ -3,20 +3,8 @@ package main import ( "github.com/RadhiFadlillah/shiori/cmd" - db "github.com/RadhiFadlillah/shiori/database" - _ "github.com/mattn/go-sqlite3" ) func main() { - sqliteDB, err := db.OpenSQLiteDatabase() - checkError(err) - - cmd.DB = sqliteDB cmd.Execute() -} - -func checkError(err error) { - if err != nil { - panic(err) - } -} +} \ No newline at end of file From 1ae90b74593e0db68ef65da14a19808ed63ff972 Mon Sep 17 00:00:00 2001 From: Sascha Date: Thu, 1 Mar 2018 11:01:06 +0100 Subject: [PATCH 02/14] docs: document environment variable --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 8fa412e9..20fed7ac 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,10 @@ You can download the latest version of `shiori` from the release page, then put go get github.com/RadhiFadlillah/go-readability ``` +### Advanced + +You can provide a path to the created SQLite database. To do this, set the environment variable `ENV_SHIORI_DB` to the desired path. + ## Usage ``` From 8661b4e870e1be107b8fe28b15e7b1b8e698e87e Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Sat, 3 Mar 2018 16:50:54 +0700 Subject: [PATCH 03/14] Use goimports on all file --- cmd/utils.go | 3 ++- database/database.go | 1 + database/sqlite.go | 7 ++++--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/utils.go b/cmd/utils.go index f1bbc6fe..2eea37bb 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -1,9 +1,10 @@ package cmd import ( + "os" + "github.com/fatih/color" "golang.org/x/crypto/ssh/terminal" - "os" ) var ( diff --git a/database/database.go b/database/database.go index 675d3f4d..4722f229 100644 --- a/database/database.go +++ b/database/database.go @@ -2,6 +2,7 @@ package database import ( "database/sql" + "github.com/RadhiFadlillah/shiori/model" ) diff --git a/database/sqlite.go b/database/sqlite.go index 21baeda8..b8bc89b5 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -3,13 +3,14 @@ package database import ( "database/sql" "fmt" - "github.com/RadhiFadlillah/shiori/model" - "github.com/jmoiron/sqlx" - "golang.org/x/crypto/bcrypt" "sort" "strconv" "strings" "time" + + "github.com/RadhiFadlillah/shiori/model" + "github.com/jmoiron/sqlx" + "golang.org/x/crypto/bcrypt" ) // SQLiteDatabase is implementation of Database interface for connecting to SQLite3 database. From 837c4701760dc479be7e0b4b7dabe2f1408b1be0 Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Sat, 3 Mar 2018 17:02:24 +0700 Subject: [PATCH 04/14] Fix: tags not imported --- cmd/import.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/cmd/import.go b/cmd/import.go index 9f1895a9..4f38cb13 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -41,15 +41,6 @@ func init() { rootCmd.AddCommand(importCmd) } -func printTagName(s *goquery.Selection) string { - tags := []string{} - for _, nd := range s.Nodes { - tags = append(tags, nd.Data) - } - - return strings.Join(tags, ",") -} - func importBookmarks(pth string, generateTag bool) error { // Open file srcFile, err := os.Open(pth) @@ -74,10 +65,17 @@ func importBookmarks(pth string, generateTag bool) error { // Get metadata title := a.Text() url, _ := a.Attr("href") + strTags, _ := a.Attr("tags") strModified, _ := a.Attr("last_modified") intModified, _ := strconv.ParseInt(strModified, 10, 64) modified := time.Unix(intModified, 0) + // Get bookmark tags + tags := []model.Tag{} + for _, strTag := range strings.Split(strTags, ",") { + tags = append(tags, model.Tag{Name: strTag}) + } + // Get bookmark excerpt excerpt := "" if dd := dt.Next(); dd.Is("dd") { @@ -85,6 +83,7 @@ func importBookmarks(pth string, generateTag bool) error { } // Get category name for this bookmark + // and add it as tags (if necessary) category := "" if dtCategory := dl.Prev(); dtCategory.Is("h3") { category = dtCategory.Text() @@ -93,9 +92,8 @@ func importBookmarks(pth string, generateTag bool) error { category = strings.Replace(category, " ", "-", -1) } - tags := []model.Tag{} if category != "" && generateTag { - tags = []model.Tag{{Name: category}} + tags = append(tags, model.Tag{Name: category}) } // Add item to list From da03aff6303c9dff2a7b71de8dc4997c53f070bf Mon Sep 17 00:00:00 2001 From: Damir Dulic Date: Sat, 3 Mar 2018 15:33:29 +0100 Subject: [PATCH 05/14] add travis ci --- .travis.yml | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..28b394a2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,39 @@ +language: go +sudo: false +matrix: + include: + - go: 1.x + env: LATEST=true + - go: 1.9 + - go: 1.8 + +before_install: + - go get github.com/mitchellh/gox # go tool for cross compiling + - go get github.com/inconshreveable/mousetrap # needed for windows builds + +install: + - go get github.com/RadhiFadlillah/shiori + +script: + - go get -t -v ./... + - diff -u <(echo -n) <(gofmt -d .) + - go vet $(go list ./... | grep -v /vendor/) + - go test -v -race ./... + # only build binaries from the latest Go release. + - if [ "${LATEST}" = "true" ]; then gox -os="linux darwin windows" -arch="amd64" -output "shiori_{{.OS}}_{{.Arch}}" -ldflags "-X main.Rev=`git rev-parse --short HEAD`" -verbose ./...; fi + +deploy: + provider: releases + skip_cleanup: true + api_key: + secure: $TOKEN + file: + - shiori_windows_amd64.exe + - shiori_darwin_amd64 + - shiori_linux_amd64 + on: + tags: true + branches: + only: + - master + condition: $LATEST = true From 222ff0371e777ec5601212ab300cc2e9bdaa5526 Mon Sep 17 00:00:00 2001 From: Andrey Vasnetsov Date: Sun, 4 Mar 2018 17:56:00 +0300 Subject: [PATCH 06/14] Change module path to relative, add check for empty tag on import --- cmd/add.go | 2 +- cmd/export.go | 2 +- cmd/import.go | 6 ++++-- cmd/print.go | 2 +- cmd/root.go | 2 +- cmd/serve.go | 4 ++-- cmd/update.go | 2 +- database/database.go | 2 +- database/sqlite.go | 2 +- main.go | 4 ++-- 10 files changed, 15 insertions(+), 13 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 94735e53..e1394667 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -5,8 +5,8 @@ import ( "strings" "time" + "../model" "github.com/RadhiFadlillah/go-readability" - "github.com/RadhiFadlillah/shiori/model" "github.com/spf13/cobra" ) diff --git a/cmd/export.go b/cmd/export.go index 050da8fa..1fe72555 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -7,7 +7,7 @@ import ( "strings" "time" - "github.com/RadhiFadlillah/shiori/model" + "../model" "github.com/spf13/cobra" ) diff --git a/cmd/import.go b/cmd/import.go index 4f38cb13..6775ec2b 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -7,8 +7,8 @@ import ( "strings" "time" + "../model" "github.com/PuerkitoBio/goquery" - "github.com/RadhiFadlillah/shiori/model" "github.com/spf13/cobra" ) @@ -73,7 +73,9 @@ func importBookmarks(pth string, generateTag bool) error { // Get bookmark tags tags := []model.Tag{} for _, strTag := range strings.Split(strTags, ",") { - tags = append(tags, model.Tag{Name: strTag}) + if strTag != "" { + tags = append(tags, model.Tag{Name: strTag}) + } } // Get bookmark excerpt diff --git a/cmd/print.go b/cmd/print.go index 407e396c..725498cd 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -6,7 +6,7 @@ import ( "os" "strings" - "github.com/RadhiFadlillah/shiori/model" + "../model" "github.com/spf13/cobra" ) diff --git a/cmd/root.go b/cmd/root.go index 51121593..b6e502dc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,7 +4,7 @@ import ( "fmt" "os" - "github.com/RadhiFadlillah/shiori/database" + "../database" "github.com/spf13/cobra" ) diff --git a/cmd/serve.go b/cmd/serve.go index e321dbbe..198d5fd5 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -13,8 +13,8 @@ import ( "strings" "time" - "github.com/RadhiFadlillah/shiori/assets" - "github.com/RadhiFadlillah/shiori/model" + "../assets" + "../model" "github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go/request" "github.com/julienschmidt/httprouter" diff --git a/cmd/update.go b/cmd/update.go index 3dbf5702..b3f74a8b 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -8,8 +8,8 @@ import ( "sync" "time" + "../model" "github.com/RadhiFadlillah/go-readability" - "github.com/RadhiFadlillah/shiori/model" "github.com/spf13/cobra" ) diff --git a/database/database.go b/database/database.go index 4722f229..14c720b6 100644 --- a/database/database.go +++ b/database/database.go @@ -3,7 +3,7 @@ package database import ( "database/sql" - "github.com/RadhiFadlillah/shiori/model" + "../model" ) // Database is interface for manipulating data in database. diff --git a/database/sqlite.go b/database/sqlite.go index b8bc89b5..bf348e43 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "github.com/RadhiFadlillah/shiori/model" + "../model" "github.com/jmoiron/sqlx" "golang.org/x/crypto/bcrypt" ) diff --git a/main.go b/main.go index c44a6aad..324669ac 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,8 @@ package main import ( - "github.com/RadhiFadlillah/shiori/cmd" - db "github.com/RadhiFadlillah/shiori/database" + "./cmd" + db "./database" _ "github.com/mattn/go-sqlite3" ) From c208894bb6e9baa60a803ced27395ae9bc337c3e Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Mon, 5 Mar 2018 07:14:58 +0700 Subject: [PATCH 07/14] Move opening database to main file --- cmd/root.go | 21 +++++---------------- main.go | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 9e004d6d..51121593 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,19 +2,19 @@ package cmd import ( "fmt" - "github.com/spf13/cobra" "os" - db "github.com/RadhiFadlillah/shiori/database" - _ "github.com/mattn/go-sqlite3" + + "github.com/RadhiFadlillah/shiori/database" + "github.com/spf13/cobra" ) var ( // DB is database that used by this cli - DB db.Database + DB database.Database rootCmd = &cobra.Command{ Use: "shiori", - Short: "Simple command-line bookmark manager built with Go.", + Short: "Simple command-line bookmark manager built with Go", } ) @@ -26,14 +26,3 @@ func Execute() { os.Exit(1) } } - -func init () { - databasePath := "shiori.db" - if value, found := os.LookupEnv("ENV_SHIORI_DB"); found { - databasePath = value - } - sqliteDB, err := db.OpenSQLiteDatabase(databasePath) - checkError(err) - - DB = sqliteDB -} \ No newline at end of file diff --git a/main.go b/main.go index 54181ae3..cdff3834 100644 --- a/main.go +++ b/main.go @@ -2,9 +2,28 @@ package main import ( + "os" + "github.com/RadhiFadlillah/shiori/cmd" + db "github.com/RadhiFadlillah/shiori/database" + _ "github.com/mattn/go-sqlite3" ) func main() { + databasePath := "shiori.db" + if value, found := os.LookupEnv("ENV_SHIORI_DB"); found { + databasePath = value + } + + sqliteDB, err := db.OpenSQLiteDatabase(databasePath) + checkError(err) + + cmd.DB = sqliteDB cmd.Execute() -} \ No newline at end of file +} + +func checkError(err error) { + if err != nil { + panic(err) + } +} From 4f2cd21ddaef8bb69859c416052208350ff4ca24 Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Mon, 5 Mar 2018 07:21:05 +0700 Subject: [PATCH 08/14] Update readme --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 20fed7ac..4a169a0d 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ Shiori is a simple bookmarks manager written in Go language. Intended as a simpl - [Features](#features) - [Installation](#installation) - [Usage](#usage) +- [Advanced](#advanced) - [Examples](#examples) - [License](#license) @@ -30,10 +31,6 @@ You can download the latest version of `shiori` from the release page, then put go get github.com/RadhiFadlillah/go-readability ``` -### Advanced - -You can provide a path to the created SQLite database. To do this, set the environment variable `ENV_SHIORI_DB` to the desired path. - ## Usage ``` @@ -61,6 +58,10 @@ Flags: Use "shiori [command] --help" for more information about a command. ``` +### Advanced + +By default, `shiori` will create database in the location where you run it. For example, if you run `shiori`. To set the database to a specific location, you can set the environment variable `ENV_SHIORI_DB` to your desired path. + ## Examples 1. Save new bookmark with tags "nature" and "climate-change". From 50238362bddc734ae3e44584e2a80340ddb2e6d1 Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Mon, 5 Mar 2018 07:55:16 +0700 Subject: [PATCH 09/14] Revert relative import path to absolute path --- cmd/add.go | 2 +- cmd/export.go | 2 +- cmd/import.go | 2 +- cmd/print.go | 2 +- cmd/root.go | 2 +- cmd/serve.go | 4 ++-- cmd/update.go | 2 +- database/database.go | 2 +- database/sqlite.go | 2 +- main.go | 4 ++-- 10 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index e1394667..94735e53 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -5,8 +5,8 @@ import ( "strings" "time" - "../model" "github.com/RadhiFadlillah/go-readability" + "github.com/RadhiFadlillah/shiori/model" "github.com/spf13/cobra" ) diff --git a/cmd/export.go b/cmd/export.go index 1fe72555..050da8fa 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -7,7 +7,7 @@ import ( "strings" "time" - "../model" + "github.com/RadhiFadlillah/shiori/model" "github.com/spf13/cobra" ) diff --git a/cmd/import.go b/cmd/import.go index 6775ec2b..3f21f786 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -7,8 +7,8 @@ import ( "strings" "time" - "../model" "github.com/PuerkitoBio/goquery" + "github.com/RadhiFadlillah/shiori/model" "github.com/spf13/cobra" ) diff --git a/cmd/print.go b/cmd/print.go index 725498cd..407e396c 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -6,7 +6,7 @@ import ( "os" "strings" - "../model" + "github.com/RadhiFadlillah/shiori/model" "github.com/spf13/cobra" ) diff --git a/cmd/root.go b/cmd/root.go index b6e502dc..51121593 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,7 +4,7 @@ import ( "fmt" "os" - "../database" + "github.com/RadhiFadlillah/shiori/database" "github.com/spf13/cobra" ) diff --git a/cmd/serve.go b/cmd/serve.go index 198d5fd5..e321dbbe 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -13,8 +13,8 @@ import ( "strings" "time" - "../assets" - "../model" + "github.com/RadhiFadlillah/shiori/assets" + "github.com/RadhiFadlillah/shiori/model" "github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go/request" "github.com/julienschmidt/httprouter" diff --git a/cmd/update.go b/cmd/update.go index b3f74a8b..3dbf5702 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -8,8 +8,8 @@ import ( "sync" "time" - "../model" "github.com/RadhiFadlillah/go-readability" + "github.com/RadhiFadlillah/shiori/model" "github.com/spf13/cobra" ) diff --git a/database/database.go b/database/database.go index 14c720b6..4722f229 100644 --- a/database/database.go +++ b/database/database.go @@ -3,7 +3,7 @@ package database import ( "database/sql" - "../model" + "github.com/RadhiFadlillah/shiori/model" ) // Database is interface for manipulating data in database. diff --git a/database/sqlite.go b/database/sqlite.go index bf348e43..b8bc89b5 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "../model" + "github.com/RadhiFadlillah/shiori/model" "github.com/jmoiron/sqlx" "golang.org/x/crypto/bcrypt" ) diff --git a/main.go b/main.go index 324669ac..c44a6aad 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,8 @@ package main import ( - "./cmd" - db "./database" + "github.com/RadhiFadlillah/shiori/cmd" + db "github.com/RadhiFadlillah/shiori/database" _ "github.com/mattn/go-sqlite3" ) From 46c88d5db5d44a2648e22016bd4cddf795524363 Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Mon, 5 Mar 2018 11:02:36 +0700 Subject: [PATCH 10/14] Stop changing index after delete --- cmd/delete.go | 8 +----- cmd/serve.go | 2 +- database/database.go | 2 +- database/sqlite.go | 63 +++++--------------------------------------- 4 files changed, 9 insertions(+), 66 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index 00b98db9..2aae6c6b 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -32,17 +32,11 @@ var ( } // Delete bookmarks from database - oldIndices, newIndices, err := DB.DeleteBookmarks(args...) + err := DB.DeleteBookmarks(args...) if err != nil { cError.Println(err) os.Exit(1) } - - fmt.Println("Bookmarks has been deleted") - for i, oldIndex := range oldIndices { - newIndex := newIndices[i] - fmt.Printf("Index %d moved to %d\n", oldIndex, newIndex) - } }, } ) diff --git a/cmd/serve.go b/cmd/serve.go index e321dbbe..b7791708 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -266,7 +266,7 @@ func apiDeleteBookmarks(w http.ResponseWriter, r *http.Request, ps httprouter.Pa checkError(err) // Delete bookmarks - _, _, err = DB.DeleteBookmarks(request...) + err = DB.DeleteBookmarks(request...) checkError(err) fmt.Fprint(w, request) diff --git a/database/database.go b/database/database.go index 4722f229..e876a875 100644 --- a/database/database.go +++ b/database/database.go @@ -15,7 +15,7 @@ type Database interface { GetBookmarks(withContent bool, indices ...string) ([]model.Bookmark, error) // DeleteBookmarks removes all record with matching indices from database. - DeleteBookmarks(indices ...string) ([]int, []int, error) + DeleteBookmarks(indices ...string) error // SearchBookmarks search bookmarks by the keyword or tags. SearchBookmarks(orderLatest bool, keyword string, tags ...string) ([]model.Bookmark, error) diff --git a/database/sqlite.go b/database/sqlite.go index 5b71a440..94f99cf5 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -3,7 +3,6 @@ package database import ( "database/sql" "fmt" - "sort" "strconv" "strings" "time" @@ -263,7 +262,7 @@ func (db *SQLiteDatabase) GetBookmarks(withContent bool, indices ...string) ([]m } // DeleteBookmarks removes all record with matching indices from database. -func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newIndices []int, err error) { +func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (err error) { // Convert list of index to int listIndex := []int{} errInvalidIndex := fmt.Errorf("Index is not valid") @@ -272,13 +271,13 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd if strings.Contains(strIndex, "-") { parts := strings.Split(strIndex, "-") if len(parts) != 2 { - return nil, nil, errInvalidIndex + return errInvalidIndex } minIndex, errMin := strconv.Atoi(parts[0]) maxIndex, errMax := strconv.Atoi(parts[1]) if errMin != nil || errMax != nil || minIndex < 1 || minIndex > maxIndex { - return nil, nil, errInvalidIndex + return errInvalidIndex } for i := minIndex; i <= maxIndex; i++ { @@ -287,16 +286,13 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd } else { index, err := strconv.Atoi(strIndex) if err != nil || index < 1 { - return nil, nil, errInvalidIndex + return errInvalidIndex } listIndex = append(listIndex, index) } } - // Sort the index - sort.Ints(listIndex) - // Create args and where clause args := []interface{}{} whereClause := " WHERE 1" @@ -315,7 +311,7 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd // Begin transaction tx, err := db.Beginx() if err != nil { - return nil, nil, errInvalidIndex + return errInvalidIndex } // Make sure to rollback if panic ever happened @@ -324,8 +320,6 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd panicErr, _ := r.(error) tx.Rollback() - oldIndices = nil - newIndices = nil err = panicErr } }() @@ -338,56 +332,11 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd tx.MustExec("DELETE FROM bookmark_tag "+whereTagClause, args...) tx.MustExec("DELETE FROM bookmark_content "+whereContentClause, args...) - // Prepare statement for updating index - stmtGetMaxID, err := tx.Preparex(`SELECT IFNULL(MAX(id), 0) FROM bookmark`) - checkError(err) - - stmtUpdateBookmark, err := tx.Preparex(`UPDATE bookmark SET id = ? WHERE id = ?`) - checkError(err) - - stmtUpdateBookmarkTag, err := tx.Preparex(`UPDATE bookmark_tag SET bookmark_id = ? WHERE bookmark_id = ?`) - checkError(err) - - stmtUpdateBookmarkContent, err := tx.Preparex(`UPDATE bookmark_content SET docid = ? WHERE docid = ?`) - checkError(err) - - // Get list of removed indices - maxIndex := 0 - err = stmtGetMaxID.Get(&maxIndex) - checkError(err) - - removedIndices := []int{} - err = tx.Select(&removedIndices, - `WITH cnt(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM cnt LIMIT ?) - SELECT x FROM cnt WHERE x NOT IN (SELECT id FROM bookmark)`, - maxIndex) - checkError(err) - - // Fill removed indices - newIndices = []int{} - oldIndices = []int{} - for _, removedIndex := range removedIndices { - oldIndex := 0 - err = stmtGetMaxID.Get(&oldIndex) - checkError(err) - - if oldIndex <= removedIndex { - break - } - - stmtUpdateBookmark.MustExec(removedIndex, oldIndex) - stmtUpdateBookmarkTag.MustExec(removedIndex, oldIndex) - stmtUpdateBookmarkContent.MustExec(removedIndex, oldIndex) - - newIndices = append(newIndices, removedIndex) - oldIndices = append(oldIndices, oldIndex) - } - // Commit transaction err = tx.Commit() checkError(err) - return oldIndices, newIndices, err + return err } // SearchBookmarks search bookmarks by the keyword or tags. From 5b33b7f50fbd8408b9d467d093de871c7feaf900 Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Mon, 5 Mar 2018 11:11:16 +0700 Subject: [PATCH 11/14] Remove unneeded imports --- cmd/delete.go | 2 -- cmd/print.go | 7 +++---- cmd/root.go | 2 -- cmd/search.go | 7 +++---- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index 2aae6c6b..6cb05e0a 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "os" "github.com/spf13/cobra" ) @@ -35,7 +34,6 @@ var ( err := DB.DeleteBookmarks(args...) if err != nil { cError.Println(err) - os.Exit(1) } }, } diff --git a/cmd/print.go b/cmd/print.go index 407e396c..1ae47235 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -3,7 +3,6 @@ package cmd import ( "encoding/json" "fmt" - "os" "strings" "github.com/RadhiFadlillah/shiori/model" @@ -26,7 +25,7 @@ var ( bookmarks, err := DB.GetBookmarks(false, args...) if err != nil { cError.Println(err) - os.Exit(1) + return } if len(bookmarks) == 0 { @@ -36,7 +35,7 @@ var ( cError.Println("No bookmarks saved yet") } - os.Exit(1) + return } // Print data @@ -44,7 +43,7 @@ var ( bt, err := json.MarshalIndent(&bookmarks, "", " ") if err != nil { cError.Println(err) - os.Exit(1) + return } fmt.Println(string(bt)) } else if indexOnly { diff --git a/cmd/root.go b/cmd/root.go index 51121593..d915cc56 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "os" "github.com/RadhiFadlillah/shiori/database" "github.com/spf13/cobra" @@ -23,6 +22,5 @@ var ( func Execute() { if err := rootCmd.Execute(); err != nil { fmt.Println(err) - os.Exit(1) } } diff --git a/cmd/search.go b/cmd/search.go index 208b89b1..35d0bbf9 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -3,7 +3,6 @@ package cmd import ( "encoding/json" "fmt" - "os" "github.com/spf13/cobra" ) @@ -34,12 +33,12 @@ var ( bookmarks, err := DB.SearchBookmarks(false, keyword, tags...) if err != nil { cError.Println(err) - os.Exit(1) + return } if len(bookmarks) == 0 { cError.Println("No matching bookmarks found") - os.Exit(1) + return } // Print data @@ -47,7 +46,7 @@ var ( bt, err := json.MarshalIndent(&bookmarks, "", " ") if err != nil { cError.Println(err) - os.Exit(1) + return } fmt.Println(string(bt)) } else if indexOnly { From d98f89ed63b42bd0e5fc6101f2a35bfa6c5e3d75 Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Mon, 5 Mar 2018 11:32:37 +0700 Subject: [PATCH 12/14] Add cross platform method for opening browser --- cmd/open.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cmd/open.go b/cmd/open.go index 6dc94ab5..f2ab3e38 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os/exec" + "runtime" "strings" "github.com/spf13/cobra" @@ -67,7 +68,7 @@ func openBookmarks(args ...string) { // Open in browser for _, book := range bookmarks { - exec.Command("xdg-open", book.URL).Run() + err = openBrowser(book.URL) if err != nil { cError.Printf("Failed to open %s: %v\n", book.URL, err) } @@ -110,3 +111,20 @@ func openBookmarksCache(trimSpace bool, args ...string) { fmt.Println() } } + +// openBrowser tries to open the URL in a browser, +// and returns whether it succeed in doing so. +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() +} From 8ebe093efd28f4dee70d0af6c4573f248c4d7f66 Mon Sep 17 00:00:00 2001 From: Radhi Date: Mon, 5 Mar 2018 14:37:50 +0700 Subject: [PATCH 13/14] Add badges to Readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index bf80d163..2c01adbe 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # Shiori +[![Travis CI](https://travis-ci.org/RadhiFadlillah/shiori.svg?branch=master)](https://travis-ci.org/RadhiFadlillah/shiori) +[![Go Report Card](https://goreportcard.com/badge/github.com/radhifadlillah/shiori)](https://goreportcard.com/report/github.com/radhifadlillah/shiori) + Shiori is a simple bookmarks manager written in Go language. Intended as a simple clone of [Pocket](https://getpocket.com//). You can use it as command line application or as web application. This application is distributed as a single binary, which means it can be installed and used easily. ![Screenshot](https://raw.githubusercontent.com/RadhiFadlillah/shiori/master/screenshot.png) From d16737954621485f93c0597794792be67adf2244 Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Mon, 5 Mar 2018 21:35:01 +0700 Subject: [PATCH 14/14] Clear UTM parameters from URL --- cmd/add.go | 26 ++++++++++++++++++++++++++ cmd/update.go | 7 +++++++ 2 files changed, 33 insertions(+) diff --git a/cmd/add.go b/cmd/add.go index 94735e53..0497b17e 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -2,6 +2,7 @@ package cmd import ( "html/template" + nurl "net/url" "strings" "time" @@ -59,6 +60,12 @@ func addBookmark(base model.Bookmark, offline bool) (book model.Bookmark, err er // Prepare initial result book = base + // Clear UTM parameters from URL + book.URL, err = clearUTMParams(book.URL) + if err != nil { + return book, err + } + // Fetch data from internet if !offline { article, err := readability.Parse(book.URL, 10*time.Second) @@ -94,3 +101,22 @@ func addBookmark(base model.Bookmark, offline bool) (book model.Bookmark, err er func normalizeSpace(str string) string { return strings.Join(strings.Fields(str), " ") } + +func clearUTMParams(uri string) (string, error) { + tempURL, err := nurl.Parse(uri) + if err != nil { + return "", err + } + + newQuery := nurl.Values{} + for key, value := range tempURL.Query() { + if strings.HasPrefix(key, "utm_") { + continue + } + + newQuery[key] = value + } + + tempURL.RawQuery = newQuery.Encode() + return tempURL.String(), nil +} diff --git a/cmd/update.go b/cmd/update.go index 3dbf5702..75baba74 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -82,6 +82,13 @@ func init() { func updateBookmarks(indices []string, url, title, excerpt string, tags []string, offline bool) ([]model.Bookmark, error) { mutex := sync.Mutex{} + + // Clear UTM parameters from URL + url, err := clearUTMParams(url) + if err != nil { + return []model.Bookmark{}, err + } + // Read bookmarks from database bookmarks, err := DB.GetBookmarks(true, indices...) if err != nil {