1+ package config
2+
3+ import (
4+ "flag"
5+ "strings"
6+ "fmt"
7+ "os"
8+ "gopkg.in/ini.v1"
9+ "github.com/ouqiang/supervisor-event-listener/utils"
10+ )
11+
12+ type Config struct {
13+ NotifyType string
14+ WebHook WebHook
15+ MailServer MailServer
16+ MailUser MailUser
17+ Slack Slack
18+ }
19+
20+ type WebHook struct {
21+ Url string
22+ }
23+
24+ type Slack struct {
25+ WebHookUrl string
26+ Channel string
27+ }
28+
29+ // 邮件服务器
30+ type MailServer struct {
31+ User string
32+ Password string
33+ Host string
34+ Port int
35+ }
36+
37+ // 接收邮件的用户
38+ type MailUser struct {
39+ Email []string
40+ }
41+
42+ func ParseConfig () * Config {
43+ var configFile string
44+ flag .StringVar (& configFile , "c" , "/etc/supervisor-event-listener.ini" , "config file" )
45+ flag .Parse ()
46+ configFile = strings .TrimSpace (configFile )
47+ if configFile == "" {
48+ Exit ("请指定配置文件路径" )
49+ }
50+ file , err := ini .Load (configFile )
51+ if err != nil {
52+ Exit ("读取配置文件失败#" + err .Error ())
53+ }
54+ section := file .Section ("default" )
55+ notifyType := section .Key ("notify_type" ).String ()
56+ notifyType = strings .TrimSpace (notifyType )
57+ if ! utils .InStringSlice ([]string {"mail" , "slack" , "webhook" }, notifyType ) {
58+ Exit ("不支持的通知类型-" + notifyType )
59+ }
60+
61+ config := & Config {}
62+ config .NotifyType = notifyType
63+ switch notifyType {
64+ case "mail" :
65+ config .MailServer = parseMailServer (section )
66+ config .MailUser = parseMailUser (section )
67+ case "slack" :
68+ config .Slack = parseSlack (section )
69+ case "webhook" :
70+ config .WebHook = parseWebHook (section )
71+ }
72+
73+ return config
74+ }
75+
76+ func parseMailServer (section * ini.Section ) MailServer {
77+ user := section .Key ("mail.server.user" ).String ()
78+ user = strings .TrimSpace (user )
79+ password := section .Key ("mail.server.password" ).String ()
80+ password = strings .TrimSpace (password )
81+ host := section .Key ("mail.server.host" ).String ()
82+ host = strings .TrimSpace (host )
83+ port , portErr := section .Key ("mail.server.port" ).Int ()
84+ if user == "" || password == "" || host == "" || portErr != nil {
85+ Exit ("邮件服务器配置错误" )
86+ }
87+
88+ mailServer := MailServer {}
89+ mailServer .User = user
90+ mailServer .Password = password
91+ mailServer .Host = host
92+ mailServer .Port = port
93+
94+ return mailServer
95+ }
96+
97+ func parseMailUser (section * ini.Section ) MailUser {
98+ user := section .Key ("mail.user" ).String ()
99+ user = strings .TrimSpace (user )
100+ if user == "" {
101+ Exit ("邮件收件人配置错误" )
102+ }
103+ mailUser := MailUser {}
104+ mailUser .Email = strings .Split (user , "," )
105+
106+ return mailUser
107+ }
108+
109+
110+ func parseSlack (section * ini.Section ) Slack {
111+ webHookUrl := section .Key ("slack.webhook_url" ).String ()
112+ webHookUrl = strings .TrimSpace (webHookUrl )
113+ channel := section .Key ("slack.channel" ).String ()
114+ channel = strings .TrimSpace (channel )
115+ if webHookUrl == "" || channel == "" {
116+ Exit ("Slack配置错误" )
117+ }
118+
119+ slack := Slack {}
120+ slack .WebHookUrl = webHookUrl
121+ slack .Channel = channel
122+
123+ return slack
124+ }
125+
126+ func parseWebHook (section * ini.Section ) WebHook {
127+ url := section .Key ("webhook_url" ).String ()
128+ url = strings .TrimSpace (url )
129+ if url == "" {
130+ Exit ("WebHookUrl配置错误" )
131+ }
132+ webHook := WebHook {}
133+ webHook .Url = url
134+
135+
136+ return webHook
137+ }
138+
139+ func Exit (msg string ) {
140+ fmt .Fprintln (os .Stderr , msg )
141+ os .Exit (1 )
142+ }
0 commit comments