-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (63 loc) · 1.42 KB
/
main.go
File metadata and controls
79 lines (63 loc) · 1.42 KB
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
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/hpcloud/tail"
"github.com/slack-go/slack"
)
func main() {
logfile, pattern, message, webhook := validargs(os.Args)
tailConfig := tail.Config{
Follow: true,
Location: &tail.SeekInfo{
Whence: io.SeekEnd,
},
}
t, err := tail.TailFile(logfile, tailConfig)
if err != nil {
fmt.Printf("Cant tail the file: %s\n", err)
os.Exit(1)
}
for line := range t.Lines {
if strings.Contains(line.Text, pattern) {
sendMsg(webhook, message)
os.Exit(0)
}
}
}
func validargs(args []string) (logfile string, pattern string, message string, webhook string) {
if len(args) != 4 {
help()
os.Exit(1)
}
logfile = os.Args[1]
pattern = os.Args[2]
message = os.Args[3]
webhook = os.Getenv("SLACK_WEBHOOK_URL")
if _, err := os.Stat(logfile); os.IsNotExist(err) {
fmt.Printf("The file %s doesn't exist.\n", logfile)
os.Exit(1)
}
if webhook == "" {
fmt.Printf("You need to set $SLACK_WEBHOOK_URL.\n")
os.Exit(1)
}
return
}
func help() {
fmt.Printf("boink - a tool to notify you when a line is in a log file.\n\n")
fmt.Printf("USAGE\n")
fmt.Printf(" export SLACK_WEBHOOK_URL='https://...'\n")
fmt.Printf(" boink [logfile] '[Substr pattern]' '[message to send]'\n")
}
func sendMsg(webhook string, message string) {
msg := slack.WebhookMessage{
Text: message,
}
err := slack.PostWebhook(webhook, &msg)
if err != nil {
fmt.Println(err)
}
}