File tree Expand file tree Collapse file tree 8 files changed +48
-27
lines changed Expand file tree Collapse file tree 8 files changed +48
-27
lines changed Original file line number Diff line number Diff line change 11The MIT License (MIT)
22
3- Copyright (C) 2015 Yasuaki Uechi <http ://randompaper.co>
3+ Copyright (C) 2016 Yasuaki Uechi (https ://randompaper.co)
44
55Permission is hereby granted, free of charge, to any person obtaining a copy
66of this software and associated documentation files (the "Software"), to deal
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -2,21 +2,22 @@ package main
22
33import (
44 "encoding/json"
5+ "github.com/mitchellh/go-homedir"
56 "github.com/spf13/viper"
67 "os"
7- "os/user"
88 "path/filepath"
99)
1010
1111type config struct {
1212 AccessToken string `json:"accessToken"`
13+ ID string `json:"id"`
1314}
1415
15- var C config
16+ var marshaledConfig config
1617
1718func 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
2223func loadConfig () error {
@@ -33,9 +34,9 @@ func loadConfig() error {
3334
3435func 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 }
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change @@ -9,24 +9,23 @@ import (
99)
1010
1111const (
12- bundleId = "co.randompaper.alfred-qiita-workflow"
12+ bundleID = "co.randompaper.alfred-qiita-workflow"
1313 version = "2.0.0"
1414)
1515
1616func 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
Original file line number Diff line number Diff line change 11package main
22
33import (
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
1112func 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 })
Original file line number Diff line number Diff 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
912func 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" )
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package main
33import (
44 "github.com/codegangsta/cli"
55 "github.com/pascalw/go-alfred"
6+ "github.com/spf13/viper"
67)
78
89func 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 ()
You can’t perform that action at this time.
0 commit comments