-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathstore.go
80 lines (68 loc) · 1.72 KB
/
store.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package store
import (
"encoding/base64"
"encoding/json"
log "github.com/sirupsen/logrus"
"github.com/vouv/srun/model"
"os"
"os/user"
"path/filepath"
)
const accountFileName = "account.json"
var RootPath string
func SetAccount(username, password string) (err error) {
return WriteAccount(&model.Account{
Username: username,
Password: password,
})
}
func ReadAccount() (account *model.Account, err error) {
file, err := OpenAccountFile(os.O_RDONLY)
if err != nil {
log.Debugf("打开账号文件错误, %s,", err)
return
}
defer file.Close()
err = json.NewDecoder(base64.NewDecoder(base64.RawStdEncoding, file)).Decode(&account)
return
}
func OpenAccountFile(flag int) (file *os.File, err error) {
accountFilename, err := getAccountFilename()
if err != nil {
return
}
return os.OpenFile(accountFilename, flag, 0600)
}
func WriteAccount(account *model.Account) (err error) {
file, err := OpenAccountFile(os.O_CREATE | os.O_TRUNC | os.O_WRONLY)
if err != nil {
log.Debugf("打开账号文件错误, %s", err)
return
}
defer file.Close()
enc := base64.NewEncoder(base64.RawStdEncoding, file)
err = json.NewEncoder(enc).Encode(account)
if err != nil {
return err
}
return enc.Close()
}
func getAccountFilename() (fileSrc string, err error) {
storageDir := filepath.Join(RootPath, ".srun")
if _, sErr := os.Stat(storageDir); sErr != nil {
if mErr := os.MkdirAll(storageDir, 0755); mErr != nil {
log.Debugf("mkdir `%s` error, %s", storageDir, mErr)
return
}
}
fileSrc = filepath.Join(storageDir, accountFileName)
return
}
func init() {
curUser, gErr := user.Current()
if gErr != nil {
log.Fatalln("无法读取账户信息, 请重新设置账户信息")
} else {
RootPath = curUser.HomeDir
}
}