-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (68 loc) · 1.4 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
package main
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/jordi-reinsma/bagel/core"
"github.com/jordi-reinsma/bagel/db"
"github.com/jordi-reinsma/bagel/slack"
)
var (
RESET = os.Getenv("RESET") == "1"
MOCK = os.Getenv("MOCK") == "1"
)
func main() {
fmt.Println("Starting bagel...")
rand.Seed(time.Now().UnixNano())
dB := db.MustConnect(RESET)
defer dB.Close()
skip, err := core.ShouldSkipExecution(dB)
if err != nil {
panic(err)
}
if skip {
fmt.Println("Skipping execution")
return
}
slack := slack.New(MOCK)
channelIDs := slack.GetChannelUUIDs()
for _, channelID := range channelIDs {
fmt.Println("Generating matches for channel", channelID)
userUUIDs, err := slack.GetUserUUIDs(channelID)
if err != nil {
panic(err)
}
users, err := dB.AddAndGetUsers(userUUIDs)
if err != nil {
panic(err)
}
fmt.Println("Users:", len(users))
matches, err := core.GenerateMatches(dB, users)
if err != nil {
panic(err)
}
for _, match := range matches {
err = slack.SendMessage(match, channelID)
if err != nil {
fmt.Println(match, err)
continue
}
err = dB.UpdateMatch(match)
if err != nil {
fmt.Println(match, err)
continue
}
}
err = slack.SendChannelMessage(channelID)
if err != nil {
panic(err)
}
fmt.Println("Matches:", len(matches))
}
err = dB.SaveExecution()
if err != nil {
panic(err)
}
fmt.Println("Done!")
}