-
Notifications
You must be signed in to change notification settings - Fork 0
/
mash.go
64 lines (50 loc) · 1.04 KB
/
mash.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
package main
import (
"context"
"log"
"os"
"os/signal"
"github.com/sysbind/mash/moodle/config"
"github.com/sysbind/mash/moodle/course/backup"
"github.com/sysbind/mash/moodle/database"
)
func main() {
cfg, db, stop := setup()
defer stop()
defer db.Close()
ab, err := backup.LoadAutoBackup(cfg)
if err != nil {
log.Fatal(err)
}
err = ab.PreFlight()
if err != nil {
log.Fatal(err)
}
err = ab.Run()
if err != nil {
log.Fatal(err)
}
}
// setup parses config, check db connection and creates context
func setup() (cfg config.Config, db database.Database, stop context.CancelFunc) {
cfg, err := config.Parse("config.php")
if err != nil {
log.Fatal(err)
}
ctx, stop := context.WithCancel(context.Background())
db, err = database.Open(ctx, cfg.DriverName(), cfg.DSN())
if err != nil {
log.Fatal(err)
}
cfg.SetDatabase(db)
// Catch OS interupt to cancel the context (stop())
appSignal := make(chan os.Signal, 3)
signal.Notify(appSignal, os.Interrupt)
go func() {
select {
case <-appSignal:
stop()
}
}()
return
}