-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
82 lines (68 loc) · 1.59 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
package main
import (
"context"
"flag"
"fmt"
"io"
"os"
"os/signal"
"syscall"
"github.com/skibish/ddns/updater"
log "github.com/sirupsen/logrus"
"github.com/skibish/ddns/conf"
"github.com/skibish/ddns/notifier"
)
var (
version string
commit string
date string
)
func main() {
if err := run(os.Args, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run(args []string, stdout io.Writer) error {
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
log.SetLevel(log.DebugLevel)
log.SetOutput(stdout)
flags := flag.NewFlagSet(args[0], flag.ExitOnError)
var (
confFile = flags.String("conf-file", "", "Location of the configuration file. If not provided, searches current directory, then $HOME for ddns.yml file")
showVersion = flags.Bool("ver", false, "Show version")
)
if err := flags.Parse(args[1:]); err != nil {
return fmt.Errorf("failed to parse flags: %w", err)
}
if *showVersion {
fmt.Printf("Version: %s\nCommit: %s\nBuild date: %s\n", version, commit, date)
return nil
}
cf, err := conf.NewConfiguration(*confFile)
if err != nil {
log.Fatal(err)
}
// try to register all provided hooks
for _, v := range cf.Notifications {
hook, err := notifier.GetHook(v)
if err != nil {
log.Debugf("failed to add a notifier: %s", err)
continue
}
log.AddHook(hook)
}
upd := updater.New(cf)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
go func() {
<-ctx.Done()
log.Debug("shutdown")
upd.Stop()
stop()
os.Exit(0)
}()
return upd.Start(ctx)
}