-
Notifications
You must be signed in to change notification settings - Fork 6
/
rc.go
50 lines (42 loc) · 927 Bytes
/
rc.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"time"
)
type PKConfig struct {
User string "json:`user`"
URL string "json:`api_url`"
AccessToken string "json:`access_token`"
Expiration time.Time "json:`token_expiraion`"
}
func getRc() (rc *PKConfig, err error) {
rc = new(PKConfig)
rc.URL = "https://pancake.io/v1/"
rcBytes, err := ioutil.ReadFile(rcPath())
if err != nil {
return
}
err = json.Unmarshal(rcBytes, rc)
return rc, err
}
func (p *PKConfig) saveRc() error {
rcBytes, err := json.Marshal(p)
if err != nil {
return err
}
return ioutil.WriteFile(rcPath(), rcBytes, os.FileMode(0644))
}
func rcPath() string {
return filepath.Join(homePath(), ".pk")
}
func homePath() string {
u, err := user.Current()
if err != nil {
panic("couldn't determine user: " + err.Error())
}
return u.HomeDir
}