-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
55 lines (47 loc) · 1.42 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
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"syscall"
"github.com/aleskandro/nextcloud-kobo-synchronizer/pkg"
)
func main() {
configFilePath := flag.String("config-file", "", "The path to the yaml config file")
basePath := flag.String("base-path", "", "The base path to use for relative paths in the config file")
sync := flag.Bool("sync", false, "Run the syncer at startup")
flag.Parse()
config, err := pkg.LoadConfig(*configFilePath, *basePath)
if err != nil {
log.Println("NextCloud Kobo syncer failed at loading config")
log.Println(err)
return
}
ctx := SetupSignalHandler()
controller := pkg.NewNetworkConnectionReconciler(config, ctx)
if *sync {
controller.HandleWmNetworkConnected(ctx)
}
controller.Run(ctx)
}
// Gently stolen from the k8s source code
var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}
var onlyOneSignalHandler = make(chan struct{})
// SetupSignalHandler registers for SIGTERM and SIGINT. A context is returned
// which is canceled on one of these signals. If a second signal is caught, the program
// is terminated with exit code 1.
func SetupSignalHandler() context.Context {
close(onlyOneSignalHandler) // panics when called twice
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 2)
signal.Notify(c, shutdownSignals...)
go func() {
<-c
cancel()
<-c
os.Exit(1) // second signal. Exit directly.
}()
return ctx
}