Skip to content

Commit

Permalink
Merge pull request #4 from konoui/dev
Browse files Browse the repository at this point in the history
update browser icon
  • Loading branch information
oui authored Jan 27, 2020
2 parents a865a80 + e68ee19 commit 63cbdbe
Show file tree
Hide file tree
Showing 21 changed files with 242 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ linters:
- lll
- maligned
- goconst
# - gocritic
- gocritic
disable-all: true
run:
skip-dirs:
Expand Down
Binary file added assets/chrome.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/firefox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 14 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func NewRootCmd() *cobra.Command {
const (
emptySsubtitle = "There are no resources"
emptyTitle = "No matching"
firefoxImage = "firefox.png"
chromeImage = "chrome.png"
)

func run(query string) error {
Expand All @@ -54,7 +56,7 @@ func run(query string) error {

c, err := newConfig()
if err != nil {
awf.Fatal(fmt.Sprintf("an error occurs: %s", err), "")
awf.Fatal("fatal error occurs", err.Error())
return err
}

Expand All @@ -79,7 +81,7 @@ func run(query string) error {

bookmarks, err := browsers.Bookmarks()
if err != nil {
awf.Fatal(fmt.Sprintf("an error occurs: %s", err), "")
awf.Fatal("fatal error occurs", err.Error())
return err
}

Expand All @@ -91,12 +93,20 @@ func run(query string) error {
}

for _, b := range bookmarks {
subtitle := fmt.Sprintf("[%s] %s", b.Folder, b.Domain)
var image string
if b.Browser == bookmarker.Firefox {
image = firefoxImage
} else {
image = chromeImage
}
awf.Append(alfred.Item{
Title: b.Title,
Subtitle: subtitle,
Subtitle: fmt.Sprintf("[%s] %s", b.Folder, b.Domain),
Autocomplete: b.Title,
Arg: b.URI,
Icon: &alfred.Icon{
Path: image,
},
})
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func newConfig() (*Config, error) {
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/")

// Set Default Value this is overwritten with config file
// Set Default Value overwritten with config file
viper.SetDefault("firefox.profile", "default")
viper.SetDefault("chrome.profile", "default")
if err := viper.ReadInConfig(); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ func TestNewConfig(t *testing.T) {
want *Config
}{
{
description: "all enable",
description: "enable all settings",
want: &Config{
RemoveDuplicate: true,
MaxCacheAge: -1,
// disable cache
MaxCacheAge: -1,
Firefox: Firefox{
Enable: true,
Profile: "default",
Expand Down
24 changes: 24 additions & 0 deletions cmd/test-rm-duplicate-firefox-chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,72 @@
"title": "Yahoo",
"subtitle": "[/1-hierarchy-b] www.yahoo.com",
"arg": "https://www.yahoo.com/",
"icon": {
"path": "chrome.png"
},
"autocomplete": "Yahoo"
},
{
"title": "Amazon Web Services",
"subtitle": "[/1-hierarchy-a/2-hierarchy-a/3-hierarchy-a] aws.amazon.com",
"arg": "https://aws.amazon.com/?nc1=h_ls",
"icon": {
"path": "chrome.png"
},
"autocomplete": "Amazon Web Services"
},
{
"title": "Stack Overflow",
"subtitle": "[/1-hierarchy-a/2-hierarchy-a/3-hierarchy-a] stackoverflow.com",
"arg": "https://stackoverflow.com/",
"icon": {
"path": "chrome.png"
},
"autocomplete": "Stack Overflow"
},
{
"title": "GitHub",
"subtitle": "[/1-hierarchy-a] github.com",
"arg": "https://github.com/",
"icon": {
"path": "chrome.png"
},
"autocomplete": "GitHub"
},
{
"title": "Amazon.com",
"subtitle": "[/1-hierarchy-b/2-hierarchy-b] www.amazon.com",
"arg": "https://www.amazon.com/",
"icon": {
"path": "chrome.png"
},
"autocomplete": "Amazon.com"
},
{
"title": "Twitter",
"subtitle": "[/1-hierarchy-b/2-hierarchy-a] twitter.com",
"arg": "https://twitter.com/login",
"icon": {
"path": "chrome.png"
},
"autocomplete": "Twitter"
},
{
"title": "Facebook",
"subtitle": "[/1-hierarchy-b/2-hierarchy-a] www.facebook.com",
"arg": "https://www.facebook.com/",
"icon": {
"path": "chrome.png"
},
"autocomplete": "Facebook"
},
{
"title": "Google",
"subtitle": "[] www.google.com",
"arg": "https://www.google.com/",
"icon": {
"path": "chrome.png"
},
"autocomplete": "Google"
}
]
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/mattn/go-shellwords v1.0.6
github.com/mitchellh/go-homedir v1.1.0
github.com/pierrec/lz4 v2.4.0+incompatible
github.com/pkg/errors v0.8.1
github.com/sahilm/fuzzy v0.1.0
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.6.1
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ github.com/pierrec/lz4 v2.4.0+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi
github.com/pierrec/lz4/v3 v3.2.0 h1:fJ8m6yRyzvcoiKHOAXm8uZntcmiC5pPNA0iPvBtXMh4=
github.com/pierrec/lz4/v3 v3.2.0/go.mod h1:280XNCGS8jAcG++AHdd6SeWnzyJ1w9oow2vbORyey8Q=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand Down
13 changes: 7 additions & 6 deletions pkg/bookmarker/bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

// Bookmark abstract each browser bookmark as the structure
type Bookmark struct {
Browser string // Source Browser
Folder string // Folder of Bookmarks
Title string // Bookmark title
Domain string // Domain of URI
URI string // Bookmark URI
Browser Browser
Folder string
Title string
Domain string
URI string
}

// Bookmarker is interface to load each bookmark file
Expand Down Expand Up @@ -54,6 +54,7 @@ func (b Bookmarks) Unmarshal(jsonData []byte) error {
return json.Unmarshal(jsonData, &b)
}

// String retrun a bookmark title of index for fuzzy interface
func (b Bookmarks) String(i int) string {
return b[i].Title
}
Expand All @@ -63,7 +64,7 @@ func (b Bookmarks) Len() int {
return len(b)
}

// Filter fuzzy search bookmarks using query
// Filter fuzzy search Bookmarks using query
func (b Bookmarks) Filter(query string) Bookmarks {
bookmarks := Bookmarks{}
results := fuzzy.FindFrom(query, b)
Expand Down
72 changes: 38 additions & 34 deletions pkg/bookmarker/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ import (
"os"
"time"

"github.com/konoui/alfred-bookmarks/pkg/cache"
"github.com/konoui/alfred-bookmarks/pkg/cacher"
"github.com/pkg/errors"
)

type browser string
var cacheDir string

// Browser  is a type of supported engine
type Browser string

const (
firefox browser = "firefox"
chrome browser = "chrome"
// Firefox is supported
Firefox Browser = "firefox"
// Chrome is supported
Chrome Browser = "chrome"
cacheFile string = "alfred-bookmarks.cache"
)
const cacheFile = "alfred-bookmarks.cache"

var cacheDir string

// Browsers determine which bookmark read from
type Browsers struct {
bookmarkers map[browser]Bookmarker
bookmarkers map[Browser]Bookmarker
removeDuplicate bool
cache cache.Cacher
cacher cacher.Cacher
}

// Option is the type to replace default parameters.
Expand All @@ -39,7 +43,7 @@ func OptionFirefox(profile string) Option {
return err
}

b.bookmarkers[firefox] = NewFirefox(path)
b.bookmarkers[Firefox] = NewFirefox(path)
return nil
}
}
Expand All @@ -52,37 +56,37 @@ func OptionChrome(profile string) Option {
return err
}

b.bookmarkers[chrome] = NewChrome(path)
b.bookmarkers[Chrome] = NewChrome(path)
return nil
}
}

// OptionRemoveDuplicate remove same url bookmark e.g) search from multi browser
// OptionRemoveDuplicate remove same url bookmark e.g) search from multi browsers
func OptionRemoveDuplicate() Option {
return func(b *Browsers) error {
b.removeDuplicate = true
return nil
}
}

// OptionCacheMaxAge bookmark cache time. unit indicate hours
// if passed arg is zero, set 24 hours. if passed arg is minus, set 0 hours
func OptionCacheMaxAge(age int) Option {
// OptionCacheMaxAge is bookmark cache time. unit indicate hours
// if passed arg is zero, set 24 hours. if passed arg is minus, disable cache
func OptionCacheMaxAge(hour int) Option {
return func(b *Browsers) error {
if age == 0 {
age = 24
} else if age < 0 {
age = 0
if hour == 0 {
hour = 24
} else if hour < 0 {
hour = 0
}

c, err := cache.New(
c, err := cacher.New(
cacheDir, cacheFile,
time.Duration(age)*time.Hour)
time.Duration(hour)*time.Hour)
if err != nil {
return err
}

b.cache = c
b.cacher = c
return nil
}
}
Expand All @@ -94,11 +98,11 @@ func OptionNone() Option {
}
}

// NewBrowsers return Browsers
// NewBrowsers is instance to get Bookmarks of multi browser
func NewBrowsers(opts ...Option) Bookmarker {
b := &Browsers{
bookmarkers: make(map[browser]Bookmarker),
cache: cache.NewNilCache(),
bookmarkers: make(map[Browser]Bookmarker),
cacher: cacher.NewNilCache(),
}

for _, opt := range opts {
Expand All @@ -110,12 +114,12 @@ func NewBrowsers(opts ...Option) Bookmarker {
return b
}

// Bookmarks return Bookmarks struct, loading cache file.
// Bookmarks return Bookmarks struct by loading cache file
func (browsers *Browsers) Bookmarks() (Bookmarks, error) {
bookmarks := Bookmarks{}
if !browsers.cache.Expired() {
if err := browsers.cache.Load(&bookmarks); err != nil {
return Bookmarks{}, err
if !browsers.cacher.Expired() {
if err := browsers.cacher.Load(&bookmarks); err != nil {
return Bookmarks{}, errors.Wrap(err, "failed to load cache data")
}
return bookmarks, nil
}
Expand All @@ -124,21 +128,21 @@ func (browsers *Browsers) Bookmarks() (Bookmarks, error) {
if err != nil {
return Bookmarks{}, err
}
if err := browsers.cache.Store(&bookmarks); err != nil {
return Bookmarks{}, err
if err := browsers.cacher.Store(&bookmarks); err != nil {
return Bookmarks{}, errors.Wrap(err, "failed to save data into cache")
}

return bookmarks, nil
}

// bookmarks return Bookmarks struct, loading each browser bookmarks and parse them.
// bookmarks return Bookmarks struct by loading each browser
func (browsers *Browsers) bookmarks() (Bookmarks, error) {
bookmarks := Bookmarks{}
for _, bookmarker := range browsers.bookmarkers {
for browser, bookmarker := range browsers.bookmarkers {
b, err := bookmarker.Bookmarks()
if err != nil {
// Note: not continue but return err if error occurs
return Bookmarks{}, err
return Bookmarks{}, errors.Wrapf(err, "failed to load bookmarks in %s", browser)
}
bookmarks = append(bookmarks, b...)
}
Expand Down
Loading

0 comments on commit 63cbdbe

Please sign in to comment.