-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (82 loc) · 2.23 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
_ "embed"
"fmt"
"github.com/antfie/FoxBot/config"
"github.com/antfie/FoxBot/db"
"github.com/antfie/FoxBot/slack"
"github.com/antfie/FoxBot/tasks"
"github.com/antfie/FoxBot/utils"
"log"
"os"
"os/signal"
"runtime"
"syscall"
)
//go:embed config.yaml
var defaultConfigData []byte
//goland:noinspection GoUnnecessarilyExportedIdentifiers
var AppVersion = "0.0"
func main() {
defer func() {
if r := recover(); r != nil {
buf := make([]byte, 1024)
stackSize := runtime.Stack(buf, false)
stackTrace := string(buf[:stackSize])
log.Printf("Main panic recovered: %v\nStack trace:\n%s", r, stackTrace)
}
}()
print(fmt.Sprintf("FoxBot version %s\n", AppVersion))
c := config.Load(defaultConfigData)
if c.CheckForNewVersions {
checkForUpdates()
}
task := &tasks.Context{
Config: c,
DB: db.NewDB(c.DBPath),
}
if c.Output.Slack != nil {
task.Slack = slack.NewSlack(c.Output.Slack, task.DB)
}
var tasksToRun []*tasks.Task
if c.Reminders != nil {
if len(c.Reminders.Reminders) < 1 {
log.Print("No reminders configured.")
} else {
tasksToRun = append(tasksToRun, tasks.NewTask(c.Reminders.Check.Frequency, task.Reminders))
}
}
if c.Countdown != nil {
if len(c.Countdown.Timers) < 1 {
log.Print("No countdown timers configured.")
} else {
tasksToRun = append(tasksToRun, tasks.NewTask(c.Countdown.Check.Frequency, task.Countdown))
}
}
if c.RSS != nil {
if len(c.RSS.Feeds) < 1 {
log.Print("No RSS feeds configured.")
} else {
tasksToRun = append(tasksToRun, tasks.NewTask(c.RSS.Check.Frequency, task.RSS))
}
}
if c.SiteChanges != nil {
if len(c.SiteChanges.Sites) < 1 {
log.Print("No sites to monitor configured.")
} else {
tasksToRun = append(tasksToRun, tasks.NewTask(c.SiteChanges.Check.Frequency, task.SiteChanges))
}
}
if len(tasksToRun) == 0 {
log.Print("Error: No tasks to run.")
os.Exit(1)
}
task.Notify(fmt.Sprintf("🦊🤖 Running with %s.", utils.Pluralize("task", len(tasksToRun))))
go tasks.Run(tasksToRun)
shutdownSignal := make(chan os.Signal, 1)
signal.Notify(shutdownSignal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-shutdownSignal
// Clear out any "^C" from the console
print("\r")
task.Notify("🦊🤖 Stopped")
}