Skip to content

Commit 4206771

Browse files
committed
小重构:
* 支持多目标通知 * 配置文件改用 toml 格式,不再需要手动 trim * 引入自动 validate,不再手动校验配置错误。 * 增加飞书支持
1 parent 14c6224 commit 4206771

24 files changed

+489
-523
lines changed

build.sh

Lines changed: 0 additions & 96 deletions
This file was deleted.

conf/config.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package conf
2+
3+
import (
4+
"fmt"
5+
"sync/atomic"
6+
7+
validator "github.com/go-playground/validator/v10"
8+
)
9+
10+
var (
11+
// c = &Config{}
12+
cfg atomic.Value
13+
)
14+
15+
type Config struct {
16+
DryRun *DryRun
17+
WebHook *WebHook
18+
Mail *Mail
19+
Slack *Slack
20+
BearyChat *BearyChat
21+
Feishu *Feishu
22+
}
23+
24+
func (c *Config) Validate() error {
25+
if c == nil {
26+
return fmt.Errorf("nil config")
27+
}
28+
29+
if c.BearyChat != nil {
30+
if c.BearyChat.URL == "" {
31+
return fmt.Errorf("Invalid bearcychat config")
32+
}
33+
}
34+
if c.Mail != nil {
35+
if len(c.Mail.Receivers) <= 0 {
36+
return fmt.Errorf("Invalid mail config")
37+
}
38+
}
39+
if c.Slack != nil {
40+
if c.Slack.URL == "" {
41+
return fmt.Errorf("Invalid slack config")
42+
}
43+
}
44+
45+
if c.WebHook != nil {
46+
if c.WebHook.URL == "" {
47+
return fmt.Errorf("Invalid slack config")
48+
}
49+
}
50+
51+
validate := validator.New()
52+
if err := validate.Struct(c); err != nil {
53+
return err
54+
}
55+
return nil
56+
}

conf/init.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package conf
2+
3+
import (
4+
"io"
5+
"os"
6+
7+
"github.com/pelletier/go-toml"
8+
)
9+
10+
func Init(fpath string) error {
11+
f, err := os.Open(fpath)
12+
if err != nil {
13+
return err
14+
}
15+
data, err := io.ReadAll(f)
16+
if err != nil {
17+
return err
18+
}
19+
c := Config{}
20+
if err := toml.Unmarshal(data, &c); err != nil {
21+
return err
22+
}
23+
if err := c.Validate(); err != nil {
24+
return err
25+
}
26+
cfg.Store(&c)
27+
return nil
28+
}
29+
30+
func Reload(fpath string) error {
31+
return Init(fpath)
32+
}
33+
34+
func Get() *Config {
35+
return cfg.Load().(*Config)
36+
}

conf/listeners.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package conf
2+
3+
type DryRun struct {
4+
Text string `toml:"text"`
5+
}
6+
7+
// 邮件服务器
8+
type Mail struct {
9+
Receivers []string `toml:"receivers" validate:"required"`
10+
ServerUser string `toml:"server_user" validate:"required"`
11+
ServerPassword string `toml:"server_password" validate:"required"`
12+
ServerHost string `toml:"server_host" validate:"required"`
13+
ServerPort int `toml:"server_port" validate:"required"`
14+
}
15+
16+
type WebHook struct {
17+
URL string `toml:"url" validate:"required"`
18+
Timeout int `toml:"timeout"`
19+
}
20+
21+
type Slack struct {
22+
URL string `toml:"url" validate:"required"`
23+
Channel string `toml:"channel" validate:"required"`
24+
}
25+
26+
type BearyChat struct {
27+
URL string `toml:"url" validate:"required"`
28+
Channel string `toml:"channel"`
29+
Timeout int `toml:"timeout"`
30+
}
31+
32+
type Feishu struct {
33+
URL string `toml:"url" validate:"required"`
34+
Timeout int `toml:"timeout"`
35+
}

config/config.go

Lines changed: 0 additions & 165 deletions
This file was deleted.

0 commit comments

Comments
 (0)