-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (94 loc) · 2.28 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"fmt"
"log"
"os"
. "superpose-sync/adapters"
"superpose-sync/adapters/ConfigFile"
"superpose-sync/adapters/inotify"
"superpose-sync/adapters/sqlite"
"superpose-sync/repositories"
"superpose-sync/services/GoogleAPI"
"superpose-sync/services/SaveGoogleInfo"
"github.com/urfave/cli/v2" // https://cli.urfave.org/v2/
)
const MY_NAME = "Superpose"
const configDir = "."
const watchersFile = configDir + "/watchers.yml"
func main() {
app := &cli.App{
Name: "superpose",
Usage: "In quantum superposition a molecule can be in two (or more) quantun states before measurement. I do this with your files :D",
Action: func(*cli.Context) error {
fmt.Println("\033[32mboom! I say!\033[39m")
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
start()
}
func start() {
err := ConfigFile.ParseFile(watchersFile)
if err != nil {
log.Fatal(err)
}
sqlite.Connect()
Drive = GoogleAPI.NewDrive()
SaveGoogleInfo.StartListener()
startWatchers()
}
var (
watcher *WatcherStruct
)
func startWatchers() {
var err error
watcher, err = Watcher()
if err != nil {
log.Fatal(err)
}
watcher.InotifyWatcher.StartWatch(receiveEvents)
}
type EventPath struct {
Name string
Mask uint32
Watcher inotify.WatchingPath
DriveID string
}
var (
EventPaths = map[string]EventPath{}
Drive = GoogleAPI.GoogleDrive{}
)
func (eventPath EventPath) Is(needle uint32) bool {
return eventPath.Mask&needle == needle
}
func receiveEvents(event inotify.FileEvent) {
var eventPath EventPath
eventPath, ok := EventPaths[event.Name]
if !ok {
id, err := repositories.GetIdByPath(event.Name)
if err != nil {
log.Println("repositories.GetIdByPath error: ", err)
}
eventPath = EventPath{
Name: event.Name,
DriveID: id,
}
}
eventPath.Mask += event.Mask
EventPaths[event.Name] = eventPath
if event.Is(inotify.InCloseWrite) || event.Is(inotify.InDelete) || event.Is(inotify.InDeleteSelf) || event.Is(inotify.InMove) {
delete(EventPaths, eventPath.Name)
syncLocalToRemote(eventPath)
}
}
func syncLocalToRemote(eventPath EventPath) {
if eventPath.Is(inotify.InDelete) || eventPath.Is(inotify.InMovedFrom) {
Drive.Remove(eventPath.DriveID)
return
}
if eventPath.Is(inotify.InCloseWrite) {
Drive.Send(eventPath.Name)
}
}