This repository has been archived by the owner on Feb 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
111 lines (96 loc) · 2.8 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package wecqupt_health_card
import (
"github.com/BurntSushi/toml"
"io/ioutil"
"log"
"os"
)
type UserConfig struct {
BasicInfo
Name string `toml:"name" json:"name"`
Sex string `toml:"sex" json:"xb"`
LocationBig string `toml:"location_big" json:"locationBig"`
LocationSmall string `toml:"location_small" json:"locationSmall"`
Latitude string `toml:"latitude" json:"latitude"`
Longitude string `toml:"longitude" json:"longitude"`
Szdq string `toml:"szdq" json:"szdq"`
Xxdz string `toml:"xxdz" json:"xxdz"`
Ywjcqzbl string `toml:"ywjcqzbl" json:"ywjcqzbl"`
Ywjchblj string `toml:"ywjchblj" json:"ywjchblj"`
Xjzdywqzb string `toml:"xjzdywqzb" json:"xjzdywqzb"`
Twsfzc string `toml:"twsfzc" json:"twsfzc"`
Ywytdzz string `toml:"ywytdzz" json:"ywytdzz"`
Remarks string `toml:"remarks" json:"beizhu"`
Mrdkkey string `toml:"-" json:"mrdkkey"`
}
type BasicInfo struct {
StuNum string `toml:"stu_num" json:"xh"`
Openid string `toml:"openid" json:"openid"`
Timestamp int64 `toml:"-" json:"timestamp"`
}
type ClockConfig struct {
Clocks []int `toml:"clocks"`
Range int `toml:"range"`
}
type EmailConfig struct {
Enable string `toml:"enable"`
Address string `toml:"address"`
Password string `toml:"Password"`
Host string `toml:"host"`
Port string `toml:"port"`
}
type SettingsConfig struct {
Once string `toml:"once"`
ImmediateWork string `toml:"immediate_work"`
TestMail string `toml:"test_mail"`
RetryWhenFailed string `toml:"retry_when_failed"`
RetryTimeGap int `toml:"retry_time_gap"`
RetryCountLimit int `toml:"retry_count_limit"`
ExitAfterRetryFailed string `toml:"exit_after_retry_failed"`
LogPath string `toml:"log_path"`
RandomPos string `toml:"random_position"`
}
type Config struct {
User UserConfig `toml:"user"`
Clock ClockConfig `toml:"clock"`
Email EmailConfig `toml:"email"`
Settings SettingsConfig `toml:"settings"`
}
func ParseConfig() (c Config, err error) {
fp, err := os.Open("config.toml")
if err != nil {
log.Println("open config file:", err)
return
}
defer fp.Close()
content, err := ioutil.ReadAll(fp)
if err != nil {
log.Println("read file error:", err)
return
}
err = toml.Unmarshal(content, &c)
if err != nil {
log.Println("parse config error", err)
return
}
return
}
func TestParseConfig(file string) (c Config, err error) {
fp, err := os.Open(file)
if err != nil {
log.Println("open config file:", err)
return
}
defer fp.Close()
content, err := ioutil.ReadAll(fp)
if err != nil {
log.Println("read file error:", err)
return
}
err = toml.Unmarshal(content, &c)
if err != nil {
log.Println("parse config error", err)
return
}
return
}