forked from RitterHou/time-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
57 lines (49 loc) · 1.24 KB
/
conf.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 (
"io/ioutil"
"log"
"os"
"regexp"
"strings"
)
var (
conf = make(map[string]string)
)
const (
fileName = "settings.ini"
content = `; Generated by https://github.com/RitterHou/time-alert
; 10/20/30/60/..., alert will be touched off if current_minute % this_number == 0
alert_time_point=30
; disable alert on this hours
; disabled_hours=22,23
`
)
func getConf(confFile string) map[string]string {
if _, err := os.Stat(confFile); os.IsNotExist(err) {
writeFile(confFile, strings.Replace(content, "\n", "\r\n", -1))
}
confContent := readFile(confFile)
var re = regexp.MustCompile("(;[\\d\\D]*?\n)") // 移除注释内容
confContent = re.ReplaceAllString(confContent, "")
confContent = strings.Replace(confContent, "\r", "\n", -1)
for _, line := range strings.Split(confContent, "\n") {
if strings.Contains(line, "=") {
value := strings.Split(line, "=")
conf[strings.Trim(value[0], " ")] = strings.Trim(value[1], " ")
}
}
return conf
}
func readFile(filePath string) string {
data, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatalln(err)
}
return string(data)
}
func writeFile(filePath string, data string) {
err := ioutil.WriteFile(filePath, []byte(data), 0644)
if err != nil {
log.Fatalln(err)
}
}