-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
134 lines (122 loc) · 2.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"os"
"path"
"strings"
aw "github.com/deanishe/awgo"
"github.com/urfave/cli"
)
const (
appName = "ghq-alfred"
appDesc = "Search your local repos"
appVersion = "0.4.0"
)
var (
wf *aw.Workflow
gitHubIcon = &aw.Icon{Value: path.Join("github-logo.png")}
bitBucketIcon = &aw.Icon{Value: path.Join("bitbucket-logo.png")}
gitIcon = &aw.Icon{Value: path.Join("git-logo.png")}
modKeys = []aw.ModKey{
aw.ModCmd,
aw.ModOpt,
aw.ModFn,
aw.ModCtrl,
aw.ModShift,
}
)
func init() {
wf = aw.New()
}
func run() {
app := cli.NewApp()
app.Name = appName
app.Usage = appDesc
app.Version = appVersion
app.Action = func(c *cli.Context) error {
query := strings.Trim(c.Args()[0], " \n")
repos := c.Args()[1:c.NArg()]
for _, repo := range repos {
addNewItem(repo)
}
if len(query) > 0 {
wf.Filter(query)
}
wf.WarnEmpty("No matching repository", "Try different query?")
wf.SendFeedback()
return nil
}
app.Run(os.Args)
}
func main() {
wf.Run(run)
}
func addNewItem(repo string) {
repoPath := strings.Split(repo, "/")
it := wf.NewItem(repo).
Title(excludeDomain(repoPath, true)).
UID(repo).
Arg(repo).
Subtitle(getDomainName(repoPath)).
Icon(getIcon(repoPath)).
Valid(true)
for _, modKey := range modKeys {
mod := createModItem(repoPath, repo, modKey)
it.SetModifier(mod)
}
}
func excludeDomain(repo []string, domain bool) string {
// full_repo_path: strings.Split("/path/to/github.com/user/full_repo_path", "/")
var i int
if domain {
// return user/full_repo_path
i = 2
} else {
// return github.com/user/full_repo_path
i = 3
}
length := len(repo)
return strings.Join(repo[length-i:length], "/")
}
func getDomainName(repo_path []string) string {
// return github.com
return repo_path[len(repo_path)-3]
}
func createModItem(repo []string, path string, modKey aw.ModKey) *aw.Modifier {
var (
arg string
sub string
)
switch modKey {
case aw.ModCmd:
arg = path
sub = "Open in Finder."
case aw.ModShift:
arg = "https://" + excludeDomain(repo, false) + "/"
sub = "Open '" + arg + "' in browser."
case aw.ModCtrl:
arg = path
sub = "Open in editor."
case aw.ModFn:
arg = path
sub = "Open in terminal app."
case aw.ModOpt:
arg = excludeDomain(repo, true)
sub = "Search '" + arg + "' with google."
}
mod := &aw.Modifier{Key: modKey}
return mod.
Arg(arg).
Subtitle(sub).
Valid(true)
}
func getIcon(repoPath []string) *aw.Icon {
domain := getDomainName(repoPath)
switch {
case strings.Contains(domain, "github"):
return gitHubIcon
case strings.Contains(domain, "bitbucket"):
return bitBucketIcon
default:
return gitIcon
}
}