-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (45 loc) · 1021 Bytes
/
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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
_ "time/tzdata"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/joho/godotenv"
"github.com/slack-go/slack"
"go.uber.org/zap"
)
func main() {
zlog, err := zap.NewProduction()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "could not instantiate logger: %v", err)
os.Exit(1)
}
log := zapr.NewLogger(zlog)
if err := realMain(log); err != nil {
log.Error(err, "an error occurred")
os.Exit(1)
}
}
func realMain(log logr.Logger) error {
_ = godotenv.Load()
cfg, err := loadConfig()
if err != nil {
return fmt.Errorf("could not load configuration: %w", err)
}
slackAPI := slack.New(cfg.slackToken)
carer := newCarer(&cfg, slackAPI, log)
ctx, cancel := context.WithCancel(context.Background())
go callOnShutdown(cancel)
return carer.run(ctx)
}
func callOnShutdown(f func()) {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGTERM, os.Interrupt)
defer signal.Stop(sigc)
<-sigc
f()
}