Skip to content

Commit 8f5e404

Browse files
committed
#5 new: bark push
1 parent 8fefb56 commit 8f5e404

File tree

3 files changed

+60
-50
lines changed

3 files changed

+60
-50
lines changed

cmd/conf.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package cmd
22

33
import (
4-
// "strings"
5-
6-
// "github.com/lollipopkit/gommon/res"
7-
// "github.com/lollipopkit/gommon/term"
84
"github.com/lollipopkit/server_box_monitor/model"
95
"github.com/urfave/cli/v2"
106
)
@@ -21,54 +17,10 @@ func init() {
2117
Usage: "Initialize config file",
2218
Action: handleConfInit,
2319
},
24-
// {
25-
// Name: "edit",
26-
// Aliases: []string{"e"},
27-
// Usage: "Edit config file",
28-
// Action: handleConfEdit,
29-
// },
3020
},
3121
})
3222
}
3323

3424
func handleConfInit(c *cli.Context) error {
3525
return model.InitConfig()
3626
}
37-
38-
// func handleConfEdit(c *cli.Context) error {
39-
// if err := model.ReadAppConfig(); err != nil {
40-
// return err
41-
// }
42-
43-
// typeOptions := []string{"interval", "rate", "name", "rules", "pushes", "exit"}
44-
// opOptions := []string{"add", "remove", "edit", "exit"}
45-
46-
// for {
47-
// ruleIds := []string{}
48-
// for _, rule := range model.Config.Rules {
49-
// ruleIds = append(ruleIds, rule.Id())
50-
// }
51-
// pushNames := []string{}
52-
// for _, push := range model.Config.Pushes {
53-
// pushNames = append(pushNames, push.Name)
54-
// }
55-
// var buf strings.Builder
56-
// buf.WriteString(res.GREEN + "interval: " + res.NOCOLOR + model.Config.Interval + "\n")
57-
// buf.WriteString(res.GREEN + "rate: " + res.NOCOLOR + model.Config.Rate + "\n")
58-
// buf.WriteString(res.GREEN + "name: " + res.NOCOLOR + model.Config.Name + "\n")
59-
// buf.WriteString(res.GREEN + "rules: " + res.NOCOLOR + strings.Join(ruleIds, " | ") + "\n")
60-
// buf.WriteString(res.GREEN + "pushes: " + res.NOCOLOR + strings.Join(pushNames, " | ") + "\n")
61-
// print(buf.String())
62-
63-
// op := term.Option("What to do?", opOptions, len(opOptions)-1)
64-
// if op == 3 {
65-
// break
66-
// }
67-
// question := "Which type to " + opOptions[op] + "?"
68-
// typ := term.Option(question, typeOptions, len(typeOptions)-1)
69-
// switch typ {
70-
// case 0:
71-
72-
// }
73-
// return nil
74-
// }

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
77
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
8-
github.com/labstack/echo/v4 v4.11.0 h1:4Dmi59tmrnFzOchz4EXuGjJhUfcEkU28iDKsiZVOQgw=
9-
github.com/labstack/echo/v4 v4.11.0/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
108
github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=
119
github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
1210
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=

model/push.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"path"
78
"regexp"
89
"strings"
910
"time"
@@ -41,6 +42,12 @@ func (p *Push) GetIface() (PushIface, error) {
4142
return nil, err
4243
}
4344
return iface, nil
45+
case PushTypeBark:
46+
var iface PushIfaceBark
47+
err := json.Unmarshal(p.Iface, &iface)
48+
if err != nil {
49+
return nil, err
50+
}
4451
}
4552
return nil, errors.New("unknown push type")
4653
}
@@ -98,6 +105,7 @@ const (
98105
PushTypeIOS PushType = "ios"
99106
PushTypeWebhook = "webhook"
100107
PushTypeServerChan = "server_chan"
108+
PushTypeBark = "bark"
101109
)
102110

103111
type PushIface interface {
@@ -218,3 +226,55 @@ func (p PushIfaceServerChan) push(args []*PushPair) error {
218226
}
219227
return nil
220228
}
229+
230+
type barkLevel string
231+
232+
const (
233+
barkLevelActive barkLevel = "active"
234+
barkLevelSensitive = "timeSensitive"
235+
barkLevelPassive = "passive"
236+
)
237+
238+
type PushIfaceBark struct {
239+
Server string `json:"server"`
240+
Title string `json:"title"`
241+
Body string `json:"body"`
242+
Level barkLevel `json:"level"`
243+
BodyRegex string `json:"body_regex"`
244+
Code int `json:"code"`
245+
}
246+
247+
func (p PushIfaceBark) push(args []*PushPair) error {
248+
body := p.Body
249+
for _, arg := range args {
250+
body = strings.Replace(body, arg.key, arg.value, 1)
251+
}
252+
if len(p.Server) == 0 {
253+
p.Server = "https://api.day.app"
254+
}
255+
url := path.Join(
256+
p.Server,
257+
p.Title,
258+
body,
259+
)
260+
if len(p.Level) != 0 {
261+
url += fmt.Sprintf("?level=%s", p.Level)
262+
}
263+
resp, code, err := http.Do("GET", url, nil, nil)
264+
if err != nil {
265+
return err
266+
}
267+
if p.Code != 0 && code != p.Code {
268+
return fmt.Errorf("code: %d, resp: %s", code, string(resp))
269+
}
270+
if p.BodyRegex != "" {
271+
reg, err := regexp.Compile(p.BodyRegex)
272+
if err != nil {
273+
return fmt.Errorf("compile regex failed: %s", err.Error())
274+
}
275+
if !reg.Match(resp) {
276+
return fmt.Errorf("resp: %s", string(resp))
277+
}
278+
}
279+
return nil
280+
}

0 commit comments

Comments
 (0)