/
notify_send.go
59 lines (48 loc) · 1.23 KB
/
notify_send.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
package channel
import (
"github.com/kos-v/sensors-informer/internal/config"
"github.com/kos-v/sensors-informer/internal/message"
"github.com/kos-v/sensors-informer/internal/report"
"os"
"os/exec"
"strconv"
"strings"
)
type NotifySendChannel struct {
Opts config.NotifySendChannelOpts
MessageFormatter message.Formatter
}
func (ch *NotifySendChannel) GetName() string {
return "notifySend"
}
func (ch *NotifySendChannel) IsEnable() bool {
return ch.Opts.Enable
}
func (ch *NotifySendChannel) Send(r report.Report) error {
args := []string{
"-u",
string(ch.Opts.Urgency),
"-t",
strconv.Itoa(ch.Opts.ExpireTime),
"-a",
"sensors-informer",
}
if ch.Opts.Hint != "" {
args = append(args, "-h", ch.Opts.Hint)
}
args = append(args, ch.formatTitle(&r), ch.formatBody(&r))
os.Setenv("DISPLAY", ":0.0")
cmd := exec.Command(ch.Opts.Command, args...)
_, err := cmd.Output()
return err
}
func (ch *NotifySendChannel) formatTitle(r *report.Report) string {
title := ch.MessageFormatter.FormatTitle(r)
if title == "" {
return "Critical temperature readings"
}
return title
}
func (ch *NotifySendChannel) formatBody(r *report.Report) string {
return strings.Join(ch.MessageFormatter.FormatBodyRows(r), "\n")
}