aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: 570890df9b159e97867a09815093540ce15ac7b0 (plain) (blame)
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
package main

import (
	"context"
	"flag"
	"log"
	"os"
	"os/signal"
	"syscall"
)

var (
	hostDir  = flag.String("host", "/host", "Host source directory (read-only)")
	dataDir  = flag.String("data", "/data", "Data destination directory")
	verbose  = flag.Bool("verbose", false, "Enable verbose logging (shorthand for -log-level debug)")
	logLevel = flag.String("log-level", "info", "Log level: debug, info, warn, error, none")
	dryRun   = flag.Bool("dry-run", false, "Preview what would be synced without copying")
)

func main() {
	flag.Parse()

	if *verbose {
		*logLevel = "debug"
	}

	level, err := ParseLogLevel(*logLevel)
	if err != nil {
		log.Fatal(err)
	}
	SetLogLevel(level)

	log.SetFlags(0)
	log.SetOutput(nil)

	stack := NewGitignoreStack(*hostDir)

	manifest, err := initialSync(*hostDir, *dataDir, stack, *dryRun)
	if err != nil {
		logErrorf("initial sync failed: %v", err)
		os.Exit(1)
	}

	if *dryRun {
		logInfo("dry-run complete, exiting")
		return
	}

	logInfo("watcher: ready, monitoring for changes ...")

	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer cancel()

	if err := watch(ctx, *hostDir, *dataDir, manifest, stack, *dryRun); err != nil {
		logErrorf("watcher failed: %v", err)
		os.Exit(1)
	}
}