-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
60 lines (47 loc) · 1.07 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
60
package main
import "os"
import "os/user"
import "path"
import "encoding/json"
import "io/ioutil"
type Config struct {
GitHubAccessToken string `json:"github_access_token"`
MediumAccessToken string `json:"medium_access_token"`
MediumUserId string `json:"medium_user_id"`
}
func LoadConfig() (*Config, error) {
config := &Config{}
configPath, err := ConfigPath()
if err != nil {
return nil, err
}
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return config, nil
} else if err != nil {
return nil, err
}
configContents, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, err
}
json.Unmarshal(configContents, config)
return config, nil
}
func SaveConfig(config *Config) error {
configPath, err := ConfigPath()
if err != nil {
return err
}
configJson, _ := json.Marshal(config)
if err := ioutil.WriteFile(configPath, configJson, 0644); err != nil {
return err
}
return nil
}
func ConfigPath() (string, error) {
user, err := user.Current()
if err != nil {
return "", err
}
return path.Join(user.HomeDir, ".md2medium"), nil
}