Skip to content

Commit 364dc2d

Browse files
authored
Merge pull request #5 from pddg/feature/awgo
Feature/awgo
2 parents 68d8e24 + 7fdf25c commit 364dc2d

File tree

7 files changed

+204
-139
lines changed

7 files changed

+204
-139
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ vendor/
2525
go-ghq-alfred
2626
*.alfredworkflow
2727
dist/
28-
28+
testdir/

Makefile

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
SHELL := /bin/bash
22
BIN_NAME := dist/go-ghq-alfred
33
WF_NAME := ghq-alfred.alfredworkflow
4+
ASSETS := $(shell find -f ./resources)
5+
TESTDIR := testdir
46

5-
test:
6-
go test ./... -v
7+
$(TESTDIR):
8+
mkdir -p $(TESTDIR)/{data,cache}
9+
10+
test: $(TESTDIR)
11+
env alfred_workflow_bundleid=testid \
12+
alfred_workflow_cache=$(TESTDIR)/cache \
13+
alfred_workflow_data=$(TESTDIR)/data \
14+
go test ./... -v
715

816
$(BIN_NAME): main.go
917
go build -o $(BIN_NAME) .
1018

1119
build: $(BIN_NAME)
1220

13-
$(WF_NAME): build
21+
$(WF_NAME): $(BIN_NAME) $(ASSETS)
1422
if [ ! -d dist ]; then \
1523
mkdir dist/; \
1624
fi

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/pddg/go-ghq-alfred
33
go 1.13
44

55
require (
6+
github.com/deanishe/awgo v0.21.0
67
github.com/pddg/alfred-models v0.0.0-20170923143305-c0134a499549
78
github.com/urfave/cli v1.20.1-0.20170911040819-7fb9c86b14e6
89
)

go.sum

+30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,34 @@
1+
github.com/bmatcuk/doublestar v1.1.5/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/deanishe/awgo v0.21.0 h1:p0EgcmEoUj5q/joi7e7KQ0fL0FTeElyfR4FgOsPOaT0=
4+
github.com/deanishe/awgo v0.21.0/go.mod h1:mWVSFCwpd+G+jCzHz00Euyhi2avtS9rnOfl+s7cSDhk=
5+
github.com/deanishe/go-env v0.4.0 h1:tpu14o16gJGTN/w2gxntwxu2l5Eby30jSrnlgOfjzwk=
6+
github.com/deanishe/go-env v0.4.0/go.mod h1:RgEcGAqdRnt8ybQteAbv1Ys2lWIRE7TlgON/sbdjuaY=
7+
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
8+
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
9+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
10+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
11+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
12+
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
13+
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
14+
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
15+
github.com/mfridman/tparse v0.7.4/go.mod h1:OSpmW/J0XQa/+TeiFBmJARmJa68FCtq0jpsWKKJeU6Q=
16+
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
117
github.com/pddg/alfred-models v0.0.0-20170923143305-c0134a499549 h1:i6HmX10+WL5/e29ZDct7gbmIXZayWnmtz2ZqBqy0rKA=
218
github.com/pddg/alfred-models v0.0.0-20170923143305-c0134a499549/go.mod h1:rvyKdDBlT8wMwZiJjVBmjqlz+8TvOX8tL3StUSNRoKI=
19+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
20+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
21+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
22+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
23+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
324
github.com/urfave/cli v1.20.1-0.20170911040819-7fb9c86b14e6 h1:FxB2ax1KabQ9P5XJAZHRd6149Q7X+0txg5E+9A1y48U=
425
github.com/urfave/cli v1.20.1-0.20170911040819-7fb9c86b14e6/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
26+
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
27+
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
28+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
29+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
30+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
31+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
32+
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
33+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
34+
howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=

main.go

+81-100
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,76 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"os"
5+
"path"
76
"strings"
8-
"sync"
97

10-
model "github.com/pddg/alfred-models"
8+
aw "github.com/deanishe/awgo"
119
"github.com/urfave/cli"
1210
)
1311

14-
func main() {
12+
const (
13+
appName = "ghq-alfred"
14+
appDesc = "Search your local repos"
15+
appVersion = "0.4.0"
16+
)
17+
18+
var (
19+
wf *aw.Workflow
20+
gitHubIcon = &aw.Icon{Value: path.Join("github-logo.png")}
21+
bitBucketIcon = &aw.Icon{Value: path.Join("bitbucket-logo.png")}
22+
gitIcon = &aw.Icon{Value: path.Join("git-logo.png")}
23+
modKeys = []aw.ModKey{
24+
aw.ModCmd,
25+
aw.ModOpt,
26+
aw.ModFn,
27+
aw.ModCtrl,
28+
aw.ModShift,
29+
}
30+
)
31+
32+
func init() {
33+
wf = aw.New()
34+
}
35+
36+
func run() {
1537
app := cli.NewApp()
16-
app.Name = "ghq-alfred"
17-
app.Usage = "Search your local repos"
18-
app.Version = "0.3.1"
38+
app.Name = appName
39+
app.Usage = appDesc
40+
app.Version = appVersion
1941
app.Action = func(c *cli.Context) error {
20-
resp := model.NewResponse()
2142
query := strings.Trim(c.Args()[0], " \n")
2243
repos := c.Args()[1:c.NArg()]
23-
ch := make(chan *model.Item)
24-
wg := &sync.WaitGroup{}
25-
for index, repo := range repos {
26-
wg.Add(1)
27-
go worker(index, repo, query, wg, ch)
28-
}
29-
go waitForWorker(wg, ch)
30-
for item := range ch {
31-
resp.Items = append(resp.Items, *item)
32-
}
33-
if resp.Items == nil {
34-
// When any item is not found.
35-
item := createNoResultItem()
36-
resp.Items = append(resp.Items, *item)
44+
for _, repo := range repos {
45+
addNewItem(repo)
3746
}
38-
j, err := json.Marshal(resp)
39-
if err != nil {
40-
// Json error
41-
fmt.Println("{'items': [{'title': 'Json object is invalid.', 'subtitle': 'Please contact to developper.', 'valid': false}]")
42-
} else {
43-
fmt.Println(string(j))
47+
if len(query) > 0 {
48+
wf.Filter(query)
4449
}
50+
wf.WarnEmpty("No matching repository", "Try different query?")
51+
wf.SendFeedback()
4552
return nil
4653
}
4754
app.Run(os.Args)
4855
}
4956

50-
func waitForWorker(wg *sync.WaitGroup, ch chan *model.Item) {
51-
wg.Wait()
52-
close(ch)
53-
}
54-
55-
func worker(index int, repo string, query string, wg *sync.WaitGroup, ch chan *model.Item) {
56-
defer wg.Done()
57-
path := strings.Split(repo, "/")
58-
if matchRepo(path, query) {
59-
// Create normal item
60-
item := createNewItem(index, repo, path)
61-
ch <- item
62-
}
63-
}
64-
65-
func createNewItem(index int, repo string, repo_path []string) *model.Item {
66-
item := model.NewItem()
67-
item.Uid = string(index)
68-
item.Title = excludeDomain(repo_path, true)
69-
item.Subtitle = getDomainName(repo_path)
70-
item.Arg = repo
71-
item.Icon.Type = ""
72-
item.Icon.Path = getIconPath(repo_path)
73-
createModItems(repo_path, repo, &item.Mods)
74-
return item
75-
}
76-
77-
func createNoResultItem() *model.Item {
78-
item := model.NewItem()
79-
item.Title = "No result found."
80-
item.Subtitle = "Please input again."
81-
item.Valid = false
82-
return item
57+
func main() {
58+
wf.Run(run)
8359
}
8460

85-
func matchRepo(repo_path []string, query string) bool {
86-
repo_path_lower, query_lower := strings.ToLower(excludeDomain(repo_path, true)), strings.ToLower(query)
87-
if strings.Index(repo_path_lower, query_lower) != -1 {
88-
return true
61+
func addNewItem(repo string) {
62+
repoPath := strings.Split(repo, "/")
63+
it := wf.NewItem(repo).
64+
Title(excludeDomain(repoPath, true)).
65+
UID(repo).
66+
Arg(repo).
67+
Subtitle(getDomainName(repoPath)).
68+
Icon(getIcon(repoPath)).
69+
Valid(true)
70+
for _, modKey := range modKeys {
71+
mod := createModItem(repoPath, repo, modKey)
72+
it.SetModifier(mod)
8973
}
90-
return false
9174
}
9275

9376
func excludeDomain(repo []string, domain bool) string {
@@ -109,45 +92,43 @@ func getDomainName(repo_path []string) string {
10992
return repo_path[len(repo_path)-3]
11093
}
11194

112-
func createModItems(repo []string, path string, mods *map[string]model.Mod) {
113-
for _, key := range model.Modifiers {
114-
var (
115-
arg string
116-
sub string
117-
)
118-
switch key {
119-
case model.Cmd:
120-
arg = path
121-
sub = "Open '" + path + "' in Finder."
122-
case model.Shift:
123-
arg = "https://" + excludeDomain(repo, false) + "/"
124-
sub = "Open '" + arg + "' in browser."
125-
case model.Ctrl:
126-
arg = path
127-
sub = "Open '" + path + "' in editor."
128-
case model.Fn:
129-
arg = path
130-
sub = "Open '" + path + "' in terminal app."
131-
case model.Alt:
132-
arg = excludeDomain(repo, true)
133-
sub = "Search '" + arg + "' with google."
134-
}
135-
mod := model.NewMod(arg, sub)
136-
(*mods)[key] = *mod
95+
func createModItem(repo []string, path string, modKey aw.ModKey) *aw.Modifier {
96+
var (
97+
arg string
98+
sub string
99+
)
100+
switch modKey {
101+
case aw.ModCmd:
102+
arg = path
103+
sub = "Open in Finder."
104+
case aw.ModShift:
105+
arg = "https://" + excludeDomain(repo, false) + "/"
106+
sub = "Open '" + arg + "' in browser."
107+
case aw.ModCtrl:
108+
arg = path
109+
sub = "Open in editor."
110+
case aw.ModFn:
111+
arg = path
112+
sub = "Open in terminal app."
113+
case aw.ModOpt:
114+
arg = excludeDomain(repo, true)
115+
sub = "Search '" + arg + "' with google."
137116
}
117+
mod := &aw.Modifier{Key: modKey}
118+
return mod.
119+
Arg(arg).
120+
Subtitle(sub).
121+
Valid(true)
138122
}
139123

140-
func getIconPath(repo_path []string) string {
141-
var icon_path string
142-
domain := getDomainName(repo_path)
143-
prefix := "./resources"
124+
func getIcon(repoPath []string) *aw.Icon {
125+
domain := getDomainName(repoPath)
144126
switch {
145127
case strings.Contains(domain, "github"):
146-
icon_path = prefix + "/github-logo.png"
128+
return gitHubIcon
147129
case strings.Contains(domain, "bitbucket"):
148-
icon_path = prefix + "/bitbucket-logo.png"
130+
return bitBucketIcon
149131
default:
150-
icon_path = prefix + "/git-logo.png"
132+
return gitIcon
151133
}
152-
return icon_path
153134
}

0 commit comments

Comments
 (0)