-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetting.go
57 lines (53 loc) · 1.23 KB
/
setting.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
)
// setting
type settingStruct struct {
path string
AutoSaveFile struct {
Enable bool `json:"enable"`
TimeIntervalSecond int `json:"timeIntervalSecond"`
Notification bool `json:"notification"`
} `json:"autoSaveFile"`
}
// read setting
func (setting *settingStruct) read() (content string, err error) {
const TimeIntervalSecondMinimum int = 10
var buf []byte
// read
setting.path = workPath + string(filepath.Separator) + "mydictionary-demo.setting.json"
buf, err = ioutil.ReadFile(setting.path)
if err != nil {
return
}
err = json.Unmarshal(buf, setting)
if err != nil {
return
}
// check
if setting.AutoSaveFile.TimeIntervalSecond < TimeIntervalSecondMinimum {
setting.AutoSaveFile.TimeIntervalSecond = TimeIntervalSecondMinimum
}
buf, err = json.MarshalIndent(setting, "", "\t")
content = string(buf) + "\n\n"
return
}
// Write : write setting
func (setting *settingStruct) Write() (err error) {
var buf []byte
// write
buf, err = json.MarshalIndent(setting, "", "\t")
if err != nil {
return
}
err = os.Remove(setting.path)
if err != nil {
return
}
err = ioutil.WriteFile(setting.path, buf, 0644)
return
}