-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
59 lines (47 loc) · 1.05 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"encoding/json"
"os"
"path/filepath"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
type config struct {
AccessToken string `json:"accessToken"`
ID string `json:"id"`
}
var marshaledConfig config
func getDefaultConfigPath() string {
homeDir, _ := homedir.Dir()
return filepath.Join(homeDir, "Library/Application Support/Alfred 3/Workflow Data/", bundleID)
}
func loadConfig() error {
configPath := getDefaultConfigPath()
viper.SetConfigName("config")
viper.AddConfigPath(configPath)
viper.SetConfigType("json")
err := viper.ReadInConfig()
if err != nil {
return err
}
return nil
}
func saveConfig() error {
configPath := getDefaultConfigPath()
viper.Unmarshal(&marshaledConfig)
buf, err := json.MarshalIndent(marshaledConfig, "", " ")
if err != nil {
return err
}
err = os.MkdirAll(configPath, 0755)
if err != nil {
return err
}
f, err := os.Create(filepath.Join(configPath, "config.json"))
if err != nil {
return err
}
defer f.Close()
f.WriteString(string(buf))
return nil
}