Skip to content

Commit e56732d

Browse files
committed
Merge pull request #5 from uetchy/fix-4
Fix #4
2 parents c39e615 + 61d62c3 commit e56732d

File tree

8 files changed

+48
-27
lines changed

8 files changed

+48
-27
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (C) 2015 Yasuaki Uechi <http://randompaper.co>
3+
Copyright (C) 2016 Yasuaki Uechi (https://randompaper.co)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ qiita my <query:optional>
5252

5353
### Development Installation
5454

55-
Run folloing commands to link this workflow to Alfred app manually.
55+
Run following commands to link this workflow to Alfred app manually.
5656

5757
```
5858
$ go get github.com/uetchy/alfred-qiita-workflow

config.go

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

33
import (
44
"encoding/json"
5+
"github.com/mitchellh/go-homedir"
56
"github.com/spf13/viper"
67
"os"
7-
"os/user"
88
"path/filepath"
99
)
1010

1111
type config struct {
1212
AccessToken string `json:"accessToken"`
13+
ID string `json:"id"`
1314
}
1415

15-
var C config
16+
var marshaledConfig config
1617

1718
func getDefaultConfigPath() string {
18-
currentUser, _ := user.Current()
19-
return filepath.Join(currentUser.HomeDir, "Library/Application Support/Alfred 2/Workflow Data/", bundleId)
19+
homeDir, _ := homedir.Dir()
20+
return filepath.Join(homeDir, "Library/Application Support/Alfred 2/Workflow Data/", bundleID)
2021
}
2122

2223
func loadConfig() error {
@@ -33,9 +34,9 @@ func loadConfig() error {
3334

3435
func saveConfig() error {
3536
configPath := getDefaultConfigPath()
36-
viper.Marshal(&C)
37+
viper.Unmarshal(&marshaledConfig)
3738

38-
buf, err := json.MarshalIndent(C, "", " ")
39+
buf, err := json.MarshalIndent(marshaledConfig, "", " ")
3940
if err != nil {
4041
return err
4142
}

info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@
149149
<key>keyword</key>
150150
<string>qiita search</string>
151151
<key>queuedelaycustom</key>
152-
<integer>1</integer>
152+
<integer>3</integer>
153153
<key>queuedelayimmediatelyinitially</key>
154154
<false/>
155155
<key>queuedelaymode</key>
156156
<integer>0</integer>
157157
<key>queuemode</key>
158-
<integer>1</integer>
158+
<integer>2</integer>
159159
<key>runningsubtext</key>
160160
<string>Seaching for "{query}" ...</string>
161161
<key>script</key>

main.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,23 @@ import (
99
)
1010

1111
const (
12-
bundleId = "co.randompaper.alfred-qiita-workflow"
12+
bundleID = "co.randompaper.alfred-qiita-workflow"
1313
version = "2.0.0"
1414
)
1515

1616
func newQiitaClient() (*qiita.Client, error) {
17-
err := loadConfig()
18-
if err != nil {
19-
return nil, err
17+
loadConfig()
18+
var client *qiita.Client
19+
if accessToken := viper.GetString("accessToken"); accessToken != "" {
20+
ts := oauth2.StaticTokenSource(
21+
&oauth2.Token{AccessToken: accessToken},
22+
)
23+
tc := oauth2.NewClient(oauth2.NoContext, ts)
24+
client = qiita.NewClient(tc)
25+
} else {
26+
client = qiita.NewClient(nil)
2027
}
21-
accessToken := viper.GetString("accessToken")
22-
if accessToken == "" {
23-
return nil, err
24-
}
25-
ts := oauth2.StaticTokenSource(
26-
&oauth2.Token{AccessToken: accessToken},
27-
)
28-
tc := oauth2.NewClient(oauth2.NoContext, ts)
29-
client := qiita.NewClient(tc)
28+
3029
return client, nil
3130
}
3231

search.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package main
22

33
import (
4-
// "fmt"
4+
"fmt"
55
"github.com/codegangsta/cli"
66
"github.com/pascalw/go-alfred"
77
"github.com/uetchy/go-qiita/qiita"
8+
"os"
89
"strings"
910
)
1011

1112
func cmdSearch(c *cli.Context) {
1213
query := strings.Join(c.Args(), " ")
1314
client, err := newQiitaClient()
1415
if err != nil {
15-
return
16+
fmt.Println(err)
17+
os.Exit(1)
1618
}
1719

1820
items, _, _ := client.Items.List(&qiita.ItemsListOptions{Query: query})

setup.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,33 @@ import (
44
"fmt"
55
"github.com/codegangsta/cli"
66
"github.com/spf13/viper"
7+
"github.com/uetchy/go-qiita/qiita"
8+
"golang.org/x/oauth2"
9+
"os"
710
)
811

912
func cmdSetup(c *cli.Context) {
1013
token := c.Args().First()
1114

15+
ts := oauth2.StaticTokenSource(
16+
&oauth2.Token{AccessToken: token},
17+
)
18+
tc := oauth2.NewClient(oauth2.NoContext, ts)
19+
client := qiita.NewClient(tc)
20+
user, err := client.AuthenticatedUser.Show()
21+
if err != nil {
22+
fmt.Println("Auth failed")
23+
os.Exit(1)
24+
}
25+
1226
loadConfig()
1327
viper.Set("accessToken", token)
14-
err := saveConfig()
28+
viper.Set("id", user.Id)
29+
30+
err = saveConfig()
1531
if err != nil {
1632
fmt.Println(err)
33+
os.Exit(1)
1734
}
1835

1936
fmt.Println("Token saved")

stocks.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"github.com/codegangsta/cli"
55
"github.com/pascalw/go-alfred"
6+
"github.com/spf13/viper"
67
)
78

89
func cmdStocks(c *cli.Context) {
@@ -11,7 +12,8 @@ func cmdStocks(c *cli.Context) {
1112
if err != nil {
1213
return
1314
}
14-
items, _, _ := client.Users.Stocks("uetchy", nil)
15+
16+
items, _, _ := client.Users.Stocks(viper.GetString("id"), nil)
1517

1618
alfred.InitTerms(query)
1719
response := alfred.NewResponse()

0 commit comments

Comments
 (0)