Skip to content

Commit 8c6ceba

Browse files
committed
Fix search command
1 parent 0313bb7 commit 8c6ceba

File tree

10 files changed

+114
-58
lines changed

10 files changed

+114
-58
lines changed

Makefile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
ALFRED_WORKFLOW_PATH ?= ~/Library/Application Support/Alfred 2/Alfred.alfredpreferences/workflows
2-
BUNDLE_ID = "co.randompaper.alfred-qiita-workflow"
3-
PACKAGE_FILE = "info.plist"
2+
BUNDLE_ID = co.randompaper.alfred-qiita-workflow
3+
PACKAGE_FILE = info.plist
4+
SYMLINK_TARGET = ${ALFRED_WORKFLOW_PATH}/${BUNDLE_ID}
45

56
run:
67
go run *.go
78

89
link:
9-
TARGET := "$(ALFRED_WORKFLOW_PATH)/$(BUNDLE_ID)"
10-
# mkdir -p "${TARGET}"
11-
# ln -sf "`pwd`", "$(TARGET)"
10+
ln -sf "$(PWD)" "${SYMLINK_TARGET}"
1211

1312
unlink:
14-
rm "$ALFRED_WORKFLOW_PATH/$BUNDLE_ID"
13+
rm "${SYMLINK_TARGET}"
14+
15+
build:
16+
gox -osarch="darwin/amd64" -output="bin/{{.OS}}_{{.Arch}}"

config.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@ type config struct {
1414

1515
var C config
1616

17-
func getHomeDir() string {
18-
currentUser, _ := user.Current()
19-
return currentUser.HomeDir
20-
}
21-
2217
func getDefaultConfigPath() string {
23-
return filepath.Join(getHomeDir(), "Library/Application Support/Alfred 2/Workflow Data/", bundleId)
18+
currentUser, _ := user.Current()
19+
return filepath.Join(currentUser.HomeDir, "Library/Application Support/Alfred 2/Workflow Data/", bundleId)
2420
}
2521

2622
func loadConfig() error {
@@ -49,7 +45,7 @@ func saveConfig() error {
4945
return err
5046
}
5147

52-
f, err := os.Create(configPath + "/config.json")
48+
f, err := os.Create(filepath.Join(configPath, "config.json"))
5349
if err != nil {
5450
return err
5551
}

info.plist

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<dict>
55
<key>bundleid</key>
66
<string>co.randompaper.qiita.alfred</string>
7+
<key>category</key>
8+
<string>Internet</string>
79
<key>connections</key>
810
<dict>
911
<key>21557827-8003-42B7-A042-16D4C9278FEC</key>
@@ -65,7 +67,7 @@
6567
<key>createdby</key>
6668
<string>Yasuaki Uechi</string>
6769
<key>description</key>
68-
<string>The workflow to search articles on Qiita</string>
70+
<string>Search articles from Qiita</string>
6971
<key>disabled</key>
7072
<false/>
7173
<key>name</key>
@@ -105,7 +107,7 @@
105107
<key>keyword</key>
106108
<string>qiita setup</string>
107109
<key>subtext</key>
108-
<string>RUN: qiita setup &lt;name&gt; &lt;password&gt;</string>
110+
<string>qiita setup &lt;personal access token&gt;</string>
109111
<key>text</key>
110112
<string>Qiita Setup</string>
111113
<key>withspace</key>
@@ -121,10 +123,12 @@
121123
<dict>
122124
<key>config</key>
123125
<dict>
126+
<key>concurrently</key>
127+
<false/>
124128
<key>escaping</key>
125129
<integer>0</integer>
126130
<key>script</key>
127-
<string>ruby ./commands/setup.rb "{query}"</string>
131+
<string>./bin/darwin_amd64 setup "{query}"</string>
128132
<key>type</key>
129133
<integer>0</integer>
130134
</dict>
@@ -144,12 +148,20 @@
144148
<integer>0</integer>
145149
<key>keyword</key>
146150
<string>qiita search</string>
151+
<key>queuedelaycustom</key>
152+
<integer>1</integer>
153+
<key>queuedelayimmediatelyinitially</key>
154+
<false/>
155+
<key>queuedelaymode</key>
156+
<integer>0</integer>
157+
<key>queuemode</key>
158+
<integer>1</integer>
147159
<key>runningsubtext</key>
148160
<string>Seaching for "{query}" ...</string>
149161
<key>script</key>
150-
<string>ruby ./commands/search.rb "{query}"</string>
162+
<string>./bin/darwin_amd64 search "{query}"</string>
151163
<key>subtext</key>
152-
<string>Search Qiita for "{query}"</string>
164+
<string>Search articles for "{query}"</string>
153165
<key>title</key>
154166
<string>Qiita Search Articles</string>
155167
<key>type</key>
@@ -190,10 +202,18 @@
190202
<integer>0</integer>
191203
<key>keyword</key>
192204
<string>qiita stocks</string>
205+
<key>queuedelaycustom</key>
206+
<integer>1</integer>
207+
<key>queuedelayimmediatelyinitially</key>
208+
<false/>
209+
<key>queuedelaymode</key>
210+
<integer>0</integer>
211+
<key>queuemode</key>
212+
<integer>1</integer>
193213
<key>runningsubtext</key>
194214
<string>Seaching for "{query}" ...</string>
195215
<key>script</key>
196-
<string>ruby ./commands/stocks.rb "{query}"</string>
216+
<string>./bin/darwin_amd64 stocks "{query}"</string>
197217
<key>subtext</key>
198218
<string>Search stocks for "{query}"</string>
199219
<key>title</key>
@@ -219,10 +239,18 @@
219239
<integer>0</integer>
220240
<key>keyword</key>
221241
<string>qiita my</string>
242+
<key>queuedelaycustom</key>
243+
<integer>1</integer>
244+
<key>queuedelayimmediatelyinitially</key>
245+
<false/>
246+
<key>queuedelaymode</key>
247+
<integer>0</integer>
248+
<key>queuemode</key>
249+
<integer>1</integer>
222250
<key>runningsubtext</key>
223251
<string>Searching for "{query}" ...</string>
224252
<key>script</key>
225-
<string>ruby ./commands/my.rb "{query}"</string>
253+
<string>./bin/darwin_amd64 my "{query}"</string>
226254
<key>subtext</key>
227255
<string>Search my articles for "{query}"</string>
228256
<key>title</key>
@@ -281,6 +309,6 @@
281309
</dict>
282310
</dict>
283311
<key>webaddress</key>
284-
<string>http://randompaper.co</string>
312+
<string>https://github.com/uetchy/alfred-qiita-workflow</string>
285313
</dict>
286314
</plist>

main.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,36 @@ package main
22

33
import (
44
"github.com/codegangsta/cli"
5+
"github.com/spf13/viper"
6+
"github.com/uetchy/alfred-qiita-workflow/qiita"
7+
"golang.org/x/oauth2"
58
"os"
69
)
710

811
const (
912
bundleId = "co.randompaper.alfred-qiita-workflow"
1013
)
1114

15+
func newQiitaClient() (*qiita.Client, error) {
16+
err := loadConfig()
17+
if err != nil {
18+
return nil, err
19+
}
20+
accessToken := viper.GetString("accessToken")
21+
if accessToken == "" {
22+
return nil, err
23+
}
24+
ts := oauth2.StaticTokenSource(
25+
&oauth2.Token{AccessToken: accessToken},
26+
)
27+
tc := oauth2.NewClient(oauth2.NoContext, ts)
28+
client := qiita.NewClient(tc)
29+
return client, nil
30+
}
31+
1232
func main() {
1333
app := cli.NewApp()
14-
app.Name = "qiita"
15-
// app.Usage = "fight the loneliness!"
34+
app.Name = "alfred-qiita"
1635
app.Commands = []cli.Command{
1736
{
1837
Name: "search",

qiita/items.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,15 @@ type ItemsService struct {
1313
type Item struct {
1414
RenderedBody *string `json:"rendered_body"`
1515
Body *string `json:"body"`
16-
Coediting *bool `json:"coediting"`
16+
CoEditing *bool `json:"coediting"`
1717
CreatedAt *time.Time `json:"created_at"`
1818
Id *string `json:"id"`
1919
Private *bool `json:"private"`
2020
Tags []Tag `json:"tags"`
2121
Title *string `json:"title"`
2222
UpdatedAt *time.Time `json:"updated_at"`
2323
URL *string `json:"url"`
24-
// TODO: User
25-
}
26-
27-
type Tag struct {
28-
Name *string `json:"name"`
29-
Versions []string `json:"versions"`
24+
User *User `json:"user"`
3025
}
3126

3227
type ItemsListOptions struct {

qiita/qiita.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"io"
7-
// "io/ioutil"
8-
"fmt"
97
"github.com/google/go-querystring/query"
108
"net/http"
119
"net/url"
@@ -64,7 +62,6 @@ func NewClient(httpClient *http.Client) *Client {
6462
httpClient = http.DefaultClient
6563
}
6664
baseURL, _ := url.Parse(defaultBaseURL)
67-
fmt.Println(baseURL)
6865

6966
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent}
7067
c.Items = &ItemsService{client: c}

qiita/tag.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package qiita
2+
3+
type Tag struct {
4+
Name *string `json:"name"`
5+
Versions []string `json:"versions"`
6+
}

qiita/user.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package qiita
2+
3+
type User struct {
4+
Name *string `json:"name"`
5+
Description *string `json:"description"`
6+
FacebookId *string `json:"facebook_id"`
7+
FolloweesCount *int `json:"followees_count"`
8+
FollowersCount *int `json:"followers_count"`
9+
GithubLoginName *string `json:"github_login_name"`
10+
Id *string `json:"id"`
11+
ItemsCount *int `json:"items_count"`
12+
LinkedInId *string `json:"linkedin_id"`
13+
Location *string `json:"location"`
14+
Organization *string `json:"organization"`
15+
PermanentId *int `json:"permanent_id"`
16+
ProfileImageURL *string `json:"profile_image_url"`
17+
TwitterScreenName *string `json:"twitter_screen_name"`
18+
WebsiteURL *string `json:"website_url"`
19+
}

search.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,29 @@
11
package main
22

33
import (
4-
"fmt"
4+
// "fmt"
55
"github.com/codegangsta/cli"
66
"github.com/pascalw/go-alfred"
7-
"github.com/spf13/viper"
87
"github.com/uetchy/alfred-qiita-workflow/qiita"
9-
"golang.org/x/oauth2"
8+
"strings"
109
)
1110

1211
func cmdSearch(c *cli.Context) {
13-
query := c.Args().First()
14-
15-
loadConfig()
16-
ts := oauth2.StaticTokenSource(
17-
&oauth2.Token{AccessToken: viper.GetString("accessToken")},
18-
)
19-
tc := oauth2.NewClient(oauth2.NoContext, ts)
20-
client := qiita.NewClient(tc)
12+
query := strings.Join(c.Args(), " ")
13+
client, err := newQiitaClient()
14+
if err != nil {
15+
return
16+
}
2117

22-
// Fetch items
2318
items, _, _ := client.Items.List(&qiita.ItemsListOptions{Query: query})
24-
fmt.Println(*items[0].Title)
25-
26-
// alfred.InitTerms(query)
27-
2819
response := alfred.NewResponse()
29-
3020
for _, item := range items {
31-
// if !alfred.MatchesTerms(query, repo.Name) {
32-
// continue
33-
// }
3421
response.AddItem(&alfred.AlfredResponseItem{
35-
Valid: true,
36-
Uid: *item.Id,
37-
Title: *item.Title,
38-
Arg: *item.URL,
22+
Valid: true,
23+
Uid: *item.Id,
24+
Title: *item.Title,
25+
Arg: *item.URL,
26+
Subtitle: *item.User.Id + " " + item.CreatedAt.Format("2006/01/02 15:04:05"),
3927
})
4028
}
4129

setup.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/codegangsta/cli"
56
"github.com/spf13/viper"
67
)
@@ -10,5 +11,10 @@ func cmdSetup(c *cli.Context) {
1011

1112
loadConfig()
1213
viper.Set("accessToken", token)
13-
saveConfig()
14+
err := saveConfig()
15+
if err != nil {
16+
fmt.Println(err)
17+
}
18+
19+
fmt.Println("Token saved")
1420
}

0 commit comments

Comments
 (0)