File tree Expand file tree Collapse file tree 4 files changed +84
-17
lines changed Expand file tree Collapse file tree 4 files changed +84
-17
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "encoding/json"
5
+ "github.com/spf13/viper"
6
+ "os"
7
+ "os/user"
8
+ "path/filepath"
9
+ )
10
+
11
+ type config struct {
12
+ AccessToken string `json:"accessToken"`
13
+ }
14
+
15
+ var C config
16
+
17
+ func getHomeDir () string {
18
+ currentUser , _ := user .Current ()
19
+ return currentUser .HomeDir
20
+ }
21
+
22
+ func getDefaultConfigPath () string {
23
+ return filepath .Join (getHomeDir (), "Library/Application Support/Alfred 2/Workflow Data/" , bundleId )
24
+ }
25
+
26
+ func loadConfig () error {
27
+ configPath := getDefaultConfigPath ()
28
+ viper .SetConfigName ("config" )
29
+ viper .AddConfigPath (configPath )
30
+ viper .SetConfigType ("json" )
31
+ err := viper .ReadInConfig ()
32
+ if err != nil {
33
+ return err
34
+ }
35
+ return nil
36
+ }
37
+
38
+ func saveConfig () error {
39
+ configPath := getDefaultConfigPath ()
40
+ viper .Marshal (& C )
41
+
42
+ buf , err := json .MarshalIndent (C , "" , " " )
43
+ if err != nil {
44
+ return err
45
+ }
46
+
47
+ err = os .MkdirAll (configPath , 0755 )
48
+ if err != nil {
49
+ return err
50
+ }
51
+
52
+ f , err := os .Create (configPath + "/config.json" )
53
+ if err != nil {
54
+ return err
55
+ }
56
+
57
+ defer f .Close ()
58
+
59
+ f .WriteString (string (buf ))
60
+ return nil
61
+ }
Original file line number Diff line number Diff line change 6
6
)
7
7
8
8
const (
9
- bundleId = "co.randompaper.alfred-qiita-workflow"
9
+ bundleId = "co.randompaper.alfred-qiita-workflow"
10
10
)
11
11
12
12
func main () {
@@ -18,20 +18,10 @@ func main() {
18
18
Name : "search" ,
19
19
Action : cmdSearch ,
20
20
},
21
+ {
22
+ Name : "setup" ,
23
+ Action : cmdSetup ,
24
+ },
21
25
}
22
26
app .Run (os .Args )
23
27
}
24
-
25
- func cmdSetToken (c * cli.Context ) {
26
- token := c .Args ().First ()
27
-
28
- configPath := "~/Library/Application Support/Alfred 2/Workflow Data/" + bundleId + "/token"
29
-
30
- fout , err := os .Create (configPath )
31
- if err != nil {
32
- return
33
- }
34
- defer fout .Close ()
35
-
36
- fout .WriteString (token )
37
- }
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
- "github.com/pascalw/go-alfred"
5
4
"fmt"
6
5
"github.com/codegangsta/cli"
6
+ "github.com/pascalw/go-alfred"
7
+ "github.com/spf13/viper"
7
8
"github.com/uetchy/alfred-qiita-workflow/qiita"
8
9
"golang.org/x/oauth2"
9
10
)
10
11
11
12
func cmdSearch (c * cli.Context ) {
12
13
query := c .Args ().First ()
13
14
15
+ loadConfig ()
14
16
ts := oauth2 .StaticTokenSource (
15
- & oauth2.Token {AccessToken : accessToken },
17
+ & oauth2.Token {AccessToken : viper . GetString ( " accessToken" ) },
16
18
)
17
19
tc := oauth2 .NewClient (oauth2 .NoContext , ts )
18
20
client := qiita .NewClient (tc )
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "github.com/codegangsta/cli"
5
+ "github.com/spf13/viper"
6
+ )
7
+
8
+ func cmdSetup (c * cli.Context ) {
9
+ token := c .Args ().First ()
10
+
11
+ loadConfig ()
12
+ viper .Set ("accessToken" , token )
13
+ saveConfig ()
14
+ }
You can’t perform that action at this time.
0 commit comments