-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
77 lines (59 loc) · 1.41 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
package main
import (
"context"
"log/slog"
"os"
"os/signal"
"sync"
"syscall"
"github.com/BurntSushi/toml"
"github.com/docopt/docopt-go"
)
var version = "develop"
const usage = `resyncd – synchronize changed files to a remote servers.
usage:
resyncd -h | --help
resyncd [options] <config>
options:
-v --verbose Verbose logging.
`
func main() {
args, err := docopt.ParseArgs(usage, nil, version)
if err != nil {
panic(err)
}
logger := setupLogger(args["--verbose"].(bool))
var conf config
if _, err := toml.DecodeFile(args["<config>"].(string), &conf); err != nil {
logger.Error("unable to read configuration file", slog.Any("err", err))
os.Exit(1)
}
if len(conf.Syncs) == 0 {
logger.Error("no configuration found")
os.Exit(1)
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
var wg sync.WaitGroup
for _, syncConf := range conf.Syncs {
filesCh := make(chan string)
wg.Add(1)
go func() {
defer wg.Done()
err := watchDirectory(ctx, logger, syncConf, filesCh)
if err != nil && err != context.Canceled {
logger.Error("error watching directory", slog.Any("err", err))
cancel()
}
}()
wg.Add(1)
go func() {
defer wg.Done()
bufferedCh := bufferize(filesCh, bufferizeDuration)
rsyncDirectory(logger, syncConf, bufferedCh)
}()
}
<-ctx.Done()
logger.Info("stopping synchronization")
wg.Wait()
}