Skip to content

Commit 0313bb7

Browse files
committed
Add config
1 parent 31430d7 commit 0313bb7

File tree

4 files changed

+84
-17
lines changed

4 files changed

+84
-17
lines changed

config.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}

main.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
const (
9-
bundleId = "co.randompaper.alfred-qiita-workflow"
9+
bundleId = "co.randompaper.alfred-qiita-workflow"
1010
)
1111

1212
func main() {
@@ -18,20 +18,10 @@ func main() {
1818
Name: "search",
1919
Action: cmdSearch,
2020
},
21+
{
22+
Name: "setup",
23+
Action: cmdSetup,
24+
},
2125
}
2226
app.Run(os.Args)
2327
}
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-
}

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-
"github.com/pascalw/go-alfred"
54
"fmt"
65
"github.com/codegangsta/cli"
6+
"github.com/pascalw/go-alfred"
7+
"github.com/spf13/viper"
78
"github.com/uetchy/alfred-qiita-workflow/qiita"
89
"golang.org/x/oauth2"
910
)
1011

1112
func cmdSearch(c *cli.Context) {
1213
query := c.Args().First()
1314

15+
loadConfig()
1416
ts := oauth2.StaticTokenSource(
15-
&oauth2.Token{AccessToken: accessToken},
17+
&oauth2.Token{AccessToken: viper.GetString("accessToken")},
1618
)
1719
tc := oauth2.NewClient(oauth2.NoContext, ts)
1820
client := qiita.NewClient(tc)

setup.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}

0 commit comments

Comments
 (0)