-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (64 loc) · 1.51 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
package main
import (
"log"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/trustmedis/s3-files-migrator/lib"
)
func main() {
config := lib.LoadConfig()
// Create new watcher.
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// Upload existing files based on config
if config.AUTOMOVE_EXISTING_FILES {
for _, path := range config.WATCH_DIR {
// scan files in each directory
files, err := filepath.Glob(filepath.Join(path, "*"))
if err != nil {
log.Println("Error scanning files: ", err)
}
for _, file := range files {
targetFilePath := filepath.Base(file)
lib.UploadFile(config, file, targetFilePath)
}
}
}
// Start listening for events.
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if !event.Has(fsnotify.Remove) {
targetFilePath := filepath.Base(event.Name)
lib.UploadFile(config, event.Name, targetFilePath)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
// Add paths to watch.
for _, path := range config.WATCH_DIR {
err = watcher.Add(path)
if err != nil {
log.Fatal("ERROR : ", err)
}
}
// Block main goroutine forever.
if config.AUTO_CLEANUP {
log.Println("**WARNING** : Auto cleanup is enabled. This will remove all files in", config.WATCH_DIR, "immediately.")
}
log.Println("Watching", strings.Join(config.WATCH_DIR, ", "))
<-make(chan struct{})
}